11.10 Answers to Chapter Questions

I l @ ve RuBoard

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 0000 2 . 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 0000 2 >> 1 is 1100 0000 2 .

The variable i works even though it is signed because it is 16 bits long. So 0x80 in 16 bits is 0000 0000 1000 0000 2 . 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.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net