Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.
In left rotation, the bits that fall off at left end are put back at right end.
In right rotation, the bits that fall off at right end are put back at left end.
Example:
Let n is stored using 8 bits. Left rotation of n = 11100101 by 3 makes n = 00101111 (Left shifted by 3 and first 3 bits are put back in last ). If n is stored using 16 bits or 32 bits then left rotation of n (000…11100101) becomes 00..0011100101000.
Right rotation of n = 11100101 by 3 makes n = 10111100 (Right shifted by 3 and last 3 bits are put back in first ) if n is stored using 8 bits. If n is stored using 16 bits or 32 bits then right rotation of n (000…11100101) by 3 becomes 101000..0011100.
C++
#include<iostream>
using namespace std;
#define INT_BITS 32
class gfg
{
public :
int leftRotate( int n, unsigned int d)
{
return (n << d)|(n >> (INT_BITS - d));
}
int rightRotate( int n, unsigned int d)
{
return (n >> d)|(n << (INT_BITS - d));
}
};
int main()
{
gfg g;
int n = 16;
int d = 2;
cout << "Left Rotation of " << n <<
" by " << d << " is " ;
cout << g.leftRotate(n, d);
cout << "\nRight Rotation of " << n <<
" by " << d << " is " ;
cout << g.rightRotate(n, d);
getchar ();
}
|
C
#include<stdio.h>
#define INT_BITS 32
int leftRotate( int n, unsigned int d)
{
return (n << d)|(n >> (INT_BITS - d));
}
int rightRotate( int n, unsigned int d)
{
return (n >> d)|(n << (INT_BITS - d));
}
int main()
{
int n = 16;
int d = 2;
printf ( "Left Rotation of %d by %d is " , n, d);
printf ( "%d" , leftRotate(n, d));
printf ( "\nRight Rotation of %d by %d is " , n, d);
printf ( "%d" , rightRotate(n, d));
getchar ();
}
|
Java
import java.io.*;
class GFG
{
static final int INT_BITS = 32 ;
static int leftRotate( int n, int d) {
return (n << d) | (n >> (INT_BITS - d));
}
static int rightRotate( int n, int d) {
return (n >> d) | (n << (INT_BITS - d));
}
public static void main(String arg[])
{
int n = 16 ;
int d = 2 ;
System.out.print( "Left Rotation of " + n +
" by " + d + " is " );
System.out.print(leftRotate(n, d));
System.out.print( "\nRight Rotation of " + n +
" by " + d + " is " );
System.out.print(rightRotate(n, d));
}
}
|
Python3
INT_BITS = 32
def leftRotate(n, d):
return (n << d)|(n >> (INT_BITS - d))
def rightRotate(n, d):
return (n >> d)|(n << (INT_BITS - d)) & 0xFFFFFFFF
n = 16
d = 2
print ( "Left Rotation of" ,n, "by"
,d, "is" ,end = " " )
print (leftRotate(n, d))
print ( "Right Rotation of" ,n, "by"
,d, "is" ,end = " " )
print (rightRotate(n, d))
|
C#
using System;
class GFG
{
static int INT_BITS = 32;
static int leftRotate( int n, int d) {
return (n << d) | (n >> (INT_BITS - d));
}
static int rightRotate( int n, int d) {
return (n >> d) | (n << (INT_BITS - d));
}
public static void Main()
{
int n = 16;
int d = 2;
Console.Write( "Left Rotation of " + n
+ " by " + d + " is " );
Console.Write(leftRotate(n, d));
Console.Write( "\nRight Rotation of " + n
+ " by " + d + " is " );
Console.Write(rightRotate(n, d));
}
}
|
Javascript
<script>
let INT_BITS = 32;
function leftRotate( n, d)
{
return (n << d)|(n >> (INT_BITS - d));
}
function rightRotate( n, d)
{
return (n >> d)|(n << (INT_BITS - d));
}
let n = 16;
let d = 2;
document.write( "Left Rotation of " + n +
" by " + d + " is " );
document.write(leftRotate(n, d));
document.write( "<br>" );
document.write( "\nRight Rotation of " + n +
" by " + d + " is " );
document.write(rightRotate(n, d));
</script>
|
OutputLeft Rotation of 16 by 2 is 64
Right Rotation of 16 by 2 is 4
Time Complexity: O(1)
Auxiliary Space: O(1)
For 16 bit number:
C++
#include <bits/stdc++.h>
using namespace std;
#define SHORT_SIZE 16
unsigned short leftRotate(unsigned short x, short d)
{
return (x << d) | (x >> (SHORT_SIZE - d));
}
unsigned short rightRotate(unsigned short x, short d)
{
return (x >> d) | (x << (SHORT_SIZE - d));
}
int main()
{
unsigned short n = 28;
short d = 2;
cout << leftRotate(n, d) << endl;
cout << rightRotate(n, d) << endl;
return 0;
}
|
C
#include <stdio.h>
#define SHORT_SIZE 16
unsigned short leftRotate(unsigned short x, short d)
{
return (x << d) | (x >> (SHORT_SIZE - d));
}
unsigned short rightRotate(unsigned short x, short d)
{
return (x >> d) | (x << (SHORT_SIZE - d));
}
int main()
{
unsigned short n = 28;
short d = 2;
printf ( "%d\n" , leftRotate(n, d));
printf ( "%d\n" , rightRotate(n, d));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int N= 28 ;
int D= 2 ;
rotate(N,D);
}
static void rotate( int N, int D)
{
int t= 16 ;
int left= ((N<<D) | N>>(t-D)) & 0xFFFF ;
int right=((N>>D) | N<<(t-D)) & 0xFFFF ;
System.out.println(left);
System.out.println(right);
}
}
|
Python3
SHORT_SIZE = 16
def leftRotate(x, d):
return (x << d) | (x >> (SHORT_SIZE - d))
def rightRotate(x, d):
return (x >> d) | (x << (SHORT_SIZE - d)) & 0xDDDDDF
n = 28
d = 2
print ( "Left Rotation of" ,n, "by"
,d, "is" ,end = " " )
print (leftRotate(n, d))
print ( "Right Rotation of" ,n, "by"
,d, "is" ,end = " " )
print (rightRotate(n, d))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void Main(String[] args) {
int N = 28;
int D = 2;
rotate(N, D);
}
static void rotate( int N, int D)
{
int t = 16;
int left = ((N<<D) | N>>(t-D)) & 0xFFFF;
int right = ((N>>D) | N<<(t-D)) & 0xFFFF;
Console.WriteLine(left);
Console.WriteLine(right);
}
}
|
Javascript
<script>
function rotate(N,D)
{
let t = 16;
let left = ((N<<D) | N>>(t-D)) & 0xFFFF;
let right = ((N>>D) | N<<(t-D)) & 0xFFFF;
document.write(left + "<br>" );
document.write(right + "<br>" );
}
let N = 28;
let D = 2;
rotate(N, D);
</script>
|
Time Complexity : O(1)
Space Complexity : O(1)
Please write comments if you find any bug in the above program or other ways to solve the same problem.