|
|
|
Chapter 20Answers to Selected Exercises
2. [was #2] To toggle a bit in a variable i = i ^ 0x0010; or, more concisely, i ^= 0x0010;
4. [was #4] #define MK_COLOR(r,g,b) ((long) (b) << 16 | (g) << 8 | (r)) 6. [was #6; modified] (a) #include <stdio.h> unsigned short swap_bytes(unsigned short i); int main(void) { unsigned short i; printf("Enter a hexadecimal number (up to four digits): "); scanf("%hx", &i); printf("Number with bytes swapped: %hx\n", swap_bytes(i)); return 0; } unsigned short swap_bytes(unsigned short i) { unsigned short high_byte = i << 8; unsigned short low_byte = i >> 8; return high_byte | low_byte; } (b) unsigned short swap_bytes(unsigned short i) { return i << 8 | i >> 8; } 8. [was #8]
(a) The value of
(b) It returns a bit-field within 14. [was #9] struct IEEE_float { unsigned int fraction: 23; /* members may need to be reversed */ unsigned int exponent: 8; unsigned int sign: 1; }; Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |