Working with bitfields |
A bitfield is a normal integer value. So in Delphi, it could be a byte or a longint for example. In C++, it could be a char or a long int or an int for example.
Normally, a variable has a meaning as a whole. So a value of 6 would mean exactly
that : six.
In a bitfield, each bit of the variable has a certain meaning. Bit 1 = 1, bit
2 = 2, bit 3 = 4, bit 4 = 8 and so on.
(the rule for calculating the value of a certain
bit is the following : bitvalue = 2 ^ (bitnumber-1) )
This makes it possible to use each bit to specify a state (0 or 1, true or false).
To set a certain bit to 1, you OR the current value with the bitvalue.
To set bit 3 (bitvalue = 4) of the variable Temp for example, you would do the
following :
Delphi : Temp
:= Temp or 4;
C++ : Temp |= 4;
To reset a certain bit (set it to 0), you AND the current value with
the inverse bitvalue.
So to reset bit 3 (bitvalue = 4) of the variable Temp for example, do the following
:
Delphi : Temp := Temp and not 4;
C++ : Temp &= !4;