1

I'm trying to know the states of different bits over a char

Let's say I've:

char a = 11 //00001011 in binary

How can I retrieve bit number 5 which is currently 0 and cast it to a bool variable? And how can I set it?

2
  • In programming I would normally expect you to count bits starting with the rightmost bit being bit zero, so that the nth bit has value 2ⁿ, although you accepted an answer that used 1-based indexing... Commented Oct 18, 2012 at 14:35
  • @Neil - A good point to be sure. I'm used to talking with individuals who think of bit positions as 1-8 instead of the 0-7 as they're usually noted in reference manuals. I made that assumption here as well, so I did clarify that. Commented Oct 18, 2012 at 14:51

7 Answers 7

6

You can use some bitwise operators:

bool fifthBitSet = a & 0x10;

Or, more generally:

bool nThBitSet = a & ( 0x1 << n );

Missed the setting part:

a |= 0x1 << n; // set n'th bit
Sign up to request clarification or add additional context in comments.

2 Comments

He said the fifth bit is zero, so it's definitely not 0x8 however you count it.
@Neil my bad, I thought he meant from the left.
3

As you've noted:

char a = 11 // == 00001011 in binary 

Since you said the bit number 5 is clear right now, let's *define the position of the bits as you're talking about them:

pos       8  7  6  5  4  3  2  1
value    [0][0][0][0][1][0][1][1]

You want to test the 5th bit, that means you need a mask that accesses the fifth bit, here's some masks:

your number: 00001011
0x1  mask:   00000001
0x2  mask:   00000010
0x4  mask:   00000100
0x8  mask:   00001000
0x10 mask:   00010000  <-- So that's the one we want, the 5th bit

To set your bit you need a bit-wise OR (|)

a |= 0x10; //   00001011
           // | 00010000  because 1 | anything = 1
           // ----------
           //   00011011  The value is set! 

To test the bit we can use the bit-wise AND (&)

bool something = a & 0x10; //   00001011
                           // & 00010000  because only 1 & 1 = 1
                           // ----------
                           //   00000000  something will be 0 (false) 

*it was pointed out that this is a 1-based indexing as opposed to the more typical 0-based indexing. Which is true. Based on the description I chose to read the question as "bit number 5" meaning typical (human) based counting of 1, 2, 3, 4, etc out of 8-bits.

If you'd rather use a 0-based indexing, this is no problem, just "shift" the same logic by one, the mask for the "5th" bit in 0-based indexing is 0x20

When talking about bits it's good to note which is the least significant bit, and 0 or 1 based to be totally clear.

Comments

2

Counting from 0 being the rightmost bit:

To test it:

int b = 5;

bool n = a & (1 << b);

To set (or clear) bit b:

if (n) {  // set
    a |= (1 << b);     // RHS only has bit 'b' set
} else {  // clear
    a &= ~(1 << b);    // RHS has every bit _except_ bit 'b' set
}

Comments

2

You mention bit number 5, I believe you're looking at the 5th least-significant bit.

You've also asked about how to set a bit at a specific position. For that, use bitwise OR - |:

a = a | 0x10;

As has already been answered, test the bit using bitwise AND - &:

bool isSet = a & 0x10; 

Often you'll use a positional flag like 0x10, but the generalizations given using bit-shifts are very useful, especially if you wrap these into a function:

int pos = 5;

// set bit 5:
a = a | (0x1 << pos);

// test bit 5:
bool isSet = a & (0x1 << pos);

For more information, the wikipedia article on bitwise operation is pretty good.

Comments

2

You can test the bits with bitmasks:

a & 0b00010000

Comments

1

Well, you can use this set of functions to get and set the bit states, I suppose:

bool getBit(int data, int bitNumber) {
  int flag = 1 << bitNumber - 1;
  return (bool)(data & flag);
}

void setBit(int& data, int bitNumber) {
  int flag = 1 << bitNumber - 1;
  data |= flag;  
}

...
int main() {
  int a = 11;
  cout << getBit(a, 5); // false
  setBit(a, 5);
  cout << getBit(a, 5); // true
}

Still I wonder why do you need to use this, and not bitsets. )

Comments

0

You can use bitwise AND to know the states.

unsigned int number = 11;

AND the number with the value in which the bit position you want to check alone is set and the remaining bits to 0.

Suppose I want to check bit 0, then

if(a&0x00000001)
   puts(" 01h bit position   is 1")
else
   puts(" 01h bit position   is 0")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.