Open In App

Rotate bits of a number

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a 32-bit integer n and an integer d, rotate the binary representation of n by d positions in both left and right directions. After each rotation, convert the result back to its decimal representation and return both values in an array as [left rotation, right rotation].
Note: 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.

Examples:

Input: n = 28, d = 2
Output: [112, 7]
Explanation: 28 in Binary is: …0000000000011100. Rotating left by 2 positions, it becomes …0000000001110000 = 112 (in decimal). Rotating right by 2 positions, it becomes …0000000000000111 = 7 (in decimal).

Input: n = 29, d = 2
Output: [116, 16391]
Explanation: 29 in Binary is: …0000000000011101. Rotating left by 2 positions, it becomes …0000000001110100 = 116 (in decimal). Rotating right by 2 positions, it becomes …010000000000111 = 16391 (in decimal).

Input: n = 11, d = 10
Output: [11264, 704]

Approach:

One important point to note is, rotating by 32 is equivalent to no rotation, so we take d % 32. For left rotation, we shift the number left and bring the overflowed bits to the right. For right rotation, we shift the number right and bring the lost bits to the left. Finally, we mask the result to ensure it stays within 32 bits.

Steps to implement the above idea:

  • Compute d % 32 to handle unnecessary full rotations.
  • For right rotation, extract the rightmost d bits, shift n right, and place the extracted bits at the leftmost position.
  • Store the right rotated value in the result array after applying a 32-bit mask to keep only valid bits.
  • For left rotation, extract the leftmost d bits, shift n left, and place the extracted bits at the rightmost position.
  • Store the left rotated value in the result array and apply a 32-bit mask to maintain correctness.
  • Return the result array containing the left rotated and right rotated values.
C++
// C++ Code to perform left and right rotations 
// on the binary representation of a 32-bit integer
#include <bits/stdc++.h>
using namespace std;

// Function to perform left rotation
int leftRotate(int n, int d) {
    
    // Rotation of 32 is same as rotation of 0
    d = d % 32;
    
    // Picking the rightmost (32 - d) bits
    int mask = ~((1 << (32 - d)) - 1);
    int shift = (n & mask);
    
    // Moving the remaining bits to their new location
    n = (n << d);
    
    // Adding removed bits at leftmost end
    n += (shift >> (32 - d));

    // Ensuring 32-bit constraint
    return n & ((1 << 32) - 1);
}

// Function to perform right rotation
int rightRotate(int n, int d) {
    
    // Rotation of 32 is same as rotation of 0
    d = d % 32;
    
    // Picking the leftmost d bits
    int mask = (1 << d) - 1;
    int shift = (n & mask);
    
    // Moving the remaining bits to their new location
    n = (n >> d);
    
    // Adding removed bits at rightmost end
    n += (shift << (32 - d));

    // Ensuring 32-bit constraint
    return n & ((1 << 32) - 1);
}

// Function to perform both rotations
vector<int> rotateBits(int n, int d) {
    
    vector<int> res(2);
    
    // Calling left and right rotation functions
    res[0] = leftRotate(n, d);
    res[1] = rightRotate(n, d);
    
    return res;
}

// Driver code
int main() {
    
    int n = 28, d = 2;

    vector<int> result = rotateBits(n, d);
    
    cout << result[0] << " " << result[1] << endl;
    
    return 0;
}
Java
// Java Code to perform left and right rotations 
// on the binary representation of a 32-bit integer
class GfG {

    // Function to perform left rotation
    static int leftRotate(int n, int d) {

        // Rotation of 32 is same as rotation of 0
        d = d % 32;

        // Moving bits left and wrapping around
        return (n << d) | (n >>> (32 - d));
    }

    // Function to perform right rotation
    static int rightRotate(int n, int d) {

        // Rotation of 32 is same as rotation of 0
        d = d % 32;

        // Moving bits right and wrapping around
        return (n >>> d) | (n << (32 - d));
    }

    // Function to perform both rotations
    static int[] rotateBits(int n, int d) {
        
        int[] res = new int[2];
        
        // Calling left and right rotation functions
        res[0] = leftRotate(n, d);
        res[1] = rightRotate(n, d);
        
        return res;
    }

    // Driver code
    public static void main(String[] args) {
        
        int n = 28, d = 2;

        int[] result = rotateBits(n, d);
        
        System.out.println(result[0] + " " + result[1]);
    }
}
Python
# Python Code to perform left and right rotations 
# on the binary representation of a 32-bit integer

# Function to perform left rotation
def leftRotate(n, d):
    
    # Rotation of 32 is same as rotation of 0
    d = d % 32
    
    # Picking the rightmost (32 - d) bits
    mask = ~((1 << (32 - d)) - 1)
    shift = (n & mask)
    
    # Moving the remaining bits to their new location
    n = (n << d)
    
    # Adding removed bits at leftmost end
    n += (shift >> (32 - d))

    # Ensuring 32-bit constraint
    return n & ((1 << 32) - 1)

# Function to perform right rotation
def rightRotate(n, d):
    
    # Rotation of 32 is same as rotation of 0
    d = d % 32
    
    # Picking the leftmost d bits
    mask = (1 << d) - 1
    shift = (n & mask)
    
    # Moving the remaining bits to their new location
    n = (n >> d)
    
    # Adding removed bits at rightmost end
    n += (shift << (32 - d))

    # Ensuring 32-bit constraint
    return n & ((1 << 32) - 1)

# Function to perform both rotations
def rotateBits(n, d):
    
    res = [0] * 2
    
    # Calling left and right rotation functions
    res[0] = leftRotate(n, d)
    res[1] = rightRotate(n, d)
    
    return res

# Driver code
if __name__ == "__main__":
    
    n, d = 28, 2

    result = rotateBits(n, d)
    
    print(result[0], result[1])
C#
// C# Code to perform left and right rotations 
// on the binary representation of a 32-bit integer
using System;

class GfG {

    // Function to perform left rotation
    static int LeftRotate(int n, int d) {

        // Rotation of 32 is same as rotation of 0
        d = d % 32;

        // Moving bits left and wrapping around
        return (n << d) | (int)((uint)n >> (32 - d));
    }

    // Function to perform right rotation
    static int RightRotate(int n, int d) {

        // Rotation of 32 is same as rotation of 0
        d = d % 32;

        // Moving bits right and wrapping around
        return ((int)((uint)n >> d)) | (n << (32 - d));
    }

    // Function to perform both rotations
    static int[] RotateBits(int n, int d) {
        
        int[] res = new int[2];
        
        // Calling left and right rotation functions
        res[0] = LeftRotate(n, d);
        res[1] = RightRotate(n, d);
        
        return res;
    }

    // Driver code
    static void Main() {
        
        int n = 28, d = 2;

        int[] result = RotateBits(n, d);
        
        Console.WriteLine(result[0] + " " + result[1]);
    }
}
JavaScript
// JavaScript Code to perform left and right rotations 
// on the binary representation of a 32-bit integer

// Function to perform left rotation
function leftRotate(n, d) {

    // Rotation of 32 is same as rotation of 0
    d = d % 32;

    // Moving bits left and wrapping around
    return ((n << d) | (n >>> (32 - d))) >>> 0;
}

// Function to perform right rotation
function rightRotate(n, d) {

    // Rotation of 32 is same as rotation of 0
    d = d % 32;

    // Moving bits right and wrapping around
    return ((n >>> d) | (n << (32 - d))) >>> 0;
}

// Function to perform both rotations
function rotateBits(n, d) {
    
    let res = new Array(2);
    
    // Calling left and right rotation functions
    res[0] = leftRotate(n, d);
    res[1] = rightRotate(n, d);
    
    return res;
}

// Driver code
let n = 28, d = 2;

let result = rotateBits(n, d);

console.log(result[0], result[1]);

Output
112 7

Time Complexity: O(1) – Only a few bitwise operations.
Space Complexity: O(1) – No extra space except variables. 



Next Article
Article Tags :
Practice Tags :

Similar Reads