Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Check if two numbers are bit rotations of each other or not

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given two positive integers x and y (0 < x, y < 2^32), check if one integer is obtained by rotating bits of the other. 

Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end.

Examples: 

Input : a = 8, b = 1
Output : yes
Explanation : Representation of a = 8 : 0000 0000 0000 0000 0000 0000 0000 1000 ,Representation of b = 1 : 0000 0000 0000, 0000 0000 0000 0000 0001. If we rotate a by 3 units right we get b, hence answer is yes.

Input : a = 122, b = 2147483678
Output : yes
Explanation :Representation of a = 122        : 0000 0000 0000 0000 0000 0000 0111 1010,Representation of b = 2147483678 : 1000 0000 0000 0000 0000 0000 0001 1110, If we rotate a by 2 units right we get b, hence answer is yes.

Approach:

  • Since total bits in which x or y can be represented is 32 since x, y > 0 and x, y < 2^32. 
  • So we need to find all 32 possible rotations of x and compare them with y till x and y are not equal. 
  • To do this we use a temporary variable x64 with 64 bits, which is result of the concatenation of x to x ie. x64 has the first 32 bits the same as bits of x and the last 32 bits are also the same as bits of x64.
  • Then we keep on shifting x64 by 1 on the right side and compare the rightmost 32 bits of x64 with y. 
  • In this way, we’ll be able to get all the possible bits combinations due to rotation.

Here is implementation of above algorithm.
 

C++




// C++ program to check if two numbers are bit rotations
// of each other.
#include <iostream>
using namespace std;
 
// function to check if  two numbers are equal
// after bit rotation
bool isRotation(unsigned int x, unsigned int y)
{
    // x64 has concatenation of x with itself.
    unsigned long long int x64 = x | ((unsigned long long int)x << 32);
 
    while (x64 >= y)
    {
        // comparing only last 32 bits
        if (unsigned(x64) == y)
            return true;
 
        // right shift by 1 unit
        x64 >>= 1;
    }
    return false;
}
 
// driver code to test above function
int main()
{
    unsigned int x = 122;
    unsigned int y = 2147483678;
 
    if (isRotation(x, y))
        cout << "yes" << endl;
    else
        cout << "no" << endl;
 
    return 0;
}

Java




// Java program to check if two numbers are bit rotations
// of each other.
class GFG {
 
// function to check if two numbers are equal
// after bit rotation
    static boolean isRotation(long x, long y) {
        // x64 has concatenation of x with itself.
        long x64 = x | (x << 32);
 
        while (x64 >= y) {
            // comparing only last 32 bits
            if (x64 == y) {
                return true;
            }
 
            // right shift by 1 unit
            x64 >>= 1;
        }
        return false;
    }
 
// driver code to test above function
    public static void main(String[] args) {
        long x = 122;
        long y = 2147483678L;
 
        if (isRotation(x, y) == false) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program to check if two
# numbers are bit rotations of each other.
 
# function to check if two numbers
# are equal after bit rotation
def isRotation(x, y) :
     
    # x64 has concatenation of x
    # with itself.
    x64 = x | (x << 32)
     
    while (x64 >= y) :
         
        # comparing only last 32 bits
        if ((x64) == y) :
            return True
 
        # right shift by 1 unit
        x64 >>= 1
 
    return False
 
# Driver Code
if __name__ == "__main__" :
 
    x = 122
    y = 2147483678
     
    if (isRotation(x, y) == False) :
        print("yes")
    else :
        print("no")
 
# This code is contributed by Ryuga

C#




// C# program to check if two numbers
// are bit rotations of each other.
using System;
 
class GFG
{
 
// function to check if two numbers
// are equal after bit rotation
static bool isRotation(long x, long y)
{
    // x64 has concatenation of
    // x with itself.
    long x64 = x | (x << 32);
 
    while (x64 >= y)
    {
        // comparing only last 32 bits
        if (x64 == y)
        {
            return true;
        }
 
        // right shift by 1 unit
        x64 >>= 1;
    }
    return false;
}
 
// Driver Code
public static void Main()
{
    long x = 122;
    long y = 2147483678L;
 
    if (isRotation(x, y) == false)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed
// by 29AjayKumar

PHP




<?php
// PHP program to check if two
// numbers are bit rotations of
// each other.
 
// function to check if two
// numbers are equal after
// bit rotation
function isRotation($x, $y)
{
    // x64 has concatenation
    // of x with itself.
    $x64 = $x | ($x << 32);
 
    while ($x64 >= $y)
    {
        // comparing only last 32 bits
        if (($x64) == $y)
            return 1;
 
        // right shift by 1 unit
        $x64 >>= 1;
    }
    return -1;
}
 
// Driver Code
$x = 122;
$y = 2147483678;
 
if (isRotation($x, $y))
    echo "yes" ,"\n";
else
    echo "no" ,"\n";
 
// This code is contributed by aj_36
?>

Javascript




<script>
 
// javascript program to check if two numbers are bit rotations
// of each other.
 
// function to check if two numbers are equal
// after bit rotation
function isRotation(x, y)
{
 
    // x64 has concatenation of x with itself.
    var x64 = x | (x << 32);
    while (x64 >= y)
    {
     
        // comparing only last 32 bits
        if (x64 == y) {
            return true;
        }
 
        // right shift by 1 unit
        x64 >>= 1;
    }
    return false;
}
 
// driver code to test above function
var x = 122;
var y = 2147483678;
 
if (isRotation(x, y) == false) {
    document.write("Yes");
} else {
    document.write("No");
}
 
// This code is contributed by 29AjayKumar
</script>

Output

yes

Time Complexity: O(logn)
Auxiliary Space: O(1)

This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Last Updated : 13 Jul, 2022
Like Article
Save Article
Similar Reads
Related Tutorials