Rotate Bits of a Number in C++
Introduction:
As a basic operation in computer programming, rotating bits of a number in C++ entails shifting the binary representation of a number to the left or right while wrapping the bits that fall off the end back to the opposite side. This procedure is often used in low-level programming, embedded systems, encryption, and several other applications where bit-level manipulation is necessary.
Data encryption, data compression, optimization, and the solution of certain algorithmic problems are only a few of the uses for bit rotation. There are primarily two ways to do it:
Left bit rotation: A binary number's bits are rotated to the left in a process known as left bit rotation. The leftmost portion circles to the rightmost location.
Right bit rotation: The bits are shifted to the right in a right bit rotation, with the rightmost bit wrapping around to the leftmost position.
In C++, bitwise operators like << (left shift) and >> (right shift) are frequently used to implement bit rotations. With the use of these operations, you can cyclically shift an integer's bits, giving you a flexible tool for fine grained data manipulation.
Example:
Consider storing n in 8 bits. The first three bits are placed back in last, and n = 11100101 is left-rotated by three to become n = 00101111. The left rotation of n (000...11100101) becomes 00..0011100101000 if n is stored using 16 bits or 32 bits, respectively.
If n is stored using 8 bits, the right rotation of n = 11100101 by 3 results in n = 10111100 (Right shifted by 3 and the final 3 bits are put back in first). The right rotation of n (000...11100101) by 3 results in 101000..0011100 whether n is stored using 16 bits or 32 bits, respectively.
C++ Code for rotating bits of a number:
Left Rotation:
Let's take an example to demonstrate the left rotation in C++:
#include <iostream>
#include<conio.h>
// Function to the left rotate a number 'num' by 'bits' positions
unsigned int leftRotate(unsigned int num, unsigned int bits)
{
return (num << bits) | (num >> (32 - bits));
}
int main()
{
unsigned int num = 11100101; // Example number: 10001010 in binary
unsigned int rotated = leftRotate(num, 3); // Rotate left by 3 bits
std::cout << "Original number: " << std::hex << num << std::endl;
std::cout << "Rotated number: " << std::hex << rotated << std::endl;
return 0;
}
Output:
Original number: a95fc5
Rotated number: 54afe28
If the int num is given as 0x8A
Then output is
Original number: 8a
Rotated number: 450
Right Rotation:
Let's take an example to demonstrate the right rotation in C++:
#include <iostream>
#include<conio.h>
unsigned int rightRotate(unsigned int num, unsigned int bits)
{
return (num >> bits) | (num << (32 - bits));
}
int main()
{
unsigned int num = 0x8A; // Example number: 10001010 in binary
unsigned int rotated = rightRotate(num, 3); // Rotate right by 3 bits
std::cout << "Original number: " << std::hex << num << std::endl;
std::cout << "Rotated number: " << std::hex << rotated << std::endl;
return 0;
}
Output:
Original number: 8a
Rotated number: 40000011
Explanation:
In this example, the << and >> operators are used in the code to make left and right rotations. Because 32-bit is a typical word size for integers, it should be noted that we are assuming a 32-bit integer for these instances. You may need to change the amount of bits (for example, to 64 for 64-bit integers) if you're working with a different word size.
Time Complexity: O (1).
Auxiliary Space: O (1).
C++ CODE:
#include <iostream>
unsigned int leftRotate(unsigned int num, unsigned int bits)
{
return (num << bits) | (num >> (32 - bits));
}
unsigned int rightRotate(unsigned int num, unsigned int bits)
{
return (num >> bits) | (num << (32 - bits));
}
int main()
{
unsigned int num = 0x8A; // Example number: 10001010 in binary
unsigned int leftRotated = leftRotate(num, 3);
std::cout << "Left rotated number: " << std::hex << leftRotated << std::endl;
unsigned int rightRotated = rightRotate(num, 3);
std::cout << "Right rotated number: " << std::hex << rightRotated << std::endl;
return 0;
}
Output:
Left rotated number: 450
Right rotated number: 40000011
Conclusion:
In C++, bit rotation is a potent and useful method for modifying the binary representation of numbers. It entails cyclically shifting the bits to the left or right, opening up a variety of uses in low-level programming, cryptography, data compression, optimization, and algorithm creation. Programmers can do left and right bit rotation, effectively wrapping bits around the number, by utilizing bitwise operators like << and >>. This skill is essential in many areas, from increasing data security through encryption to increasing algorithm efficiency. Understanding bit rotation is crucial for programmers dealing with bit-level operations and taking on tasks requiring fine-grained control over binary data. It acts as a fundamental C++ building component for resolving challenging issues and streamlining code.