Tuesday, November 26, 2024
Google search engine
HomeLanguagesPython3 Program to Check if two numbers are bit rotations of each...

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

Given two positive integers x and y, check if one integer is obtained by rotating bits of other.
 

Input constraint: 0 < x, y < 2^32 

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.
More information on bit rotation can be found here
Example 1 : 
 

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

Example 2 : 
 

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

 

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 it 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 concatenation of x to x ie.. 
x64 has first 32 bits same as bits of x and last 32 bits are also same as bits of x64.
Then we keep on shifting x64 by 1 on 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 combination due to rotation.
Here is implementation of above algorithm.
 

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) :
         
        # comapring 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


Output

yes

Time Complexity: O(log2n), where n is the given number.
Auxiliary Space: O(1)

Please refer complete article on Check if two numbers are bit rotations of each other or not for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments