# Which bit wise operator is suitable for turning off a particular bit in a number in C/C++?

+6 votes
3,424 views
Which bit wise operator is suitable for turning off a particular bit in a number in C/C++?
posted Nov 17, 2013
Share this question

## 4 Answers

+1 vote

Use XOR(^) operator for switching ON/OFF a particular bit

answer Nov 18, 2013
But it may not tell if you have switched off or on. It will just toggle.
Then this will work
(number | ~(0 | 2^(bitPosn)))
0 votes

say you want to switch off 8th bit from right, use & operator in the following way.

``````num = num & (~(1<<7));
``````

say you want to switch on 8th bit from right, use | operator in the following way.

``````num = num | ((1<<7));
``````
answer Nov 18, 2013
0 votes

Use bit-wise AND (&) to turn off a particular bit in a number.

``````int bit_off(int num, int pos)
{
num = num & (~ (1 << pos));
return num;
}
``````
answer Dec 5, 2013
I think it should be num = num & (~ (1 << (pos-1)));
Yeah, It depends on u r going to start value of pos from 0 or 1....I m considering 0 !!
–1 vote

why cant we try this..

(bit position) & 0 .

answer Nov 18, 2013 by
(num & 0) will always results to 0.
but the question is for off only...
yeah, check my answer...it is only for off a particular bit
Similar Questions
+3 votes

Something like :

IN(Range)