Circular shift on the binary representation of an integer by `k` positions
Given two positive integers n
and k
, perform a circular shift on the binary representation of n
by k
positions.
The circular shift can be of two types:
- Left circular shift (moving the final bit to the first position while shifting all other bits to the next position).
- Right circular shift (moving the first bit to the last position while shifting all other bits to the previous position).
For example,
N = 127 (00000000000000000000000001111111)
shift = 3
Output:
Left Shift 00000000000000000000001111111000
Right Shift 11100000000000000000000000001111
The idea is to perform a normal bitwise left or right shift by first isolating the sequence of k–bits from the right or left side, respectively. Finally, return the bitwise OR
of the shifted number with isolated bits at their correct position.
For example, consider n = 127
which is 00000000000000000000000001111111
.
Circular Right shift by 3:
00000000000000000000000001111111
2. Right shift by 3
00000000000000000000000001111111
00000000000000000000000000001111
3. OR with isolated bits:
00000000000000000000000000001111
11100000000000000000000000000000
————————————————————————————————
11100000000000000000000000001111
Circular Left shift by 3:
00000000000000000000000001111111
2. Left shift by 3:
00000000000000000000000001111111
00000000000000000000001111111000
3. OR with isolated bits:
00000000000000000000001111111000
00000000000000000000000000000000
————————————————————————————————
00000000000000000000001111111000
Following is the C++ and Java program that demonstrates it:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <bitset> using namespace std; // A macro that defines integer size #define SIZE_INT sizeof(int) * 8 // Function to perform left circular shift or right circular shift // on integer `n` by `k` positions based on flag `isLeftShift` int circularShift(unsigned n, int k, bool isLeftShift) { // left shift by `k` if (isLeftShift) { return (n << k) | (n >> (SIZE_INT - k)); } // right shift by `k` return (n >> k) | (n << (SIZE_INT - k)); } int main() { unsigned n = 127; int shift = 3; cout << "No Shift " << bitset<32>(n) << endl; cout << "Left Shift " << bitset<32>(circularShift(n, shift, true)) << endl; cout << "Right Shift " << bitset<32>(circularShift(n, shift, false)) << endl; return 0; } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Main { public static String toBinaryString(int n) { return String.format("%32s", Integer.toBinaryString(n)) .replaceAll(" ", "0"); } // Function to perform left circular shift or right circular // shift on integer `n` by `k` positions based on flag `isLeftShift` public static int shift(int n, int k, boolean isLeftShift) { // left shift by `k` if (isLeftShift) { return (n << k) | (n >> (Integer.SIZE - k)); } // right shift by `k` return (n >> k) | (n << (Integer.SIZE - k)); } public static void main(String[] args) { int n = 127; int shift = 3; System.out.println("No Shift " + toBinaryString(n)); System.out.println("Left Shift " + toBinaryString(shift(n, shift, true))); System.out.println("Right Shift " + toBinaryString(shift(n, shift, false))); } } |
No Shift 00000000000000000000000001111111
Left Shift 00000000000000000000001111111000
Right Shift 11100000000000000000000000001111
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)