What is an unsigned char?

2019-01-01 04:32发布

In C/C++, what an unsigned char is used for? How is it different from a regular char?

标签: c++ c char
16条回答
高级女魔头
2楼-- · 2019-01-01 05:07

Because i feel it's really called for, i just want to state some rules of C and C++ (they are the same in this regard). First, all bits of unsigned char participate in determining the value if any unsigned char object. Second, unsigned char is explicitly stated unsigned.

Now, i had a discussion with someone about what happens when you convert the value -1 of type int to unsigned char. He refused the idea that the resulting unsigned char has all its bits set to 1, because he was worried about sign representation. But he don't have to. It's immediately following out of this rule that the conversion does what is intended:

If the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (6.3.1.3p2 in a C99 draft)

That's a mathematical description. C++ describes it in terms of modulo calculus, which yields to the same rule. Anyway, what is not guaranteed is that all bits in the integer -1 are one before the conversion. So, what do we have so we can claim that the resulting unsigned char has all its CHAR_BIT bits turned to 1?

  1. All bits participate in determining its value - that is, no padding bits occur in the object.
  2. Adding only one time UCHAR_MAX+1 to -1 will yield a value in range, namely UCHAR_MAX

That's enough, actually! So whenever you want to have an unsigned char having all its bits one, you do

unsigned char c = (unsigned char)-1;

It also follows that a conversion is not just truncating higher order bits. The fortunate event for two's complement is that it is just a truncation there, but the same isn't necessarily true for other sign representations.

查看更多
余生请多指教
3楼-- · 2019-01-01 05:09

unsigned char is the heart of all bit trickery. In almost ALL compiler for ALL platform an unsigned char is simply a BYTE. An unsigned integer of (usually) 8 bits. that can be treated as a small integer or a pack of bits.

In addiction, as someone else has said, the standard doesn't define the sign of a char. so you have 3 distinct "char" types: char, signed char, unsigned char.

查看更多
何处买醉
4楼-- · 2019-01-01 05:10

unsigned char takes only positive values....like 0 to 255

where as

signed char takes both positive and negative values....like -128 to +127

查看更多
有味是清欢
5楼-- · 2019-01-01 05:10

quoted frome "the c programming laugage" book:

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 2^n, where n is the number of bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values between 0 and 255, while signed chars have values between -128 and 127 (in a two' s complement machine.) Whether plain chars are signed or unsigned is machine-dependent, but printable characters are always positive.

查看更多
登录 后发表回答