I use a byte to store some flag like 10101010
, and I would like to know how to verify that a specific bit is at 1
or 0
.

- 30,738
- 21
- 105
- 131

- 11,383
- 11
- 37
- 27
10 Answers
Here's a function that can be used to test any bit
:
bool is_bit_set(unsigned value, unsigned bitindex)
{
return (value & (1 << bitindex)) != 0;
}
Explanation:
The left shift operator <<
creates a bitmask. To illustrate:
(1 << 0)
equals00000001
(1 << 1)
equals00000010
(1 << 3)
equals00001000
So a shift of 0
tests the rightmost bit. A shift of 31
would be the leftmost bit of a 32-bit value.
The bitwise-and operator (&
) gives a result where all the bits that are 1
on both sides are set. Examples:
1111 & 0001
equals0001
1111 & 0010
equals0010
0000 & 0001
equals0000
.
So, the expression:
(value & (1 << bitindex))
will return the bitmask if the associated bit (bitindex
) contains a 1
in that position, or else it will return 0
(meaning it does not contain a 1
at the assoicated bitindex
).
To simplify, the expression tests if the result is greater than zero
.
- If
Result > 0
returnstrue
, meaning the byte has a1
in the testedbitindex
position. - All else returns
false
meaning the result was zero, which means there's a0
in testedbitindex
position.
Note the != 0
is not required in the statement since it's a bool, but I like to make it explicit.

- 8,979
- 2
- 23
- 49

- 81,409
- 55
- 245
- 302
-
How about an assert when bitindex is > 31 ? – luke Sep 24 '08 at 13:23
-
I think the current functionality, which would return false in that case, is proper... – Chris Marasti-Georg Sep 24 '08 at 13:23
-
1Why bit shift I do not understand :( – Pokus Sep 24 '08 at 13:26
-
1It's really just an example, intended to demonstrate how to test bits. In production code you might want to add some error checking, but that would only add noise here. – Kristopher Johnson Sep 24 '08 at 13:26
-
Shifting will remote what in my example? All bit to the left? – Pokus Sep 24 '08 at 13:29
-
You bit shift to move the 1 to the correct position. If bit index is 3, then you are shifting 1 left 3, so you are checking against 0x000...0100 – Chris Marasti-Georg Sep 24 '08 at 13:29
-
ok the bitindex is read from 0 to 7 from the right to the left ! I got it I think! – Pokus Sep 24 '08 at 13:31
-
Correct. Although the example where Kristopher shifts (1 << 3) should be 00001000 I believe – mdec Sep 24 '08 at 13:35
-
Regarding luke's question: This is endian safe. The left and right shift operators always consider the least-significant bit to be on the "right" and the most significant on the "left", regardless of byte order. – Kristopher Johnson Sep 24 '08 at 13:40
-
Also is* is a reserved namespace, although some would argue it's pedantic. – James Antill Sep 24 '08 at 16:11
As an extension of Patrick Desjardins' answer:
When doing bit-manipulation it really helps to have a very solid knowledge of bitwise operators.
Also the bitwise "AND" operator in C is &
, so you want to do this:
unsigned char a = 0xAA; // 10101010 in hex
unsigned char b = (1 << bitpos); // Where bitpos is the position you want to check
if(a & b) {
//bit set
}
else {
//not set
}
Above I used the bitwise "AND" (& in C) to check whether a particular bit was set or not. I also used two different ways of formulating binary numbers. I highly recommend you check out the Wikipedia link above.

- 30,738
- 21
- 105
- 131

- 5,122
- 4
- 25
- 26
-
There is no reason for you to have to store the bitshift in a certain variable, I was using it as an example of a bitwise operator. However if you dont bitshift you will have to use an explicit value somewhere. The bitshift I used was an easy way to do 00000100 where bitpos is 2 in this example – mdec Sep 24 '08 at 13:28
You can use an AND operator. The example you have: 10101010 and you want to check the third bit you can do: (10101010 AND 00100000) and if you get 00100000 you know that you have the flag at the third position to 1.

- 30,738
- 21
- 105
- 131

- 136,852
- 88
- 292
- 341
-
C doesn't have an "AND" operator. Furthermore, you need to use a bitwise AND, not a logical one. – Michael Carman Sep 24 '08 at 13:19
Kristopher Johnson's answer is very good if you like working with individual fields like this. I prefer to make the code easier to read by using bit fields in C.
For example:
struct fieldsample
{
unsigned short field1 : 1;
unsigned short field2 : 1;
unsigned short field3 : 1;
unsigned short field4 : 1;
}
Here you have a simple struct with four fields, each 1 bit in size. Then you can write your code using simple structure access.
void codesample()
{
//Declare the struct on the stack.
fieldsample fields;
//Initialize values.
fields.f1 = 1;
fields.f2 = 0;
fields.f3 = 0;
fields.f4 = 1;
...
//Check the value of a field.
if(fields.f1 == 1) {}
...
}
You get the same small size advantage, plus readable code because you can give your fields meaningful names inside the structure.

- 30,738
- 21
- 105
- 131

- 392
- 2
- 16
-
2Note that a problem with using bit fields is that the way they are laid out in memory is implementation-dependent, so it might be difficult to use them with data that you exchange with other programs. – Kristopher Johnson Jan 19 '09 at 16:05
If you are using C++ and the standard library is allowed, I'd suggest storing your flags in a bitset:
#include <bitset>
//...
std::bitset<8> flags(someVariable);
as then you can check and set flags using the [] indexing operator.

- 25,101
- 4
- 35
- 56
Nobody's been wrong so far, but to give a method to check an arbitrary bit:
int checkBit( byte in, int bit )
{
return in & ( 1 << bit );
}
If the function returns non-zero, the bit is set.

- 23,839
- 7
- 70
- 61
byte THIRDBIT = 4; // 4 = 00000100 i.e third bit is set
int isThirdBitSet(byte in) {
return in & THIRDBIT; // Returns 1 if the third bit is set, 0 otherwise
}

- 2,180
- 1
- 15
- 22
You can do as Patrick Desjardins says and you make a bit-to-bit OR to the resulting of the previous AND operation.
In this case, you will have a final result of 1 or 0.

- 30,738
- 21
- 105
- 131

- 2,313
- 4
- 21
- 24
Traditionally, to check if the lowest bit is set, this will look something like:
int MY_FLAG = 0x0001;
if ((value & MY_FLAG) == MY_FLAG)
doSomething();

- 601
- 1
- 5
- 9
Use a bitwise (not logical!) AND to compare the value against a bitmask.
if (var & 0x08) {
/* The fourth bit is set */
}

- 30,628
- 10
- 74
- 122