20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Bit Fields<br />

295<br />

The structure packed_struct is def<strong>in</strong>ed to conta<strong>in</strong> six members.The first member is<br />

not named.The :3 specifies three unnamed bits.The second member, called f1, is also an<br />

unsigned <strong>in</strong>t.The :1 that immediately follows the member name specifies that this<br />

member is to be stored <strong>in</strong> one bit.The flags f2 and f3 are similarly def<strong>in</strong>ed as be<strong>in</strong>g a<br />

s<strong>in</strong>gle bit <strong>in</strong> length.The member type is def<strong>in</strong>ed to occupy eight bits, whereas the member<br />

<strong>in</strong>dex is def<strong>in</strong>ed as be<strong>in</strong>g 18 bits long.<br />

The C compiler automatically packs the preced<strong>in</strong>g bit field def<strong>in</strong>itions together.The<br />

nice th<strong>in</strong>g about this approach is that the fields of a variable def<strong>in</strong>ed to be of type<br />

packed_struct can now be referenced <strong>in</strong> the same convenient way normal structure<br />

members are referenced. So, if you declare a variable called packed_data as follows:<br />

struct packed_struct packed_data;<br />

you could easily set the type field of packed_data to 7 with the simple statement<br />

packed_data.type = 7;<br />

or you could set this field to the value of n with the similar statement<br />

packed_data.type = n;<br />

In this last case, you need not worry about whether the value of n is too large to fit <strong>in</strong>to<br />

the type field; only the low-order eight bits of n will be assigned to packed_data.type.<br />

Extraction of the value from a bit field is also automatically handled, so the statement<br />

n = packed_data.type;<br />

extracts the type field from packed_data (automatically shift<strong>in</strong>g it <strong>in</strong>to the low-order<br />

bits as required) and assigns it to n.<br />

Bit fields can be used <strong>in</strong> normal expressions and are automatically converted to <strong>in</strong>tegers.<br />

So the statement<br />

i = packed_data.<strong>in</strong>dex / 5 + 1;<br />

is perfectly valid, as is<br />

if ( packed_data.f2 )<br />

...<br />

which tests if flag f2 is true or false. One th<strong>in</strong>g worth not<strong>in</strong>g about bit fields is that there<br />

is no guarantee whether the fields are <strong>in</strong>ternally assigned from left to right or from right<br />

to left.This should not present a problem unless you are deal<strong>in</strong>g with data that was created<br />

by a different program or by a different mach<strong>in</strong>e. In such cases, you must know how<br />

the bit fields are assigned and make the declarations appropriately.You could have<br />

def<strong>in</strong>ed the structure packed_struct as<br />

struct packed_struct<br />

{<br />

unsigned <strong>in</strong>t <strong>in</strong>dex:18;<br />

unsigned <strong>in</strong>t type:8;<br />

unsigned <strong>in</strong>t f3:1;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!