12.8 Answers to Chapter Questions

I l @ ve RuBoard

Answer 12-1: The problem is that we have a single signed-integer-bit field. A three-bit-wide signed-integer field can take on the following values:

 struct foo {     int three_bits:3; }; 

Bit pattern

Decimal value

100

-4

110

-3

101

-2

111

-1

000

001

1

010

2

011

3

A two-bit-wide signed-integer field can take on the following values:

 struct foo {     int two_bits:3; }; 

Bit pattern

Decimal value

10

-2

11

-1

000

001

1

A one-bit-wide signed-integer field can take on the following values:

 struct foo {     int one_bit:1; }; 

Bit pattern

Decimal value

1

-1

So the two values of this bit field are 0 and -1. That means that 1 can never be stored in this field.

A unsigned bit field of width 1 can hold the values 0 and 1. Using the declaration unsigned int valid:1 makes the program work correctly.

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