devxlogo

Accessing Individual Bits of a Value

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.)
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist