I need to read a specific bit from a byte. The value i want to test is 0 or 1.
unsigned char Buffer[0]=2;
//or binary 0b00000010
How can i read n bit from buffer. If it's 0 or 1? Example if 7 bit from byte is 0 or 1
I need to read a specific bit from a byte. The value i want to test is 0 or 1.
unsigned char Buffer[0]=2;
//or binary 0b00000010
How can i read n bit from buffer. If it's 0 or 1? Example if 7 bit from byte is 0 or 1
To check a bit if its 0 or 1, you can define a simple macro like:
and then use it in if-clauses.
Note the '!!' operator, to ensure that it returns 0 or 1.
To answer your question: which will check the value of 7th bit.
Similar way if you need to check the value of nth bit, replace 7 in if condition with n.
You can just use this function to pass your char and N(number of bit).The function will invoke a bitwise AND filtering out the bit you need to check.
Of course you had to define your bit position in advance.
e.g.
Bit7 (MSB)
Bit0 (LSB)
Test for bit 7:
You must define precisely how you count the bits:
0
or1
Assuming bit
0
is the least significant, you can get bit7
with this expression:Here is a generic loop: