How to Set, Clear, and Toggle a Single Bit in C++?
Last Updated :
29 Jan, 2024
In bit manipulation, setting a bit, clearing a bit, and toggling a single bit are basic operations. We can easily carry out these operations using the bitwise operators in C++.
In this article, we will see how to set, clear, and toggle operations on the Kth bit.
Example
Input: N = 15, K = 0Output:
Setting Kth bit: 15
Clearing Kth bit: 14
Toggling Kth bit: 14
Explanation: 15 is represented as 1111 in binary and has its first bit 1. Setting it will result in 1111 (15 in decimal). Clearing it will result in 1110 (14 in decimal). Toggling it will result in 1110 (14 in decimal).
Setting a Bit
Setting a bit means that if the given K-th bit is 0, then we set it to 1 and if it is 1 then simply ignore it. By performing bitwise OR (' | ') of a set bit with any bit gives a set bit only, i.e. 0 | 1 = 1 and 1 | 1 = 1. So we can use bitwise OR to set a bit.
Formula:
number |= 1 << bit_position
Clearing a Bit
Clearing a bit means that if the K-th bit is 1, then we clear it to 0, and if it is 0 then simply ignore it. By performing bitwise AND ('&') of a reset bit with any bit gives a reset bit, i.e. 0 & 0 = 0 and 1 & 0 = 0. So, we can use the bitwise AND (&) operator along with the bitwise NOT (~
) operator to clear a bit.
Formula:
number &= ~(1 << bit_position);
Toggling a Bit
Toggling a bit means that if the K-th bit is 1, then we change it to 0 and if it is 0 then change it to 1. By performing XOR ('^') of set and unset gives a set bit and XOR of a set and set bit or unset and unset bits gives an unset bit, i.e. 0 ^ 1 = 1 and 1 ^ 1 = 0. So we can use XOR to toggle a bit.
Formula:
number ^= 1 << bit_position;
C++ Program to Set, Clear and Toggle a Bit
The below example demonstrates how to perform set, clear and toggle operations on a single bit.
C++
// C++ program to demonstrate how to perform set, clear and
// toggle operations on a single bit.
#include <iostream>
using namespace std;
// Function to set the kth bit of n
int setBit(int n, int k) { return (n | (1 << (k))); }
// Function to clear the kth bit of n
int clearBit(int n, int k) { return (n & (~(1 << (k)))); }
// Function to toggle the kth bit of n
int toggleBit(int n, int k) { return (n ^ (1 << (k))); }
int main()
{
int n = 15, k = 0;
cout << n << " with " << k
<< "-th bit Set: " << setBit(n, k) << endl;
cout << n << " with " << k
<< "-th bit Cleared: " << clearBit(n, k) << endl;
cout << n << " with " << k
<< "-th bit Toggled: " << toggleBit(n, k) << endl;
return 0;
}
Output15 with 0-th bit Set: 15
15 with 0-th bit Cleared: 14
15 with 0-th bit Toggled: 14
Similar Reads
How to Count Set Bits in an Integer in C++? In binary representation of a number, a set bit is defined as the binary digit (bit) that is set to 1. In this article, we will learn how to count the set bits in a given integer in C++.ExampleInput: 13Output:The number of set bits in 13 (1101) is: 3Counting Set Bits in an IntegerTo count the set bi
2 min read
How to Create Stack of Bitset in C++? In C++, a stack is a container adapter that provides a last-in, first-out (LIFO) type of data structure with two major operations, namely push and pop while Bitset is a container that can store N bits and provides constant-time operations to manipulate individual bits. In this article, we will learn
2 min read
How to Use Bit Manipulation Methods in C++ Bit manipulation is a technique used for optimizing performance and memory usage in various programming scenarios. It is very useful from a Competitive Programming point of view. In this article, we will learn how we can use bit manipulation methods in C++ and provide examples to help you understand
5 min read
bitset count() in C++ STL bitset::count() is an inbuilt STL in C++ which returns the number of set bits in the binary representation of a number. Syntax: int count() Parameter: The function accepts no parameter. Return Value: The function returns the number of set bits. It returns the total number of ones or the number of se
2 min read
bitset test() in C++ STL bitset::test() is an inbuilt function in C++ STL which tests whether the bit at a given index is set or not. Syntax: bitset_name.test(index) Parameters: The function accepts only a single mandatory parameter index which specifies the index at which the bit is set or not. Return Value: The function r
2 min read
Printing Boolean Values in C++ In C++, a boolean data type can only have two possible values true and false. However, when we print the boolean data types using the standard output streams like cout they are printed as 0 or 1 instead of true or false. In this article, we will learn how to print boolean values in C++ so that true
2 min read