How to check bits in a byte.
-
Hi all, i was wondering how do i check for individual bits in a byte. Like if i wanted to check if the 3rd bit in a byte was 1 or 0, how do i do that in c++?
-
Hi all, i was wondering how do i check for individual bits in a byte. Like if i wanted to check if the 3rd bit in a byte was 1 or 0, how do i do that in c++?
You can use binary operators (such as
and
) with proper operands. for example:bool checkbit2(unsigned char b)
{
if ( b & 0x04) return true else return false;
}returns
true
if third bit (which is bit 2) ofb
is set (note the operand0x04
, that is binary00000100
). hope that helps.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi all, i was wondering how do i check for individual bits in a byte. Like if i wanted to check if the 3rd bit in a byte was 1 or 0, how do i do that in c++?