• Create Account

# Checking if a bit is set in a byte

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

24 replies to this topic

### #1DarkRonin  Members   -  Reputation: 1067

Like
0Likes
Like

Posted 02 June 2014 - 06:42 PM

Hi Guys,

Just wondering if there is an easy way to check if a bit is set in a byte.

So, if I wanted to see it bit 5 was set for example?

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #2Hodgman  Moderators   -  Reputation: 49111

Like
17Likes
Like

Posted 02 June 2014 - 06:44 PM

POPULAR

bool IsBitSet( unsigned char byte, int index )
{
return (byte & mask) != 0;
}

Edited by Hodgman, 02 June 2014 - 08:33 PM.
Thanks, Chris_F

### #3Servant of the Lord  Crossbones+   -  Reputation: 33412

Like
17Likes
Like

Posted 02 June 2014 - 07:42 PM

POPULAR

When you AND two values together, every bit that wasn't 1 in *both* values is set to zero. So if you want to check if a certain bit is set, you AND that one bit to a value, and then see if the result is 0 (the bit wasn't present), or if the result is the same what you AND'd.

if( (10110 & 10000) == 10000 )

You can drop the " == mask " part because, in C++, non-zero integers automaticly convert to 'true':

if(value & mask) //If the result is 00000, then the statement evaluates to false, otherwise it evaluates to true.

Hodgeman's func does the same thing, but also uses an index to create the mask by shifting the value 00000001 over N places to become (for example) 00010000.

Edited by Servant of the Lord, 02 June 2014 - 07:45 PM.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #4DarkRonin  Members   -  Reputation: 1067

Like
0Likes
Like

Posted 02 June 2014 - 08:18 PM

Thanks guys!

I'll give this a try very shortly
Win32 Developer
One Of Them - Martial arts game that is mid development.

### #5Chris_F  Members   -  Reputation: 3018

Like
11Likes
Like

Posted 02 June 2014 - 08:31 PM

POPULAR

bool IsBitSet( unsigned char byte, int index )
{
return (byte & index) != 0;
}

I think you meant to use mask and not index in your return statement.

### #6Álvaro  Crossbones+   -  Reputation: 19925

Like
13Likes
Like

Posted 02 June 2014 - 08:34 PM

POPULAR

Alternatively:

bool is_bit_set(unsigned char byte, int index) {
return (byte >> index) & 1;
}

### #7Chris_F  Members   -  Reputation: 3018

Like
5Likes
Like

Posted 02 June 2014 - 08:53 PM

POPULAR

Here is the results I get with Clang 3.3 with -O3:

IsBitSet(unsigned char, int):                          # @IsBitSet(unsigned char, int)
btl    %esi, %edi
setb    %al
ret

is_bit_set(unsigned char, int):                       # @is_bit_set(unsigned char, int)
btl    %esi, %edi
setb    %al
ret


and with GCC 4.9 with -O3:

IsBitSet(unsigned char, int):
mov    ecx, esi
mov    eax, 1
movzx    edi, dil
sal    eax, cl
test    eax, edi
setne    al
ret
is_bit_set(unsigned char, int):
movzx    eax, dil
mov    ecx, esi
sar    eax, cl
and    eax, 1
ret


### #8DarkRonin  Members   -  Reputation: 1067

Like
0Likes
Like

Posted 02 June 2014 - 09:14 PM

When you AND two values together, every bit that wasn't 1 in *both* values is set to zero. So if you want to check if a certain bit is set, you AND that one bit to a value, and then see if the result is 0 (the bit wasn't present), or if the result is the same what you AND'd.

if( (10110 & 10000) == 10000 )

You can drop the " == mask " part because, in C++, non-zero integers automaticly convert to 'true':
if(value & mask) //If the result is 00000, then the statement evaluates to false, otherwise it evaluates to true.

Hodgeman's func does the same thing, but also uses an index to create the mask by shifting the value 00000001 over N places to become (for example) 00010000.

This actually seems to work very well. Easy to understand too
Win32 Developer
One Of Them - Martial arts game that is mid development.

### #9Pink Horror  Members   -  Reputation: 2459

Like
2Likes
Like

Posted 04 June 2014 - 08:11 AM

if( (10110 & 10000) == 10000 )

I wonder, why do some people write "== mask" instead of "!= 0"? I suppose it doesn't matter much when the variable is "mask" and/or you use a function, but I've seen this pattern with huge blocks of hard-coded if checks with duplicated giant enum names everywhere.

### #10Brother Bob  Moderators   -  Reputation: 10028

Like
9Likes
Like

Posted 04 June 2014 - 08:21 AM

POPULAR

if( (10110 & 10000) == 10000 )

I wonder, why do some people write "== mask" instead of "!= 0"? I suppose it doesn't matter much when the variable is "mask" and/or you use a function, but I've seen this pattern with huge blocks of hard-coded if checks with duplicated giant enum names everywhere.

Consider what happens if mask contains more than one bit set. Comparing the AND-result against the mask checks if all bits are set, while comparing against 0 checks if any bit is set.

In that case it is not a matter of relative style or convention, but a matter of absolute correctness; one is correct and the other one is wrong.

### #11aregee  Members   -  Reputation: 1072

Like
2Likes
Like

Posted 04 June 2014 - 02:29 PM

if( (10110 & 10000) == 10000 )

I wonder, why do some people write "== mask" instead of "!= 0"? I suppose it doesn't matter much when the variable is "mask" and/or you use a function, but I've seen this pattern with huge blocks of hard-coded if checks with duplicated giant enum names everywhere.

Consider what happens if mask contains more than one bit set. Comparing the AND-result against the mask checks if all bits are set, while comparing against 0 checks if any bit is set.

In that case it is not a matter of relative style or convention, but a matter of absolute correctness; one is correct and the other one is wrong.

To me it is also a question about clarity.  When you read the code, it is immediately clear what the intention is when you test against 'mask', rather than wondering for a few seconds why you are testing against 'not equal to zero'.

### #12L. Spiro  Crossbones+   -  Reputation: 24746

Like
9Likes
Like

Posted 04 June 2014 - 03:23 PM

POPULAR

To me it is also a question about clarity.  When you read the code, it is immediately clear what the intention is when you test against 'mask', rather than wondering for a few seconds why you are testing against 'not equal to zero'.

It’s not about clarity. Brother Bob is exactly correct. It is about correct functionality of the program.

ui32Mask = 0xF;
if ( (ui32Value & ui32Mask) != 0 ) // Are ANY of the lower 4 bits set?
if ( (ui32Value & ui32Mask) == ui32Mask ) // Are ALL of the lower 4 bits set?

There is nothing about style or clarity—they absolutely do not do the same thing (unless only 1 bit is set in ui32Mask).
It depends purely on whether you want to test for any bits or all bits, and that is essential for the correctness of any algorithm/code/program.

L. Spiro

Edited by L. Spiro, 04 June 2014 - 03:26 PM.

### #13aregee  Members   -  Reputation: 1072

Like
3Likes
Like

Posted 05 June 2014 - 12:52 PM

To me it is also a question about clarity.  When you read the code, it is immediately clear what the intention is when you test against 'mask', rather than wondering for a few seconds why you are testing against 'not equal to zero'.

It’s not about clarity. Brother Bob is exactly correct. It is about correct functionality of the program.

ui32Mask = 0xF;
if ( (ui32Value & ui32Mask) != 0 ) // Are ANY of the lower 4 bits set?
if ( (ui32Value & ui32Mask) == ui32Mask ) // Are ALL of the lower 4 bits set?

There is nothing about style or clarity—they absolutely do not do the same thing (unless only 1 bit is set in ui32Mask).
It depends purely on whether you want to test for any bits or all bits, and that is essential for the correctness of any algorithm/code/program.

L. Spiro

Where do I say that Brother Bob is wrong?  He is spot on.  I just said that for me it is ALSO about clarity.  Notice the word 'also'.  In the case where you are just testing for one bit, both cases are right, but then you should probably not call it 'mask', but rather call it 'bit'.  OP asked how to test for a bit, but the case with a mask is more generic.

Edit: if it was not clear; I meant that using 'mask' was more clear than using '!= 0'.

Edited by aregee, 05 June 2014 - 12:54 PM.

### #14Joshhua5  Members   -  Reputation: 1353

Like
0Likes
Like

Posted 05 June 2014 - 06:57 PM

I do it like this, but I'd have the enum in it's own header to be reused

enum struct BIT{
_1 = 0x1,
_2 = 0x2,
_3 = 0x4,
_4 = 0x8,
_5 = 0x10,
_6 = 0x20,
_7 = 0x40,
_8 = 0x80,
};

int eg = 20;

if(eg & BIT::_5){

}


### #15L. Spiro  Crossbones+   -  Reputation: 24746

Like
3Likes
Like

Posted 05 June 2014 - 07:27 PM

I do it like this, but I'd have the enum in it's own header to be reused

enum struct BIT{
_1 = 0x1,
_2 = 0x2,
_3 = 0x4,
_4 = 0x8,
_5 = 0x10,
_6 = 0x20,
_7 = 0x40,
_8 = 0x80,
};

int eg = 20;

if(eg & BIT::_5){

}


You’re still falling short of optimal.

enum BIT{   // “struct” does not belong.
_1 = 1 << 0,
_2 = 1 << 1,
_3 = 1 << 2,
_4 = 1 << 3,
_5 = 1 << 4,
_6 = 1 << 5,
_7 = 1 << 6,
_8 = 1 << 7,
};


L. Spiro

Edited by L. Spiro, 05 June 2014 - 07:28 PM.

### #16Ectara  Crossbones+   -  Reputation: 3097

Like
2Likes
Like

Posted 06 June 2014 - 02:07 AM

I do it like this, but I'd have the enum in it's own header to be reused

enum struct BIT{
_1 = 0x1,
_2 = 0x2,
_3 = 0x4,
_4 = 0x8,
_5 = 0x10,
_6 = 0x20,
_7 = 0x40,
_8 = 0x80,
};

int eg = 20;

if(eg & BIT::_5){

}


That forces people to be one-indexed for bit enumeration; sometimes, being zero-indexed is easier, when you use that index directly as the number of positions to shift. Or, put another way, if the index is the exponent of 2 for the value represented by the bit at that index.

### #17Joshhua5  Members   -  Reputation: 1353

Like
0Likes
Like

Posted 06 June 2014 - 08:26 AM

The strict does belong, it means you must access it with scope operator (BIT: instead of making everything global.

### #18Kian  Members   -  Reputation: 243

Like
0Likes
Like

Posted 10 June 2014 - 10:30 AM

If you want to use the scope operator (which I endorse), it would be better to do:

struct BIT
{
enum {
_1 = 1 << 0,
_2 = 1 << 1,
_3 = 1 << 2,
_4 = 1 << 3,
_5 = 1 << 4,
_6 = 1 << 5,
_7 = 1 << 6,
_8 = 1 << 7,
};
};

### #19Kian  Members   -  Reputation: 243

Like
0Likes
Like

Posted 10 June 2014 - 10:31 AM

### #20Kian  Members   -  Reputation: 243

Like
0Likes
Like

Posted 10 June 2014 - 10:39 AM

I'm not sure you can use scoped enums (enum class/enum struct) to do bitwise operations, since you can't convert them to int without an explicit cast. If you want to use it as a mask you need to stick a regular enum in a class or struct.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS