ProAnswers.org

Which bitwise operator is suitable for turning OFF a particular bit in a number?

Bitwise AND operator (&), one’s complement operator(~)

Example: To unset the 4th bit of byte_data or to turn off a particular bit in a number.

Explanation:

Consider,

char byte_data= 0b00010111;

byte_data= (byte_data)&(~(1<<4));

1 can be represented in binary as 0b00000001 = (1<<4)

<< is a left bit shift operator,

it shifts the bit 1 by 4 places towards left.

(1<<4) becomes 0b00010000

And ~ is the one’s complement operator in C language.

So ~(1<<4) = complement of 0b00010000

= 0b11101111

Replacing value of byte_data and ~(1<<4) in

(byte_data)&(~(1<<4));

we get (0b00010111) & (0b11101111)

Perform AND operation to below bytes.

00010111

11101111


00000111


Thus the 4th bit is unset.