in Bjarne's "The C++ Programming Language" book, the following piece of code on chars is given:
signed char sc = -140;
unsigned char uc = sc;
cout << uc // prints 't'
1Q) chars are 1byte (8 bits) in my hardware. what is the binary representation of -140
? is it possible to represent -140 using 8 bits. I would think range is guaranteed to be at least [-127...127] when signed chars is considered. how is it even possible to represent -140 in 8 bits?
2Q) assume it's possible. why do we subtract 140
from uc
when sc
is assigned to uc
? what is the logic behind this?
EDIT: I've wrote cout << sizeof (signed char)
and it's produced 1 (1 byte). I put this to be exact on the byte-wise size of signed char
.
EDIT 2: cout << int {sc }
gives the output 116
. I don't understand what happens here?
First of all: Unless you're writing something very low-level that requires bit-representation manipulation - avoid writing this kind of code like the plague. It's hard to read, easy to get wrong, confusing, and often exhibits implementation-defined/undefined behavior.
To answer your question though:
The code assumed you're on a platform in which the types signed char
and unsigned char
have 8 bits (although theoretically they could have more). And that the hardware has "two's complement" behavior: The bit representation of the result of an arithmetic operation on an integer type with N bits is always modulo 2^N. That also specifies how the same bit-pattern is interpreted as signed or unsigned. Now, -140 modulo 2^8 is 116 (01110100), so that's the bit pattern sc
will hold. Interpreted as a signed char (-128 through 127), this is still 116.
An unsigned char
can represent 116
as well, so the second assignment results in 116 as well.
116 is the ASCII code of the character t
; and std::cout
interprets unsigned char
values (under 128) as ASCII codes. So, that's what gets printed.