C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

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

0 votes
44 views
asked Jan 8, 2014 by sara Guru (24,570 points)

1 Answer

0 votes
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.
answered Jan 8, 2014 by rajesh Guru (39,140 points)

Related questions

0 votes
1 answer 33,441 views
0 votes
1 answer 1,538 views
0 votes
1 answer 57 views
0 votes
1 answer 19 views
...