### Bit manipulation functions: clear a bit, set a bit, flip a bit and test a bit

• Setting a certain bit in a bit pattern

• Recall how to Set a certain bit in variable:

 ``` a = 1010...x...1101 0000..010..0000 <------------- Or this bit pattern OR --------------- 1010...1...1101 ```

We can best use the base 2, 8 or 16 number notation to denote bit patterns !!!

• Example: set the 2nd bit and the 4th bit in a bit pattern

 ``` int main( int argc, char* argv[] ) { char a = 1; /* 1 = 00000001 */ a = a | 0b00000100; /* 00000100 - set 2nd bit */ a = a | 0b00010000; /* 00010000 - set 4th bit */ printf( "a = %d\n", a ); } ```

• Clearing a certain bit in a bit pattern

• Clear a certain bit in a bit pattern

• Suppose the variable a contains a bit pattern:

 ``` a = 1010...x...1101 ```

• Problem:

 Clear the bit x to the value 1 without changing all other bits in the pattern.

• We can clear the bit x inside the bit pattern to 0 using the AND operation with the following specific pattern:

 ``` a = 1010...x...1101 1111..101..1111 AND --------------- 1010...0...1101 ```

Note:

• The bit pattern

 ``` 1111...101...1111 ```

can be obtained using:

 ``` NOT ( 0000...010...0000 ) ```

• Example: clear the 4th bit and the 2nd bit in a bit pattern

 ``` int main( int argc, char* argv[] ) { char a = 31; /* a = 00011111 (= 31) */ a = a & (~0b00010000); /* Now: a = 00001111 (= 15) */ printf( "a = %d\n", a ); a = a & (~0b0000100); /* Now: a = 00001011 (= 11) */ printf( "a = %d\n", a ); } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc bit-op3.c To run:          ./a.out

• Flip a certain bit in a bit pattern

• Flipping a bit:

 Flipping a bit = changing a 0 bit to 1 or changing a 1 bit to 0

• Flip a certain bit in a bit pattern

• Suppose the variable a contains a bit pattern:

 ``` a = 1010...x...1101 ```

• Problem:

 Flip the bit x without changing all other bits in the pattern.

• We can flip the bit x inside the bit pattern using the XOR operation with the following specific pattern:

 ``` a = 1010...x...1101 0000..010..0000 XOR --------------- 1010...x...1101 ```

• Example: flip the 4th bit in a bit pattern OFF and ON

 ``` int main( int argc, char* argv[] ) { char a = 31; /* a = 00011111 (= 31) */ a = a ^ 0b00010000; /* Now: a = 00001111 (= 15) */ printf( "a = %d\n", a ); a = a ^ 0b00010000; /* Now: a = 00011111 (= 31) */ printf( "a = %d\n", a ); } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc bit-op5.c To run:          ./a.out

• Testing if a certain bit in a bit pattern is set/reset

• Testing a certain bit in a bit pattern

• Suppose the variable a contains a bit pattern:

 ``` a = 1010...x...1101 ```

• Problem:

 Test whether the bit x is 1 or not without changing any bit in the pattern.

• We can test the bit x inside the bit pattern by first filtering out this bit using the AND operation with the following specific pattern:

 ``` a = 1010...x...1101 0000..010..0000 AND --------------- 0000...x...0000 ```

and then:

 Testing whether the result is equal to 0 or not.

• Example: test whether the 3rd bit in a bit pattern is set or reset

 ``` int main( int argc, char* argv[] ) { char a = .....; /* Any bit pattern */ if ( (a & 0b00001000) == 0 ) /* Test is 3rd bit is set */ printf( "bit is 0\n" ); else printf( "bit is 1\n" ); } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc bit-op4.c To run:          ./a.out

• Postscript

• We will see soon that there is a better way to construct bit patterns that are used in the bit manipulation operations

• Programming note

• These bit wise operators are also defined in Java.

• They are often omitted in introduction courses because they are not used in program at the introduction level.