Set, Clear and Toggle a Given Bit of a Number in C
Last Updated :
12 Aug, 2024
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.
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.
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.
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;
}
OutputOriginal 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.
Similar Reads
C program to set K-th bit of a number N
Given a number N and an integer K, the task is to set the Kth bit of the number N, i.e., if the Kth bit is 0, then set it to 1 and if it is 1 then leave it unchanged. Examples: Input: N = 5, K = 2Output: 7Explanation: 5 is represented as 101 in binary and has its second bit 0, so setting it will res
3 min read
C/C++ Program to Count set bits in an integer
Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on âPRACTICE
2 min read
Program to find absolute value of a given number
Given an integer N, The task is to find the absolute value of the given integer. Examples: Input: N = -6 Output: 6Explanation: The absolute value of -6 is 6 which is non negative Input: N = 12 Output: 12 Naive Approach: To solve the problem follow the below idea: The absolute value of any number is
9 min read
C program to count zeros and ones in binary representation of a number
Given a number N, the task is to write C program to count the number of 0s and 1s in the binary representation of N. Examples: Input: N = 5 Output: Count of 0s: 1 Count of 1s: 2 Explanation: Binary representation of 5 is "101".Input: N = 22 Output: Count of 0s: 2 Count of 1s: 3 Explanation: Binary r
5 min read
How to Convert an Integer to a String in C?
Write a C program to convert the given integer value to string. Examples Input: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234". Input: -567Output: "-567"Explanation: The integer -567 is converted to the string "-567". Different Methods to Convert an Integer to a St
4 min read
C Program to Rotate bits of a number
Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put ba
2 min read
C Program to Convert a Char From Upper to Lower and Vice Versa in a Single Line Efficiently
Here we will build a program to showcase an efficient way to convert a char from upper to lower and vice versa in C in a single line. Below is one example which explains how bitwise operators help in efficient programming and also convert the given alphabet from one case to another. Example: C/C++ C
1 min read
C Program to Check for Odd or Even Number
Write a C program to check whether the given number is an odd number or an even number. A number that is completely divisible by 2 is an even number and a number that is not completely divisible by 2 leaving a non-zero remainder is an odd number. Example Input: N = 4Output: EvenExplanation: 4 is div
4 min read
C Program to Check Whether a Number is Positive or Negative or Zero
Write a C program to check whether a given number is positive, negative, or zero. Examples Input: 10Output: PositiveExplanation: Since 10 is greater than 0, it is positive. Input: -5Output: NegativeExplanation: Since -5 is less than 0, it is negative. Different Ways to Check for Positive Numbers, Ne
3 min read
Find the remainder when N is divided by 4 using Bitwise AND operator
Given a number N, the task is to find the remainder when N is divided by 4 using Bitwise AND operator. Examples: Input: N = 98 Output: 2Explanation: 98 % 4 = 2. Hence the output is 2. Input: 200Output: 0Explanation: 200 % 4 = 0. Hence output is 0. Recommended: Please try your approach on {IDE} first
9 min read