user1909612
user1909612

Reputation: 273

How do i flip the value of a single bit?

I' ll be honest, this is an homework i just can' t get done. The task is to handle 8 different checkboxes by using a single byte. The constructor of the form which contains the checkboxes takes a byte as input and sets the checkboxes to "checked" based on the input. The flag byte is then stored somewhere and after that each time one checkbox has his status changed (from checked to unchecked and from unchecked to checked) i must change the flag byte accordingly. So far i was only able to do the first part:

public Form1(byte flags)
{
    InitializeComponent();
    flags2 = flags;
    if ((flags & 0x01) >> 0 != 0) checkBox1.Checked = true; 
    if ((flags & 0x02) >> 1 != 0) checkBox2.Checked = true; 
    if ((flags & 0x04) >> 2 != 0) checkBox3.Checked = true; 
    if ((flags & 0x08) >> 3 != 0) checkBox4.Checked = true; 
    if ((flags & 0x10) >> 4 != 0) checkBox4.Checked = true;
    if ((flags & 0x20) >> 5 != 0) checkBox6.Checked = true; 
    if ((flags & 0x40) >> 6 != 0) checkBox7.Checked = true;
    if ((flags & 0x80) >> 7 != 0) checkBox8.Checked = true; 
}    

But i really don' t know how to do a not on just 1 bit. I thought about using additions but then the rest would change the bits to the left. I can only use this trick on the most significative bit where a rest doesn' t have consequences.

private void checkBox8_CheckedChanged(object sender, EventArgs e)
{
    flags2 += 0x80;
}       

For the other bits i could really use some help.

Upvotes: 1

Views: 621

Answers (4)

Michael Gunter
Michael Gunter

Reputation: 12811

To toggle bits, use the XOR operator (^):

flags ^= <bits>;   -or-  flags2 = flags ^ <bits>;   -e.g.-  flags ^= 0x04;

To set bits, use the OR operator (|):

flags |= <bits>;   -or-  flags2 = flags | <bits>;   -e.g.-  flags |= 0x08;

To clear bits, use the AND operator (&) and the NOT operator (~)

flags &= ~<bits>;  -or-  flags2 = flags & ~<bits>;  -e.g.-  flags &= ~0x08;

Upvotes: 3

Steve
Steve

Reputation: 216353

Supposing that the variable where you have stored the initial status is flags2 then

private void CheckBox1_CheckedChanged(Object sender, EventArgs e) 
{
   if(checkBox1.Checked)
       flags2 |= 1;
   else
       flags2 &= ~0xFE; 

   // ~0x2=0xFD, ~0x4=0xFB, ~0x08=0xF7, 
   // ~0x10=0xF5, ~0x20=0xEB, ~0x40=0xD7, ~0x80=0xAF
}

Also note that your initial test could be rewritten simply as

if ((flags & 0x04) != 0) checkBox1.Checked = true; 

Upvotes: 1

Michael Gunter
Michael Gunter

Reputation: 12811

Also, you can simplify your initialization code:

checkBox1.Checked = ((flags & 0x01) != 0);
checkBox2.Checked = ((flags & 0x02) != 0);
checkBox3.Checked = ((flags & 0x04) != 0);
checkBox4.Checked = ((flags & 0x08) != 0);
checkBox5.Checked = ((flags & 0x10) != 0);
checkBox6.Checked = ((flags & 0x20) != 0);
checkBox7.Checked = ((flags & 0x40) != 0);
checkBox8.Checked = ((flags & 0x80) != 0);

Upvotes: 0

joe
joe

Reputation: 8654

You have to invert the bit mask:

flags &= ~0x10

will set bit 4 (0x10) to 0.
The ~ operator inverts all bits (8 bit example):

 0x10 (0001000)
~0x10 (1110111)

To toggle a bit use this (xor):

flags ^= 0x10

Using additions never works if you want to operate with bits.
For practice, I recommend to write down all numbers as bit masks: 0x10 = 0001000 (base 2)

Upvotes: 1

Related Questions