I l@ve RuBoard Previous Section Next Section

12.8 Answers to Chapter Questions

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

0

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

0

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

0

0

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 Previous Section Next Section