Open In App

Set, Clear and Toggle a Given Bit of a Number in C

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to Set, Clear, and Toggle the given bit of a number.

  • Setting a bit means that if Kth bit is 0, then set it to 1 and if it is 1 then leave it unchanged.
  • Clearing a bit means that if Kth bit is 1, then clear it to 0 and if it is 0 then leave it unchanged.
  • Toggling a bit means that if Kth 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.

Setting a Bit in C

To set a specific bit in a number, you have to perform the Bitwise OR operation on the given number with a bit mask in which only the bit you want to set is set to 1, and all other bits are set to 0.

Input: N = 01100111, K = 5

Output: N = 01100111, mask_used: 00100000

We can create the bitmask using the left shift operator and then perform the Bitwise OR operation.

N = N | 1 << K
or
N |= 1 << K

where K is the position of the bit that is to be set, and N is the number.

Clearing a Bit in C

To clear a specific bit in a number, you have to perform the Bitwise AND operation on the given number with a bit mask in which only the bit you want to clear is set to 0, and all other bits are set to 1.

Input: N = 01100111, K = 5

Output: N = 01100111, mask_used = 1011111

We can create the bitmask using the left shift operator along with NOT operator and then perform the Bitwise AND operation.

N = N & ~ (1 << K)
or
N &= ~ (1 << K)

where K is the position of the bit that is to be set and N is the number.

Toggle a Bit in C

To toggle a specific bit in a number, you have to perform the Bitwise XOR operation on the given number with a bit mask in which only the bit you want to toggle is set to 1, and all other bits are set to 0.

Input: N = 01100111, K = 5

Output: N = 01100111, mask_used: 00010000

We can create the bitmask using the left shift operator and then perform the Bitwise XOR operation.

N = N ^ 1 << K
or
N ^= 1 << K

where K is the position of the bit that is to be set and N is the number.

C Program to Set, Clear and Toggle a Bit of the Given Number

C
// C program to set, clear and toggle a bit of the given number
#include <stdio.h>

int setBit(int N, int K) {
  
    // Bitwise OR with the mask
    return (N | (1 << (K - 1)));
}

int clearBit(int N, int K) {
  
    // Bitwise AND with the mask
    return (N & (~(1 << (K - 1))));
}

int toggleBit(int N, int K) {
  
    // Bitwise XOR with the mask
    return (N ^ (1 << (K - 1)));
}

int main() {
    int N = 5, K = 1;
  	printf("Original Number: %d,  Bit Position: %d\n", N, K);

 	// Performing the operations 
    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
Original Number: 5,  Bit Position: 1
5 with 1-th bit Set: 5
5 with 1-th bit Cleared: 4
5 with 1-th bit Toggled: 4

Time Complexity: O(1), all the operations will have constant time complexity.
Auxiliary Space: O(1)

Conclusion

Setting, clearing, and toggling specific bits in a number are fundamental operations that can be efficiently performed using bitwise operators in C. These operations are often used in low-level programming, where direct manipulation of individual bits is required, such as in embedded systems, memory management, and cryptography.



Next Article
Article Tags :
Practice Tags :

Similar Reads