I’d like to check if a binary number has a ‘0’ or a ‘1’ at a specific position.
example:
if the binary number is: 101000100
- checking at position zero (that is at
the rightmost ‘0’) should result in
‘0’. - checking at position 2 should result
in ‘1’. - checking at position 3 should result
in ‘0’. -
checking at position 6 should result
in ‘1’.etc…
I’m coding in C, so obviously I could use sprintf / scanf and the likes, but I guess there must be something better (read: more time efficient / easier)!
What would be a good mechanism to do this?
This will filter out the bit you’re looking for:
If you really need a 1 or 0 response, you can use this to make it a boolean value:
Or even better (thanks Vadim K.):