I came across a part of code which extracts third bit of variable
using
bool flag = (variable & 0x4) == 0x4;
However, the same can be achieved by bool flag = variable & 4;
. Isn’t it? Any reason that this bit extraction specification need to have additional check with == 0x4
?
>Solution :
It’s the same as far as the compiler is concerned. I didn’t even turn on optimizations and got the same assembly:
variable & 4 == 4 vs variable & 4
Thea above with with gcc. There’s some subtle differences with msvc, but it’s the same number of instructions.