Java Program to Check if two numbers are bit rotations of each other or not
Last Updated :
11 Jul, 2022
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.
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
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!
Similar Reads
Javascript Program to Check if two numbers are bit rotations of each other or not 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
3 min read
Check if two numbers are bit rotations of each other or not 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 :
6 min read
Javascript Program to Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABAOutput : TrueInput : GEEKS, EKSGEOutput : TrueWe have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (longest
2 min read
Check if Strings Are Rotations of Each Other Given two string s1 and s2 of same length, the task is to check whether s2 is a rotation of s1.Examples: Input: s1 = "abcd", s2 = "cdab"Output: trueExplanation: After 2 right rotations, s1 will become equal to s2.Input: s1 = "aab", s2 = "aba"Output: trueExplanation: After 1 left rotation, s1 will be
15+ min read
Javascript Program to Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str
4 min read