Electro4u 28 Dec 2022 8 views 0 comments C Programming

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

Turning Off a Specific Bit in a Number in C using Bitwise Operators

To turn off a particular bit in a number, the suitable bitwise operator to use is the bitwise AND operator (&).

The bitwise AND operator works by setting each bit of the result to 1 only if the corresponding bits of both operands are 1. If any of the corresponding bits are 0, the result bit is set to 0. By using a bitwise AND with a number that has a 0 bit in the position we want to turn off, we can set that bit to 0 in the original number.

Here's an example in C code that turns off the third bit (counting from the right) of a variable x:

x = x & ~(1 << 2);

In this example, the expression 1 << 2 creates a value with a 1 in the third bit position (2 because we are counting from 0), and the bitwise NOT operator (~) is used to invert all the bits in this value so that it has a 0 in the third bit position and 1s elsewhere. Finally, the bitwise AND operator is used to mask off the third bit of x by setting it to 0 while leaving all other bits unchanged.

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

C Bitwise Operators Explained: 10 Practice Programs with Output

E.1: Bitwise vs. Logical Operators#include<stdio.h> int main(void){ int x=7,y=19; printf("%d %d ",x&y...

AG Anvesh G 29 Nov 2026
Bit wise operators Assignment

Bitwise Operators practical assignment: Take runtime input, bit-level manipulation, and different data units r...

AG Anvesh G 27 Sep 2026
Understanding Float and Double Binary Representation in C

Understanding Float and Double Binary Representation in CIntroduction:When we store decimal numbers such as 10...

AG Anvesh G 31 May 2026