answersLogoWhite
Jobs & Education
Database Programming
C Programming
The Difference Between

What is meant by unsigned char?

394041

Top Answer
User Avatar
Wiki User
2009-10-08 23:43:43
2009-10-08 23:43:43
Answer

An unsigned char is a byte; its value can be between 0 and (2^8) - 1 (i.e., 0-255).

1

Related Questions

User Avatar

for example: unsigned char attach (unsigned char byte, unsigned char bit) { unsigned char mybyte; mybyte = byte&0x7f; if (bit) mybyte |= 0x80; return mybyte; }

User Avatar

Yes: unsigned char CircLeft (unsigned char value) { if (value&0x80) return (value<<1) + 1; else return (value<<1); } unsigned char CircRight (unsigned char value) { if (value&0x01) return (value>>1) + 0x80; else return (value>>1); }

User Avatar

unsigned char * memcpy(unsigned char * s1, unsigned char * s2, long size) { long ix; s1= (char *)malloc(sizeof(strlen(s2))); for(ix=0; ix < size; ix++) s1[ix] = s2[ix]; return s1; }

User Avatar

It's their value range:signed char: -128..127unsigned char: 0..255

User Avatar

You can find a copy of that file in here: http://ftp.ibiblio.org/pub/micro/PC-stuff/freedos/files/devel/c/dos.h in case the server is down check the extracted file: #ifndef __DOS_H #define __DOS_H #include <_defs.h> #include <stddef.h> #ifdef __WATCOMC__ #pragma pack( __push, 1 ) #endif struct _R16BIT { unsigned short ax, bx, cx, dx, si, di, es, cs, ss, ds, flags; unsigned char cflag; }; struct _R8BIT { unsigned char al, ah, bl, bh, cl, ch, dl, dh; }; union _INTR { struct _R16BIT x; struct _R8BIT h; }; #define INTR _INTR struct country { int co_date; /* date format */ char co_curr[ 5 ]; /* currency symbol */ char co_thsep[ 2 ]; /* thousands separator */ char co_desep[ 2 ]; /* decimal separator */ char co_dtsep[ 2 ]; /* date separator */ char co_tmsep[ 2 ]; /* time separator */ char co_currstyle; /* currency style */ char co_digits; /* significant digits in currency */ char co_time; /* time format */ long co_case; /* case map */ char co_dasep[ 2 ]; /* data separator */ char co_fill[ 10 ]; /* filler */ }; #define COUNTRY country struct DOSERROR { int de_exterror; /* extended error */ char de_class; /* error class */ char de_action; /* action */ char de_locus; /* error locus */ }; struct date { unsigned int da_year; /* current year */ unsigned char da_day; /* day of the month */ unsigned char da_mon; /* month (1 = Jan) */ }; struct dosdate_t { unsigned char day; /* 1--31 */ unsigned char month; /* 1--12 */ unsigned int year; /* 1980--2099 */ unsigned char dayofweek; /* 0--6; 0 = Sunday */ }; struct devhdr { long dh_next; short dh_attr; unsigned short dh_strat; unsigned short dh_inter; char dh_name[ 8 ]; }; struct dfree { unsigned df_avail; /* Available clusters */ unsigned df_total; /* Total clusters */ unsigned df_bsec; /* Bytes per sector */ unsigned df_sclus; /* Sectors per cluster */ }; struct diskfree_t { unsigned total_clusters; unsigned avail_clusters; unsigned sectors_per_cluster; unsigned bytes_per_sector; }; typedef struct { char drive; char pattern [ 13 ]; char reserved [ 7 ]; char attrib; short time; short date; long size; char nameZ [ 13 ]; } dosSearchInfo; struct fatinfo { char fi_sclus; /* sectors per cluster */ char fi_fatid; /* the FAT id byte */ int fi_nclus; /* number of clusters */ int fi_bysec; /* bytes per sector */ }; struct ffblk { #ifdef __CLIB_LFN__ unsigned short cr_time; /* time of file creation */ unsigned short cr_date; /* date of file creation */ unsigned short ac_time; /* time of last file access */ unsigned short ac_date; /* date of last file access */ char ff_reserved[ 13 ]; /* reserved for use by DOS */ #else char ff_reserved[ 21 ]; /* reserved for use by DOS */ #endif char ff_attrib; /* attribute byte for file */ unsigned short ff_ftime; /* time of last write to file */ unsigned short ff_fdate; /* date of last write to file */ unsigned long ff_fsize; /* length of file in bytes */ #ifdef __CLIB_LFN__ char ff_name[ 256 ]; /* null-terminated filename */ unsigned short lfnhandle;/* DOS LFN support handle */ #else char ff_name[ 13 ]; /* null-terminated filename */ #endif }; struct find_t { #ifdef __CLIB_LFN__ unsigned short cr_time; /* time of file creation */ unsigned short cr_date; /* date of file creation */ unsigned short ac_time; /* time of last file access */ unsigned short ac_date; /* date of last file access */ char reserved[ 13 ]; /* reserved for use by DOS */ #else char reserved[ 21 ]; /* reserved for use by DOS */ #endif char attrib; /* attribute byte for file */ unsigned short wr_time; /* time of last write to file */ unsigned short wr_date; /* date of last write to file */ unsigned long size; /* length of file in bytes */ #ifdef __CLIB_LFN__ char name[ 256 ]; /* null-terminated filename */ unsigned short lfnhandle;/* DOS LFN support handle */ #else char name[ 13 ]; /* null-terminated filename */ #endif }; #define _find_t find_t struct fcb { char fcb_drive; char fcb_name[ 8 ], fcb_ext[ 3 ]; short fcb_curblk, fcb_recsize; long fcb_filsize; short fcb_date; char fcb_resv[ 10 ], fcb_currec; long fcb_random; }; struct xfcb { char xfcb_flag; char xfcb_resv[ 5 ]; char xfcb_attr; struct fcb xfcb_fcb; }; struct dostime_t { unsigned char hour; /* Hours */ unsigned char minute; /* Minutes */ unsigned char second; /* Seconds */ unsigned char hsecond; /* Hundredths of seconds */ }; struct time { unsigned char ti_min; /* minutes */ unsigned char ti_hour; /* hours */ unsigned char ti_hund; /* hundredths of seconds */ unsigned char ti_sec; /* seconds */ }; #ifdef __WATCOMC__ #pragma pack( __pop ) #endif extern int __getversion( void ); extern char ** environ; #define _version (*__getversion)() extern unsigned char _osmajor; extern unsigned char _osminor; extern int absread( int drive, int sects, long lsect, void *buffer ); extern int abswrite( int drive, int sects, long lsect, void *buffer ); extern int allocmem( unsigned size, unsigned *seg ); extern int bdos( int ah, unsigned dx, unsigned al ); extern int bdosptr( int ah, void *argument, unsigned al ); extern int _callint( union INTR *regs ); extern struct COUNTRY * country( int xcode, struct COUNTRY *ct ); extern void ctrlbrk( int ( *handler )( void ) ); extern void delay( unsigned mill ); extern void _disable( void ); extern unsigned _dos_allocmem( unsigned size, unsigned *seg ); extern unsigned _dos_close( int handle ); extern unsigned _dos_creat( const char *path, int attr, unsigned *handle ); extern unsigned _dos_creatnew( const char *path, int attr, unsigned *handle ); extern int __cdecl dosexterror( struct DOSERROR *errblk ); extern unsigned _dos_findfirst( char *filename, int attrib, void *strptr ); extern unsigned _dos_findnext( void *strptr ); extern unsigned _dos_findclose( void *strptr ); extern unsigned _dos_freemem( unsigned seg ); extern void __cdecl _dos_getdate( struct dosdate_t *ptr ); extern unsigned __cdecl _dos_getdiskfree( unsigned char dr, struct diskfree_t *d ); extern unsigned _dos_getdrive( unsigned *disk ); extern unsigned _dos_getfileattr( const char *filename, unsigned *attrs ); extern unsigned __cdecl _dos_getftime( int handle, unsigned *date, unsigned *time ); extern void __cdecl _dos_gettime( struct dostime_t *timeptr ); extern void ( interrupt far * _dos_getvect( unsigned intno ) )( ); extern void _dos_keep( unsigned char retcode, unsigned size ); extern unsigned _dos_open( const char *path, unsigned flags, unsigned *handle ); extern unsigned _dos_read( int handle, void far *buf, unsigned len, unsigned *nread ); extern unsigned _dos_setblock( unsigned newsize, unsigned seg, unsigned *max ); extern void _dos_setdate( struct dosdate_t *ptr ); extern void _dos_setdrive( unsigned disk, unsigned *total ); extern unsigned _dos_setfileattr( const char *filename, unsigned attrs ); extern unsigned _dos_setftime( int handle, unsigned date, unsigned time ); extern unsigned _dos_settime( struct dostime_t *timeptr ); extern void _dos_setvect( unsigned intno, void ( interrupt far *vect )() ); extern time_t dostounix( struct date *date, struct time *time ); extern unsigned _dos_write( int handle, void far *buf, unsigned len, unsigned *bytes ); extern void _enable( void ); extern int freemem( unsigned seg ); extern int getcbrk( void ); extern void __cdecl getdate( struct date *datep ); extern void __cdecl getdfree( unsigned char drive, struct dfree *dtable ); extern char * getdta( void ); extern void getfat( unsigned char drive, struct fatinfo *dtable ); extern void getfatd( struct fatinfo *dtable ); extern unsigned getpsp( void ); extern void __cdecl gettime( struct time *timeptr ); extern void ( interrupt far * getvect( int intno ) )( ); extern int getverify( void ); extern int inp( unsigned id ); extern unsigned inpw( unsigned id ); extern unsigned inport( unsigned id ); extern unsigned char inportb( unsigned id ); extern void keep( unsigned char retcode, unsigned size ); extern void nosound( void ); extern int outp( unsigned id, int value ); extern unsigned outpw( unsigned id, unsigned value ); extern void outport( unsigned id, unsigned value ); extern void outportb( unsigned id, unsigned char value ); extern char * parsfnm( const char *cmdline, struct fcb *ptr, int al ); extern int peek( unsigned seg, unsigned offs ); extern char peekb( unsigned seg, unsigned offs ); extern void poke( unsigned seg, unsigned offs, int value ); extern void pokeb( unsigned seg, unsigned offs, char value ); extern int randbrd( struct fcb *buf, int rnum ); extern int randbwr( struct fcb *buf, int rnum ); extern int setblock( unsigned newsize, unsigned seg ); extern int setcbrk( int value ); extern void setdate( struct date *datep ); extern void setdta( char far *dta ); extern void setpsp( unsigned psp ); extern void settime( struct time *timeptr ); extern void setvect( int intno, void ( interrupt far *vect )() ); extern void setverify( int flag ); extern void sleep( unsigned x ); extern void sound( unsigned frequency ); extern void unixtodos( time_t longtime, struct date *date, struct time *time ); extern int unlink( const char *filename ); #define disable _disable #define enable _enable #define _A_NORMAL 0x00 #define _A_RDONLY 0x01 #define _A_HIDDEN 0x02 #define _A_SYSTEM 0x04 #define _A_VOLID 0x08 #define _A_SUBDIR 0x10 #define _A_ARCH 0x20 #define FA_NORMAL _A_NORMAL #define FA_RDONLY _A_RDONLY #define FA_HIDDEN _A_HIDDEN #define FA_SYSTEM _A_SYSTEM #define FA_LABEL _A_VOLID #define FA_DIREC _A_SUBDIR #define FA_ARCH _A_ARCH #define NFDS 20 #define SEEK_SET 0 #define SEEK_CUR 1 #define SEEK_END 2 #define MK_FP( seg, ofs ) ( ( void * )\ ( ( ( unsigned long )( seg ) << 16 ) |\ ( unsigned )( ofs ) ) ) #define FP_SEG(fp) ( ( unsigned )( ( unsigned long )( fp ) >> 16 ) ) #define FP_OFF(fp) ( ( unsigned )( fp ) ) #define __peek( a,b ) ( *( ( int far * ) MK_FP ( ( a ), ( b ) ) ) ) #define __peekb( a,b ) ( *( ( char far * ) MK_FP ( ( a ), ( b ) ) ) ) #define __poke( a,b,c ) ( *( ( int far * ) MK_FP ( (a),(b)) ) = ( int )(c)) #define __pokeb( a,b,c ) ( *( ( char far * ) MK_FP ( (a),(b)) ) = ( char )(c)) #define peek( a,b ) ( *( ( int far * ) MK_FP ( ( a ), ( b ) ) ) ) #define peekb( a,b ) ( *( ( char far * ) MK_FP ( ( a ), ( b ) ) ) ) #define poke( a,b,c ) ( *( ( int far * ) MK_FP ( (a),(b)) ) = ( int )(c)) #define pokeb( a,b,c ) ( *( ( char far * ) MK_FP ( (a),(b)) ) = ( char )(c)) #ifndef __NO_INLINE_FUNCTIONS #pragma aux _enable = "sti"; #pragma aux _disable = "cli"; #pragma aux inp = "in al, dx" parm [dx] value [al] modify [ax dx]; #pragma aux inpw = "in ax, dx" parm [dx] value [ax] modify [ax dx]; #pragma aux inport = "in ax, dx" parm [dx] value [ax] modify [ax dx]; #pragma aux inportb = "in al, dx" parm [dx] value [al] modify [ax dx]; #pragma aux outp = "out dx, al" parm [dx] [al] value [al] modify [ax dx]; #pragma aux outpw = "out dx, ax" parm [dx] [ax] value [ax] modify [ax dx]; #pragma aux outport = "out dx, ax" parm [dx] [ax] modify [ax dx]; #pragma aux outportb = "out dx, al" parm [dx] [al] modify [ax dx]; #endif #endif

User Avatar

char, short (unsigned, signed), (unsigned, signed), long (unsigned, signed).

User Avatar

the value range is different, for example: signed char: -128..127 unsigned char: 0..255 signed short: -32768..32767 unsigned short: 0..65535

User Avatar

All possible values of an unsigned char are unsigned, so there is no bit that "represents a signed value." With an 8-bit byte, 1 in the most significant bit of an unsigned char represents the value 128. Consequently unsigned chars with a 1 in this position have values between 128 (when all other bits are 0) and 255 (when all other bits are 1).

User Avatar

You can't convert the data type of any variable.

User Avatar

The best way I can explain this is to give you an example of code with no documentation. If there was documentation, you would be able to tell what this code did in a matter of seconds. Without it you could waste many minutes, or even hours, trying to understand it. inline unsigned char f_1(const unsigned char *ptr, const unsigned int i) { const div_t d = div(i, 8); return (ptr[d.quot] & (1 << d.rem))?1:0; } inline void f_2(unsigned char *ptr, const unsigned int i) { const div_t d = div(i, 8); ptr += d.quot; *ptr = *ptr | (1 << d.rem); } inline void f_3(unsigned char *ptr, const unsigned int i) { const div_t d = div(i, 8); const unsigned char n = 1 << d.rem; ptr += d.quot; *ptr = ((*ptr | n ) ^ n); } void f(const unsigned int n) { const unsigned int m = n + (n%8==0?0:8-n%8); const unsigned int n = m / 8; const unsigned int m_2 = m / 2; unsigned int i, j; unsigned char *a = malloc(sizeof(unsigned char) * n); memset(a, 0xaa, n); f_3(a, 1); f_2(a, 2); for( i = 3; i < m_2; i+=2 ) { while(i < m_2 && !f_1(a, i)) i+=2; for( j = i + i; j < m; j+=i ) f_3(a, j); } free(a); }

User Avatar

signed char unsigned long int void *

User Avatar

There are far more than 4 integral types in C++. As of C++11, there were 27 integral types: bool char signed char unsigned char wchar_t char16_t char32_t short signed short unsigned short short int signed short int unsigned short int int signed int unsigned int long signed long unsigned long long int signed long int unsigned long int long long signed long long unsigned long long long long int signed long long int unsigned long long int

User Avatar

Consult your limits.h and math.h. For char it will be -128..127 or 0.255 (signed and unsigned).

User Avatar

int x;float x;char x;double x;long x;long long x;short x;unsigned int x;unsigned float x;unsigned double x;signed char x;unsigned long x;unsigned long long x;int *x;float *x;char *x;double *x;long *x;long long *x;int x[100];typedef struct rect {int left;int top;int right;int bottom;};int main(int argv, char* argc[]) {return 0;}That enough for you?Well, these are definitions, declarations are like these:extern char x;int main (int argc, char **argv);

User Avatar

unsigned find (const char* str, char c) { unsigned total; total = 0; if (str!=NULL) while (*str) { if (*str++==c) ++total; } return total; }

User Avatar

I guess it is a char or an unsigned char. When calling MS-DOS/BIOS interrupts, it represents the value of the AH register.

User Avatar

The primitive data types in C include:[signed|unsigned] char[signed|unsigned] short[signed|unsigned] int[signed|unsigned] long[signed|unsigned] long longfloatdoublelong doubleEnumerations (enum) and arrays may also be considered to be primitive types.

User Avatar

The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers are always positive or zero, and obey the laws of arithmetic modulo 2n, where n is the number of bits in the type. So, for instance, if charsare 8 bits, unsigned charvariables have values between 0 and 255, while signed charshave values between -128 and 127 (in a two's complement machine.) Whether plain charsare signed or unsigned is machine-dependent, but printable characters are always positive.

User Avatar

char: A one-byte integer with implementation-defined signedness.signed char: A signed one-byte integer.unsigned char: An unsigned one-byte integer.wchar_t: A wide character of implementation-defined size and signedness.short: A signed integer with at least 16 bits.unsigned short: An unsigned integer with at least 16 bits.int: A signed integer with at least 16 bits.unsigned int: An unsigned integer with at least 16 bits.long: A signed integer with at least 32 bits.unsigned long: An unsigned integer with at least 32 bits.long long: A signed integer with at least 64 bits.unsigned long long: An unsigned integer with at least 64 bits.float: A floating-point number with 32 bits of precision.double: A floating-point number with 64 bits of precision.long double: A floating-point number with 80-96 bits of precision.

User Avatar

/* note that neither of these functions have been tested, so there may be typos and the like *//* if you're looking to return a real value: */unsigned int complement(unsigned int value){unsigned int returnvalue = 0;while(value){returnvalue returnvalue += !(value & 1);value >>= 1;}return returnvalue;}/* if you're looking for a string representing the binary number: */char *complement(unsigned int value){int numchars = 8 * sizeof(unsigned int);int n;char *returnvalue = malloc((numchars + 1) * sizeof(char));for(n = 0; n if(value & (1 returnvalue[n] = '1';}else{returnvalue[n] = '0';}}returnvalue[numchars] = (char)NULL;return returnvalue;}

User Avatar

The range of all fundamental types is compiler-dependant. The standard specifies that a char is always 1 byte in length and that a byte is at least 8 bits in length. This is because C source code is written using ASCII character codes which require at least 8-bits per byte. It is never safe to assume that all supported architectures use an 8-bit byte. A char may be signed or unsigned depending on the compiler implementation. Some compilers will allow you to specify whether a char is signed or unsigned. As a result, a char is treated independent of the explicitly signed or unsigned char types. Thus there are three char types in total: char (often referred to as plain char), signed char and unsigned char. The standard states that all three will have the same length. As such, an unsigned char will have a range of at least 0 to 255 while a signed char will have a range of at least -127 to 127. The range of a plain char depends on whether it is signed or unsigned. Note that the lower range of a signed char is -127, not -128 as might be expected. This is because ones-complement notation has two representations for the value zero, one positive, the other negative. -128 is only possible with twos-complement notation, but not all systems use twos-complement. All other types are expressed in terms of a char, such that a short is always at least as long as a char, an int is always at least as long as a short, a long int is always at least as long as an int and a long long int is always at least as long as a long int. Use the sizeof() operator to determine the exact length (in bytes). The compiler will also provide constants in the C-standard library that define the length of a byte (in bits) as well as the upper and lower range of each type. Never make assumptions about the size or range. If you do, your code will not be portable.

User Avatar

The following are all the standard C++ data types:intunsigned intshort intunsigned short intlong intunsigned long intcharwchar_tboolfloatdoublelong double

User Avatar

The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535. They are not integral data type like int, short etc. i.e. the char data type can't hold the numeric values. The syntax of declaring a char type variable is shown as:char caps = 'c';

User Avatar

The biggest problem with factorials is that primitive data types are insufficient to store the results. For instance, factorial 21 (denoted as 21!) is 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 which is 51,090,942,171,709,440,000. But a 64-bit unsigned integer is only capable of storing numbers up to 18,446,744,073,709,551,615. Therefore any program you write using primtives will be limited to factorials in the range 1 to 20. To resolve this problem we need a more dynamic user-defined data type, one that is capable of utilising as many bytes as necessary to store a given number. The following program makes use of an user-defined bigint data type that is capable of handling extremely large integers. The implementation is incomplete but provides enough functionality to calculate factorials of any size, the only limit being available memory. The implementation has much room for improvement, particularly with regards the multiplication operator which uses a naive addition operation to gain the product. A more efficient method would utilise a more human-like multiplication method. #include<iostream> #include<vector> class bigint { private: std::vector<unsigned char>* m_vector; size_t m_used; bool m_signed; static const unsigned char get_digit(const unsigned char& digits, const size_t& index); static const size_t bits_per_digit; static const size_t digits_per_byte; static const size_t digit_mask; public: ~bigint(void){delete(m_vector);} bigint(unsigned char n=0,const bool sign=false); bigint(const bigint& copy):m_vector(new std::vector<unsigned char>(*copy.m_vector)),m_used(copy.m_used),m_signed(copy.m_signed){} bigint operator*(const bigint& rhs){bigint n(*this); return(n*=rhs);} bigint& operator*=(const bigint& rhs); bigint operator+(const bigint& rhs); bigint& operator++(void); bigint operator++(const int); bigint& operator+=(const bigint& rhs){return(operator=(*this+rhs));} const bool operator<(const bigint& rhs)const; const bool operator<=(const bigint& rhs)const{return(operator<(rhs)operator==(rhs));} bigint& operator=(bigint rhs){swap(rhs);return(*this);} bigint& operator=(unsigned char n){swap(bigint(n));return(*this);} const bool operator==(const bigint& rhs)const; const std::string as_string(void)const; const bool is_signed(void)const{return(m_signed);} const size_t length(void)const{return(m_used);} void swap(bigint& rhs)throw(){std::swap(m_vector,rhs.m_vector);std::swap(m_used,rhs.m_used);std::swap(m_signed,rhs.m_signed);} }; // Static attributes: const size_t bigint::digits_per_byte=sizeof(unsigned char)*2; // 2 digits per byte const size_t bigint::bits_per_digit=4; const size_t bigint::digit_mask=0xf; // bin:00001111 // Returns the number as a string const std::string bigint::as_string(void)const { const unsigned char zero='0'; size_t used=m_used; std::string result; if( !used ) result+=zero; else { result.resize(used,' '); for(std::vector<unsigned char>::const_iterator it=m_vector->begin(); it!=m_vector->end(); ++it) { unsigned char digits=*it; for(size_t i=0; i<digits_per_byte && used--; ++i) { unsigned char value=(unsigned char)(digits&digit_mask); _ASSERTE( value<10 ); result[used]=value+zero; digits>>=bits_per_digit; } } if(m_signed && result.front()!=zero) result.insert(0,"-"); } return( result ); } // Default constructor: bigint::bigint(unsigned char n/*=0*/,const bool sign/*=false*/):m_vector(new std::vector<unsigned char>),m_used(0),m_signed(sign) { while(n) { unsigned char digits=0; for(size_t i=0; n&&i<digits_per_byte; n/=10, ++i) { unsigned char digit=n%10; digit<<=(i*bits_per_digit); digits|=digit; ++m_used; } m_vector->push_back(digits); } } // Static function. // Returns the digit within the given digits at the given index. const unsigned char bigint::get_digit( __in const unsigned char& digits, __in const size_t& index) { _ASSERTE(index<digits_per_byte); const size_t offset=(digits_per_byte-index-1)*bits_per_digit; unsigned char mask=digit_mask; mask<<=offset; unsigned char result=digits&mask; result>>=offset; return(result); } // Multiply operator overload bigint& bigint::operator*=(const bigint& n) { bigint mult(1); bigint old(*this); while(mult<n) { operator+=(old); ++mult; } return(*this); } // Plus operator overload bigint bigint::operator+(const bigint& n) { _ASSERTE(m_vector); _ASSERTE(n.m_vector); bigint result; unsigned char carry=0; std::vector<unsigned char>::const_iterator first = m_vector->begin(); std::vector<unsigned char>::const_iterator second = n.m_vector->begin(); while( first!=m_vector->end() second!=n.m_vector->end() carry ) { unsigned char digits=0; unsigned char digits_1=first!=m_vector->end()?*first++:0; unsigned char digits_2=second!=n.m_vector->end()?*second++:0; for(size_t i=0; i<digits_per_byte; ++i ) { unsigned char digit=carry; digit+=digits_1&digit_mask; digits_1>>=bits_per_digit; digit+=digits_2&digit_mask; digits_2>>=bits_per_digit; carry=digit/10; digit%=10; digits|=(digit<<(i*bits_per_digit)); ++result.m_used; if( !carry && !digits_1 && !digits_2 && first==m_vector->end() && second==n.m_vector->end() ) break; } result.m_vector->push_back( digits ); } return( result ); } // Prefix increment operator overload bigint& bigint::operator++(void) { bigint one(1); return(operator+=(one)); } // Postfix increment operator overload bigint bigint::operator++(const int) { bigint old(*this); operator++(); return(old); } // Less than operator overload const bool bigint::operator<(const bigint& rhs)const { _ASSERTE(m_vector); _ASSERTE(rhs.m_vector); bool result=m_used<rhs.m_used; if(!result && m_used==rhs.m_used) { _ASSERTE(m_vector->size()==rhs.m_vector->size()); std::vector<unsigned char>::const_reverse_iterator it1=m_vector->rbegin(); std::vector<unsigned char>::const_reverse_iterator it2=rhs.m_vector->rbegin(); for(;it1!=m_vector->rend()&&!result;++it1,++it2) { unsigned char digits1=*it1; unsigned char digits2=*it2; if(digits1==digits2) continue; for(size_t i=0; i<digits_per_byte&&!result; ++i) { unsigned char digit1=get_digit(digits1,i); unsigned char digit2=get_digit(digits2,i); if(!(result=digit1==digit2)) result=digit1<digit2; } } } return(result); } // Equality operator overload const bool bigint::operator==(const bigint& rhs)const { _ASSERTE(m_vector); _ASSERTE(rhs.m_vector); bool result=m_used==rhs.m_used; if(result) { _ASSERTE(m_vector->size()==rhs.m_vector->size()); std::vector<unsigned char>::const_reverse_iterator it1=m_vector->rbegin(); std::vector<unsigned char>::const_reverse_iterator it2=rhs.m_vector->rbegin(); for(;it1!=m_vector->rend()&&result;++it1,++it2) { unsigned char digits1=*it1; unsigned char digits2=*it2; for(size_t i=0; i<digits_per_byte&&result; ++i) { unsigned char digit1=get_digit(digits1,i); unsigned char digit2=get_digit(digits2,i); result=digit1==digit2; } } } return(result); } // Non-member operator overload: // Output stream insertion std::ostream& operator<<(std::ostream& os,const bigint& n) { os<<n.as_string().c_str(); return( os ); } // Non-member ADL void swap(bigint& a, bigint& b) { a.swap(b); } int main() { std::cout<<"Factorials from 1 to 100\n"<<std::endl; bigint num(0); while(num++<100) { bigint factorial(1); bigint multi(1); while(multi<num) factorial*=(++multi); std::cout<<num<<"! = "<<factorial<<std::endl; } }


Copyright © 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.