11.10 Answers to Chapter Questions
Answer 11-2: DIRECT_CONNECT is defined to be bit number 8 by
the expression (1 << 8); however, the eight
bits in a character variable are numbered 76543210. There is no bit
number 8. A solution to this problem is to make
flags a short integer with 16 bits.
Exercise 11-3: The problem is that ch is a character (8 bits).
The value 0x80 represented in 8 bits is 1000
00002. The first bit, the sign bit, is set.
When a right shift is done on this variable, the sign bit is used for
fill, so 1000 00002 >> 1 is 1100
00002.
The variable i works even though it is signed
because it is 16 bits long. So 0x80 in 16 bits is 0000 0000 1000
00002. Notice that the bit
we've got set is nowhere near the sign bit.
The solution to the problem is to declare ch as an
unsigned variable.
|