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