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
You must define precisely how you count the bits:
0
or 1
Assuming bit 0
is the least significant, you can get bit 7
with this expression:
int bit7 = ((unsigned char)Buffer[0] >> 7) & 1;
Here is a generic loop:
for (int i = 7; i >= 0; i--) {
putchar('0' + (((unsigned char)Buffer[0] >> i) & 1));
}