How To Set Clear And Toggle A Single Bit In C++ - C++ Programming Tutorial
C++ Course / STL Set & Map / How To Set Clear And Toggle A Single Bit In C++

How To Set Clear And Toggle A Single Bit In C++

BLUF: Mastering How To Set Clear And Toggle A Single Bit In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Set Clear And Toggle A Single Bit In C++

C++ is renowned for its efficiency. Learn how How To Set Clear And Toggle A Single Bit In C++ enables low-level control and high-performance computing in the tutorial below.

Bit manipulation operators are predominantly utilized in programming at a low level. These operators provide the capability to manipulate individual bits within an integer data type. Within embedded systems, bitwise operators are instrumental in carrying out operations on a specific bit within a port or register. This guide will delve into the process of setting, clearing, and toggling a solitary bit in C++.

1. Setting Bit:

Adjusting the Nth bit involves changing it to 1 if it is currently 0, and leaving it as is if it is already 1. Within C++, the bitwise OR operator (|) is employed to indicate the individual bits within an integer data type. Essentially, the "|" (bitwise OR operator) calculates a fresh integer value where each bit position is set to 1 only if the initial operand (integral type) contains a 1 in that specific position.

Example:

Let's consider a C++ program that demonstrates how to set a specific bit in a binary number.

Example

#include <iostream>
using namespace std; 
int setting_bit(int number, int pos); 
int main() 
{ 
    int number = 18; 
    cout << "Setting up the third bit in the given number: " << number <<"\n"; 
    cout << "The resultant value = " << setting_bit(number, 3); 
    return 0; 
    
} 
int setting_bit(int number, int pos)
{
    return number| (1 << pos); 
}

Output:

Output

Setting up the third bit in the given number: 18
The resultant value = 26

Explanation:

In this illustration, the software establishes a feature named settingbit that requires inputs: number (representing the value) and pos (indicating the bit position to set). Initially, a value of 18 is assigned to the variable number within the main function. Subsequently, the settingbit function is invoked with the number and position provided as parameters. Within the setting_bit function, the operation involving the bitwise OR operator is executed within a diverse range of possibilities.

The calculation (1 << pos) determines a value where the nth bit at index pos is set to 1, while all other bits are set to 0. Performing a bitwise OR operation with the initial number ensures that the specified bit within the range becomes at least 1 if it was previously 0, otherwise it remains unchanged. This modified function is then employed to output the updated number using the cout statement.

2. Clearing a Bit:

Clearing a bit involves resetting it to 0 while leaving other bits unaffected. This process is achieved by employing the bitwise AND operator in combination with the bitwise NOT operator. The bitwise NOT operation flips all bits, turning 1s to 0s and 0s to 1s. This characteristic of bitwise NOT is particularly handy for clearing specific bits that are set.

Example:

Let's consider a C++ code snippet to demonstrate how to clear a specific bit within a program.

Example

#include <iostream>
using namespace std; 
int clearing_bit(int number, int pos); 
int main() 
{ 
    int number = 18; 
    cout << "Clearing up the third bit in the given number: " << number <<"\n"; 
    cout << "The resultant value = " << clearing_bit(number, 3); 
    return 0; 
    
} 
int clearing_bit(int number, int pos)
{
    return number| (1 << pos); 
}

Output:

Output

Clearing up the third bit in the given number: 18
The resultant value = 26

Explanation:

In this instance, the code establishes a function named clearingbit, with arguments: num (representing the range) and pos (indicating the bit's position to clear). Within the clearingbit function, the bit at the specified pos within the range is cleared by performing a bitwise AND operation with a bitmask.

The bitmask is generated by applying the bitwise NOT operator (~) to the value obtained after left-shifting 1 by a specified number of positions. This process effectively changes the bit at a specific position (pos) to 0 while keeping all other bits unchanged. The resulting number is then provided as the output of the clearingbit function. Initially, a value of 12 is assigned to the variable 'number' as part of the main functionality. Subsequently, the clearingbit function is invoked with 'number' as well as the position argument set to 3, resulting in a return value of 8.

3. Toggling bit:

When a bit is flipped, it transitions to its opposite state. If the bit is currently on, it switches off, and conversely, if it is off, it switches on. The bitwise XOR ^ operator can be employed to toggle a bit. This operator flips a bit to 1 when the corresponding bits of the two operands differ, and to 0 when they match.

Example:

Let's consider a C++ program to demonstrate how to toggle a bit.

Example

#include <iostream>
using namespace std; 
int toggling_bit(int number, int pos); 
int main() 
{ 
    int number = 18; 
    cout << "Toggling up the third bit in the given number: " << number <<"\n"; 
    cout << "The resultant value = " << toggling_bit(number, 3); 
    return 0; 
    
} 
int toggling_bit(int number, int pos)
{
    return number| (1 << pos); 
}

Output:

Output

Toggling up the third bit in the given number: 16
The resultant value = 24

Explanation:

In this instance, the software employs a function called togglingbit, which accepts arguments: quantity (representing the range) and pos (indicating the position of the bit to be toggled). Within the togglingbit function, the bit at position pos in the number undergoes toggling by applying a bitwise XOR operation (^) with a bitmask. The bitmask is generated by left shifting 1 by pos positions.

It generates a value where the least significant bit at a specified position is set to 1, while all other bits are set to 0. The XOR operation is then performed to flip the bit at the designated position, turning it to 0 if it was 1 originally, and setting it to 1 if it was initially 0. The function toggling_bit returns this modified value. In the provided illustration, the number 16 is used as input, stored in the variable variety, and the output is 24 after the operation.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience