Accessing Individual Bits of a Value

Ever needed to access individual bits of a value? It can be done with bitshifting and a sequence of logical &s quickly and easily. When finished, you will have a series of bit values that are only one bit in length.

//bit accesstest - using char as the source typestruct ByteBits{  unsigned bit0 : 1;  unsigned bit1 : 1;  unsigned bit2 : 1;  unsigned bit3 : 1;  unsigned bit4 : 1;  unsigned bit5 : 1;  unsigned bit6 : 1;  unsigned bit7 : 1;};int main(){  ByteBits bittest;  char value=170; //10101010 l-to-r in binary  memcpy(&bittest,&value,1);  //bittest.bit0->7 will be  //0,1,0,1,0,1,0,1 respectively (the bits will be reversed  //as that's how they are ordered in memory.)
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: