In this guide, you will discover how to perform bit rotation on a number using C++.
A specified number of shifts either to the left or right manipulates the binary form of a digit, causing its bits to rotate. Various bitwise operations benefit from this technique. In C++, the fundamental types of rotation include shifting to the left and shifting to the right. These rotations form the core principles of bit rotation theory.
- Bitwise Operators:
Bitwise Left Shift (<<): This operator moves the bits of a number to the left by a predetermined amount. The number is effectively multiplied by two to the power of the shift amount.
Bitwise Right Shift (>>): This operator moves the bits to the right in a predetermined number of positions. The number is effectively divided by 2 to the power of the shift amount. The leftmost positions of unsigned numbers are filled with zeros.
- Left rotation: All bits are rotated to the left by a certain number of positions, and any bits that fall off the left end are wrapped around to the right end. It can be accomplished by combining the bitwise OR operation with a left shift.
- Right rotation: It entails wrapping the bits that fall off the right end back to the left end and moving all bits a certain number of positions to the right. Bitwise OR and the appropriate shift can be used to accomplish this.
- Data Type and Size Handling: Take the size of the data type into account while rotating the bits. There may be unpredictable behavior if bits are shifted beyond the size of the data type. To prevent problems, you should rotate bits within this size since an integer in C++ typically takes up 4 bytes (32 bits) or 8 bytes (64 bits) , depending on the system.
It is essential to take into account the size of the data type and the potential for overflow or underflow when employing bit manipulation in C++. Bit manipulation is commonly used in embedded systems, systems programming, and other situations that demand precise control over data representation.
Modifying the individual bits of a numeric value in C++ involves employing bitwise manipulations such as left shift (<<) and right shift (>>). These shifted bits are merged together through the bitwise OR (|) operator to achieve the desired rotation outcome.
Example:
#include <iostream>
// Function to rotate a number's bits to the left
unsigned int Left_Rotate(unsigned int num, unsigned int d)
{
return (num << d) | (num >> (sizeof(num) * 8 - d));
}
// function to rotate a number's bits to the right
unsigned int Right_Rotate(unsigned int num, unsigned int d)
{
return (num >> d) | (num << (sizeof(num) * 8 - d));
}
int main()
{
unsigned int num_ber = 64;
// Example number
unsigned int Rotate_By = 4;
// Number of rotating bits
std::cout << "Original number: " << num_ber << std::endl;
std::cout << "Left rotate by " << Rotate_By << " bits: " << Left_Rotate(num_ber, Rotate_By) << std::endl;
std::cout << "Right rotate by " << Rotate_By << " bits: " << Right_Rotate(num_ber, Rotate_By) << std::endl;
return 0;
}
Output:
Original number: 64
Left rotate by 4 bits: 1024
Right rotate by 4 bits: 4
Explanation:
- In this example, input/output activities need the inclusion of the header file "iostream" .
- There are two defined functions for handling left and right bit rotations, respectively: LeftRotate and RightRotate . The number to be rotated and the number of bits to rotate by are the two arguments that these functions accept.
- With the help of the left shift operator, the Left_Rotate function rotates the bits of the number num by d degrees to the left. As a result of the bitwise OR (|) operation that follows, the bits of num is moved to the right by sizeof(num) * 8 - d positions. The left-to-right bits that are relocated ensure that they are brought back to the right end with this operation.
- Using the right shift operator >>, the Right_Rotate function rotates the bits of the number num by d degrees to the right. After that, the bits of num is moved left by sizeof(num) * 8 - d places as a result of a bitwise OR (|) operation. The bits that are pushed out to the right are returned to the left end due to this operation.
- Both an example number number (which is originally set to 64) and the number of bits to rotate RotateBy (which is initially set to 4) are defined in the main function.
- The original number is printed.
- Both the results of rotating the bits to the left by RotateBy bits and to the right by RotateBy bits are reported.