I have to retrieve first bit from 1 byte.
For this i stored whole 1 byte in unsigned char Array. Now i retrieved first bit as per below code:
unsigned char byteArray[100];
.....
.....//some code to store 1 byte in byte array
//now 1st bit will be:
int bit=(int) byteArray[0];
I just wanted to confirm that step involved in retrieving 1st bit is right or not? Is there any better way to retrieve single bit from unsigned char array(byte array) in C.
No, the array indexer is going to return data the size of an unsigned char, not a single bit. Use
byteArray[0] & 1;– masking the value so that you only get the first bit (depending on which end is the “first”). The other end would be& 128.