17.13 Answers to Chapter Questions

I l @ ve RuBoard

Answer 17-1: The problem is that memset is a character-fill routine. An integer consists of 2 or 4 bytes ( characters ). Each byte is assigned the value 1. So a 2-byte integer will receive the value:

 integer = 0x0101; 

The 1-byte hex value for -1 is 0xFF. The 2-byte hex value of -1 is 0xFFFF. So we can take two single-byte -1 values, put them together, and come out with -1. This works for zero also. Any other number will produce the wrong answer. For example, 1 is 0x01. Two bytes of this is 0x0101, or 257.

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