C/C++ Why to use unsigned char for binary data?

后端 未结 8 1073
-上瘾入骨i
-上瘾入骨i 2020-12-07 10:27

Is it really necessary to use unsigned char to hold binary data as in some libraries which work on character encoding or binary buffers? To make sense of my que

8条回答
  •  一向
    一向 (楼主)
    2020-12-07 11:12

    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 least significant byte.

    The two other character types generally don't work the same. signed char is signed, anyhow, so conversion of values that don't fit it is not well defined. char is not fixed to be signed or unsigned, but on a particular platform to which your code is ported it might be signed even it is unsigned on yours.

提交回复
热议问题