C program to clear nth bit of a number using bitwise and operator

165 Views Asked by At

I don't understand why these are different
this is how I wrote it :

 number &= 0 << time;

and this is how it should be written :

number &= ~(1 << time);
1

There are 1 best solutions below

0
Alex Guteniev On BEST ANSWER

Bitwise operations are performed on all bits.

The result of 0 << time is timeth bit set to zero, and all other bits are zero. So all bits are zero. Bitwise and with zero will always result in zero.

Contrary, the result of 1 << time is timeth bit set to one, and all other bits are zero. After bitwise negation, the result is timeth bit set to zero, and all other bits are one. Bitwise and will zero out only timeth bit.