# Set, Clear and Toggle a given bit of a number in C

Given a number N, the task is to set, clear and toggle the K-th bit of this number N.

- Setting a bit means that if K-th bit is 0, then set it to 1 and if it is 1 then leave it unchanged.
- Clearing a bit means that if K-th bit is 1, then clear it to 0 and if it is 0 then leave it unchanged.
- Toggling a bit means that if K-th bit is 1, then change it to 0 and if it is 0 then change it to 1.

**Examples:**

Input:N = 5, K = 1Output:Setting Kth bit: 5 Clearing Kth bit: 4 Toggling Kth bit: 4Explanation:5 is represented as 101 in binary and has its first bit 1, so setting it will result in 101 i.e. 5. clearing it will result in 100 i.e. 4. toggling it will result in 100 i.e. 4.Input:N = 7, K = 2Output:Setting Kth bit: 7 Clearing Kth bit: 5 Toggling Kth bit: 5Explanation:7 is represented as 111 in binary and has its second bit 1, so setting it will result in 111 i.e. 7. clearing it will result in 101 i.e. 5. toggling it will result in 101 i.e. 5.

**Approach:**

Below are the steps to set, clear and toggle Kth bit of N:

**Setting a bit**

- Since we all know that performing bitwise OR of any bit with a set bit results in a set bit, i.e.
Any bit <bitwise OR> Set bit = Set bit which means, 0 | 1 = 1 1 | 1 = 1

- So for setting a bit, performing a bitwise OR of the number with a set bit is the best idea.
N = N | 1 << K OR N |= 1 << K where K is the bit that is to be set

**Clearing a bit**

- Since bitwise AND of any bit with a reset bit results in a reset bit, i.e.
Any bit <bitwise AND> Reset bit = Reset bit which means, 0 & 0 = 0 1 & 0 = 0

- So for clearing a bit, performing a bitwise AND of the number with a reset bit is the best idea.
n = n & ~(1 << k) OR n &= ~(1 << k) where k is the bit that is to be cleared

**Toggle a bit**

- Since XOR of unset and set bit results in a set bit and XOR of a set and set bit results in an unset bit. Hence performing bitwise XOR of any bit with a set bit results in toggle of that bit, i.e.
Any bit <bitwise XOR> Set bit = Toggle which means, 0 ^ 1 = 1 1 ^ 1 = 0

- So in order to toggle a bit, performing a bitwise XOR of the number with a reset bit is the best idea.
n = n ^ 1 << k OR n ^= 1 << k where k is the bit that is to be cleared

Below is the implementation of the above approach:

`// C program to set, clear and toggle a bit ` ` ` `#include <stdio.h> ` ` ` `// Function to set the kth bit of n ` `int` `setBit(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(n | (1 << (k - 1))); ` `} ` ` ` `// Function to clear the kth bit of n ` `int` `clearBit(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(n & (~(1 << (k - 1)))); ` `} ` ` ` `// Function to toggle the kth bit of n ` `int` `toggleBit(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(n ^ (1 << (k - 1))); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, k = 1; ` ` ` ` ` `printf` `(` `"%d with %d-th bit Set: %d\n"` `, ` ` ` `n, k, setBit(n, k)); ` ` ` `printf` `(` `"%d with %d-th bit Cleared: %d\n"` `, ` ` ` `n, k, clearBit(n, k)); ` ` ` `printf` `(` `"%d with %d-th bit Toggled: %d\n"` `, ` ` ` `n, k, toggleBit(n, k)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5 with 1-th bit Set: 5 5 with 1-th bit Cleared: 4 5 with 1-th bit Toggled: 4

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Toggle all the bits of a number except k-th bit.
- For every set bit of a number toggle bits of other
- Program to toggle K-th bit of a number N
- Toggle all even bits of a number
- Toggle all odd bits of a number
- Toggle first and last bits of a number
- Program to clear K-th bit of a number N
- Toggle bits of a number except first and last bits
- Toggle the last m bits
- Toggle all bits after most significant bit
- Toggle bits in the given range
- Toggle case of a string using Bitwise Operators
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find the largest number smaller than integer N with maximum number of set bits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.