Is there a way to obtain a particular bit in a hexadecimal number to validate?
e.g: 0x11 and I want to know if index 2 is 1. If so, I will do an action.
I look with bit operations, but I didn't find anything about a particular bit.
Based on the 0x11
, I'm going to guess that you're working with C or something sufficiently similar to use the same bitwise operators.
In this case, you start by building a bitmask that has the bit you care about set. To get a mask with that bit set, you normally take 1
(which has only its least significant bit set) and shift it left the appropriate number of places to put that single bit in the position you care about.
Then you do a bitwise and
between the mask and the input.
Finally, you check whether the result of that and
was zero or not.
int position = 2;
int bitmask = 1 << position;
if (input & mask)
// that bit was a 1
else
// that bit was a 0