hello all,
i have a problem with bits. I want to use a integer to store and test some
flags. I have code like this
#define NOTHING 0X00
#define FIRST_BIT 0x01
#define SECOND_BIT 0x02
#define THIRD_BIT 0x04
#define FORTH_BIT 0x08
short int iFlags = NOTHING;
if (CheckBox1->Checked)
iFlags |= FIRST_BIT;
else
iFlags &= ~FIRST_BIT;
if (CheckBox2->Checked)
iFlags |= SECOND_BIT;
else
iFlags &= ~SECOND_BIT;
// Set Child Key status Bit
if (CheckBox3->Checked)
iFlags |= THIRD_BIT;
else
iFlags &= ~THIRD_BIT;
if (CheckBox4->Checked)
iFlags |= FORTH_BIT;
else
iFlags &= ~FORTH_BIT;
This code looks to work. Now i can test single bit using:
if (iFlags & SECOND_BIT) // test to see if second bit is set
but code does not work if i test for two bits:
if ((!iFlags & SECOND_BIT) && (!iFlags & THIRD_BIT))
{
Label1->Caption = "2 and 3 not set";
}
else if ((!iFlags & SECOND_BIT) && (iFlags & THIRD_BIT))
{
Label1->Caption = "2 not set, 3 set";
}
else if ((iFlags & SECOND_BIT) && (!iFlags & THIRD_BIT))
{
Label1->Caption = "2 set, 3 not set";
}
else if ((iFlags & SECOND_BIT) && (iFlags & THIRD_BIT))
{
Label1->Caption = "2 and 3 set";
}
Can anyone show me what is wrong with this or how i fix?
thank you - mataus
"mataus" writes:
> hello all,
>
> i have a problem with bits. I want to use a integer to store and test some
> flags. I have code like this
>
> #define NOTHING 0X00
> #define FIRST_BIT 0x01
> #define SECOND_BIT 0x02
> #define THIRD_BIT 0x04
> #define FORTH_BIT 0x08
Ok, your bits are all mutually exclusive, which is good. (I've seen
people define them 1,2,3,4... and are surprised at the results!)
But to avoid confusion in some programmers, and to avoid accidental
error with the value that comes after 0x8, I prefer this syntax:
enum Bits {
NOTHING = 0,
B_FIRST = 1 << 0,
B_SECOND = 1 << 1,
B_THIRD = 1 << 2,
B_FOURTH = 1 << 3,
// etc
};
People don't seem to have problems adding the next bit(s), but I
frequently see the one after 0x8 being 0x16, which is wrong. :)
Anyway, I digress...
> short int iFlags = NOTHING;
Ok... bits are all 0.
> if (CheckBox1->Checked)
> iFlags |= FIRST_BIT;
> else
> iFlags &= ~FIRST_BIT;
No need for the else case, since you have just guaranteed that the
bits are initially 0. Same with all following cases.
> if (CheckBox2->Checked)
> iFlags |= SECOND_BIT;
> else
> iFlags &= ~SECOND_BIT;
>
> // Set Child Key status Bit
> if (CheckBox3->Checked)
> iFlags |= THIRD_BIT;
> else
> iFlags &= ~THIRD_BIT;
>
> if (CheckBox4->Checked)
> iFlags |= FORTH_BIT;
> else
> iFlags &= ~FORTH_BIT;
Thus:
if (CheckBox1->Checked) iFlags |= B_FIRST;
if (CheckBox2->Checked) iFlags |= B_SECOND;
if (CheckBox3->Checked) iFlags |= B_THIRD;
if (CheckBox4->Checked) iFlags |= B_FOURTH;
> but code does not work if i test for two bits:
>
> if ((!iFlags & SECOND_BIT) && (!iFlags & THIRD_BIT))
operator! is an inappropriate operator for bit logic. It is the
logical negate, not the bitwise negate, and its precedence is too
high, such that you are converting iFlags to "true" or "false" and
then ANDing the bits (1 or 0) with your bit flags.
--
Chris (TeamB);