C++ Library - <bit>
The <bit> header in C++, provides several functions for bit manipulation and bitwise operations, which are essential for low-level programming tasks. Includes functions for counting bits, finding the highest/lowest set bit etc. This header is part of the Numeric library.
Including <bit> Header
To include the <bit> header in your C++ program, you can use the following syntax.
#include <bit>
Functions of <bit> Header
Below is list of all functions from <bit> header.
Type Casting and Byte Manipulation
In C++ type casting and byte manipulation functions deals with representation of data types at the byte level. The binary representation of data is crucial for data serialization and performance optimization.
| S.No | Functions & Description |
|---|---|
| 1 | bit_cast
This function reinterpret the object representation of one type as that of another. |
| 2 | byteswap
This function reverses the bytes in the given integer value. |
Manual Byteswap Implementation
In the following example, we are going to implement byteswap manually.
#include <bit>
#include <iostream>
uint16_t manual_byteswap(uint16_t value) {
return (value >> 8) | (value << 8);
}
int main() {
uint16_t value = 0x1234;
uint16_t swapped = manual_byteswap(value);
std::cout << std::hex << swapped;
return 0;
}
Output
If we run the above code it will generate the following output −
3412
Power of Two Operations
By using these opeartions, we can Check whether a number is a power of two, or finding the nearest power of two, can optimize memory allocation or bitwise operations.
| S.No | Functions & Description |
|---|---|
| 1 | has_single_bit
This function checks if a number is an integral power of 2. |
| 2 | bit_ceil
This function finds the smallest integral power of two not less than the given value. |
| 3 | bit_floor
This function finds the largest integral power of two not greater than the given value. |
| 4 | bit_width
This function finds the smallest number of bits needed to represent the given value. |
Finding the smallest power
In the following example, we are going to find the smallest power of two using bit_ceil.
#include <bit>
#include <iostream>
int main() {
uint32_t value = 10;
uint32_t ceil_value = std::bit_ceil(value);
std::cout << ceil_value;
return 0;
}
Output
If we run the above code it will generate the following output −
16
Bitwise Rotation
Bitwise rotation involves shifting the bits of a number left or right.These Rotations are commonly used in cryptographic algorithms, hash functions, and low-level optimizations
| S.No | Functions & Description |
|---|---|
| 1 | rotl
This function computes the result of bitwise left-rotation. |
| 2 | rotr
This function computes the result of bitwise right-rotation. |
Rotating bits
In the following example, we are going to use rotl to rotate the bits of a number to the left.
#include <bitset>
#include <bit>
#include <iostream>
uint8_t manual_rotl(uint8_t value, int shift) {
return (value << shift) | (value >> (8 - shift));
}
int main() {
uint8_t value = 0b10110001;
uint8_t rotated = manual_rotl(value, 2);
std::cout << std::bitset<8>(rotated);
return 0;
}
Output
If we run the above code it will generate the following output −
11000110
Bit Counting
Bit counting functions are used to count consecutive bits (0s or 1s) or total bits set to 1. These operations are mostly used in low-level programming, where efficient manipulation of individual bits can significantly optimize performance.
| S.No | Functions & Description |
|---|---|
| 1 | countl_zero
This function counts the number of consecutive 0 bits, starting from the most significant bit. |
| 2 | countl_one
This function counts the number of consecutive 1 bits, starting from the most significant bit. |
| 3 | countr_zero
This function counts the number of consecutive 0 bits, starting from the least significant bit. |
| 4 | countr_one
This function counts the number of consecutive 1 bits, starting from the least significant bit. |
| 5 | popcount
This function counts the number of 1 bits in an unsigned integer. |
Counting the Bits Manually
In the following example, we are going to use popcount to count the number of bits set to 1 in a number.
#include <bit>
#include <iostream>
uint32_t manual_popcount(uint32_t value) {
uint32_t count = 0;
while (value) {
count += value & 1;
value >>= 1;
}
return count;
}
int main() {
uint32_t value = 0b10110001;
uint32_t count = manual_popcount(value);
std::cout << "Number of 1s: " << count;
return 0;
}
Output
If we run the above code it will generate the following output−
Number of 1s: 4