Open In App

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

Last Updated : 20 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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 = 1
Output: 
Setting Kth bit: 5
Clearing Kth bit: 4
Toggling Kth bit: 4
Explanation: 
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 = 2
Output: 
Setting Kth bit: 7
Clearing Kth bit: 5
Toggling Kth bit: 5
Explanation: 
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;
}


Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads