Java Program to Rotate bits of a number Last Updated : 08 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at the right end are put back at the left end. Recommended: Please try your approach on PRACTICE first, before moving on to the solution.Example of Rotating bits of a NumberLet n is stored using 8 bits. Left rotation of n = 11100101 by 3 makes n = 00101111 (Left shifted by 3 and first 3 bits are put back in last ). If n is stored using 16 bits or 32 bits then left rotation of n (000...11100101) becomes 00..0011100101000. Right rotation of n = 11100101 by 3 makes n = 10111100 (Right shifted by 3 and last 3 bits are put back in first ) if n is stored using 8 bits. If n is stored using 16 bits or 32 bits then right rotation of n (000...11100101) by 3 becomes 101000..0011100. Program to Rotate Bits of a Number in JavaBelow is the implementation of Bit rotation in Java: Java // Java code to rotate bits // of number class GFG { static final int INT_BITS = 32; /*Function to left rotate n by d bits*/ static int leftRotate(int n, int d) { /* In n<<d, last d bits are 0. To put first 3 bits of n at last, do bitwise or of n<<d with n >>(INT_BITS - d) */ d %= INT_BITS; return (n << d) | (n >> (INT_BITS - d)); } /*Function to right rotate n by d bits*/ static int rightRotate(int n, int d) { /* In n>>d, first d bits are 0. To put last 3 bits of at first, do bitwise or of n>>d with n <<(INT_BITS - d) */ d %= INT_BITS; return (n >> d) | (n << (INT_BITS - d)); } // Driver code public static void main(String arg[]) { int n = 16; int d = 2; System.out.print("Left Rotation of " + n + " by " + d + " is "); System.out.println(leftRotate(n, d)); System.out.print("Right Rotation of " + n + " by " + d + " is "); System.out.println(rightRotate(n, d)); } } Output : Left Rotation of 16 by 2 is 64Right Rotation of 16 by 2 is 4Complexity of the above method:Time Complexity: O(1)Auxiliary Space: O(1) Method for 16 bit NumberBelow is the implementation of the above method: Java import java.io.*; class GFG { public static void main(String[] args) { int N = 28; int D = 2; rotate(N, D); } static void rotate(int N, int D) { // your code here int t = 16; D %= 16; // In case, D > 16, t - D will be negative. int left = ((N << D) | N >> (t - D)) & 0xFFFF; int right = ((N >> D) | N << (t - D)) & 0xFFFF; System.out.println("Left Rotation of " + N + " by " + D + " is "+left); System.out.println("Right Rotation of " + N + " by " + D + " is "+right); } } Output:Left Rotation of 28 by 2 is 112Right Rotation of 28 by 2 is 7Complexity of the above Method:Time Complexity : O(1)Auxiliary Space: O(1) Please refer complete article on Rotate bits of a number for more details! Comment More infoAdvertise with us Next Article Java Program to Rotate bits of a number kartik Follow Improve Article Tags : Bit Magic Java Java Programs DSA rotation +1 More Practice Tags : Bit MagicJava Similar Reads Java Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required 2 min read Java Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 2 min read Java Program to Rotate Matrix Elements A matrix is simply a two-dimensional array. So, the goal is to deal with fixed indices at which elements are present and to perform operations on indexes such that elements on the addressed should be swapped in such a manner that it should look out as the matrix is rotated.For a given matrix, the ta 6 min read Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by " 5 min read Java Program to Rotate Linked List block wise Given a Linked List of length n and block length k rotate in a circular manner towards right/left each block by a number d. If d is positive rotate towards right else rotate towards left. Examples: Input: 1->2->3->4->5->6->7->8->9->NULL, k = 3 d = 1 Output: 3->1->2->6->4->5->9->7->8->NULL Explanati 4 min read Java 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 info 3 min read Java Program to Find Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati 2 min read Java Program to Count rotations divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8 Output: 1 Input: 20 Output: 1 Rotation: 20 is divisible by 4 02 is not divisible by 4 Input : 13502 Output : 0 No rotation is divisible by 4 Input : 43292816 Output : 5 5 rot 3 min read Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek 3 min read Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System 5 min read Like