Bit-Level Operations
     

Examples

 

Consider the following c program:

 

bits.c:

#include <stdio.h>

main()

{

char B=0;

int i;

 

printf("%d\n\n", B);

B=B | (1<<4) ; /*set 4th bit*/

printf("%d\n\n", B);

 

/* print all bits */

for (i=7; i>=0; i--)

        printf("%d ", (B&(1<<i))>>i) ;

printf("\n\n");

 

B=B & (~(1<<4)) ; /*clear 4th bit*/

printf("%d\n\n", B);

}

% gcc –g –o bits bits.c

% bits

0

 

16

 

0 0 0 1 0 0 0 0

 

0