Mega Search
23.2 Million


Sign Up

Make a donation  
bit setting problem  
News Group: borland.public.cppbuilder.language.cpp

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 



Vote for best question.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 12:02 AM EST
From: mataus
 
Re: bit setting problem  
News Group: borland.public.cppbuilder.language.cpp
Remy Lebeau (TeamB) wrote:

>Alternatively, when checking whether multiple bits are all set or all not 
>set, you can combine them when using the '&' operator:
>
>    if( !(iFlags & (SECOND_BIT | 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
>    {
>        Label1->Caption = "2 and 3 set";
>    }

 - or -

	if( iFlags & SECOND_BIT )
	{
		if( iFlags & THIRD_BIT )
		{
			Label1->Caption = "2 and 3 set";
		}else{
			Label1->Caption = "2 set, 3 not set";
		};
	}else{
		if( iFlags & THIRD_BIT )
		{
			Label1->Caption = "2 not set, 3 set";
		}else{
			Label1->Caption = "2 and 3 not set";
		};
	};

 - or -

	if( CheckBox2->Checked )
	{
		if( CheckBox3->Checked )
		{
			Label1->Caption = "2 and 3 set";
		}else{
			Label1->Caption = "2 set, 3 not set";
		};
	}else{
		if( CheckBox3->Checked )
		{
			Label1->Caption = "2 not set, 3 set";
		}else{
			Label1->Caption = "2 and 3 not set";
		};
	};



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 8:49 AM EST
From: Bob Gonder
 
Re: bit setting problem  
News Group: borland.public.cppbuilder.language.cpp
"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);

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 10:54 AM EST
From: Chris Uzdavinis (TeamB)
 
Re: bit setting problem  
News Group: borland.public.cppbuilder.language.cpp
"mataus"  wrote in message 
news:47855e91@newsgroups.borland.com...

> but code does not work if i test for two bits:

Your use of the '!' operator is throwing off the comparisons, as you are 
inverting the bits of iFlags before the '&' operator is invoked on it.  Try 
moving the '!' operator around, ie:

    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";
    }

Alternatively, don't use the '!' operator at all:

    if( ((iFlags & SECOND_BIT) == 0) && ((iFlags & THIRD_BIT) == 0) )
    {
        Label1->Caption = "2 and 3 not set";
    }
    else if( ((iFlags & SECOND_BIT) == 0) && (iFlags & THIRD_BIT) )
    {
        Label1->Caption = "2 not set, 3 set";
    }
    else if( (iFlags & SECOND_BIT) && ((iFlags & THIRD_BIT) == 0) )
    {
        Label1->Caption = "2 set, 3 not set";
    }
    else /*if( (iFlags & SECOND_BIT) && (iFlags & THIRD_BIT) )*/
    {
        Label1->Caption = "2 and 3 set";
    }

Alternatively, when checking whether multiple bits are all set or all not 
set, you can combine them when using the '&' operator:

    if( !(iFlags & (SECOND_BIT | 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 | THIRD_BIT) == (SECOND_BIT | 
THIRD_BIT) )*/
    {
        Label1->Caption = "2 and 3 set";
    }

Or:

    if( (iFlags & (SECOND_BIT | THIRD_BIT)) == 0 )
    {
        Label1->Caption = "2 and 3 not set";
    }
    else if( ((iFlags & SECOND_BIT) == 0) && (iFlags & THIRD_BIT) )
    {
        Label1->Caption = "2 not set, 3 set";
    }
    else if( (iFlags & SECOND_BIT) && ((iFlags & THIRD_BIT) == 0) )
    {
        Label1->Caption = "2 set, 3 not set";
    }
    else /*if( iFlags & (SECOND_BIT | THIRD_BIT) == (SECOND_BIT | 
THIRD_BIT) )*/
    {
        Label1->Caption = "2 and 3 set";
    }


Gambit 



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 4:25 PM EST
From: Remy Lebeau \(TeamB\)