Posts

Showing posts with the label Bytebuffer

C/C++ Why To Use Unsigned Char For Binary Data?

Answer : In C the unsigned char data type is the only data type that has all the following three properties simultaneously it has no padding bits, that it where all storage bits contribute to the value of the data no bitwise operation starting from a value of that type, when converted back into that type, can produce overflow, trap representations or undefined behavior it may alias other data types without violating the "aliasing rules", that is that access to the same data through a pointer that is typed differently will be guaranteed to see all modifications if these are the properties of a "binary" data type you are looking for, you definitively should use unsigned char . For the second property we need a type that is unsigned . For these all conversion are defined with modulo arihmetic, here modulo UCHAR_MAX+1 , 256 in most 99% of the architectures. All conversion of wider values to unsigned char thereby just corresponds to truncation to the leas...