Checking if a bit is set in a byte

Started by
23 comments, last by Trienco 9 years, 10 months ago

if( (10110 & 10000) == 10000 )
if( (value & mask) == mask )


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'.

Advertisement

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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'.

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){

}

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.

The strict does belong, it means you must access it with scope operator (BIT::) instead of making everything global.
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,
    };
};
Huh, misread the first bit. Also, I can't edit my answer?
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.

This topic is closed to new replies.

Advertisement