Python3 Program to Rotate bits of a number Last Updated : 06 Sep, 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. INTRODUCTION:In this Python program, we will learn how to rotate the bits of a given number. Bit rotation, also known as bit shifting, is a technique for rotating the bits of a binary number to the left or right. This can be useful in a variety of contexts, such as in computer science and cryptography.The program will take an integer input and a rotation distance, and will output the resulting rotated number. The program will use bit shifting and bitwise OR operations to accomplish the rotation.We will begin by declaring the necessary variables and prompting the user for input. Next, we will use bit shifting and bitwise OR to rotate the bits of the input number according to the specified rotation distance. Finally, we will output the resulting rotated number to the console.EXAMPLE 1: Python def rotate_bits(num, rot): return (num << rot) & 0xFFFFFFFF num = int(input("Enter a number: ")) rot = int(input("Enter the number of positions to rotate: ")) result = rotate_bits(num, rot) print("The result of the rotation is:", result) Example: 2Let 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. Python3 # Python3 code to # rotate bits of number INT_BITS = 32 # Function to left # rotate n by d bits def leftRotate(n, 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) return (n << d)|(n >> (INT_BITS - d)) # Function to right # rotate n by d bits def rightRotate(n, 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) return (n >> d)|(n << (INT_BITS - d)) & 0xFFFFFFFF # Driver program to # test above functions n = 16 d = 2 print("Left Rotation of",n,"by" ,d,"is",end=" ") print(leftRotate(n, d)) print("Right Rotation of",n,"by" ,d,"is",end=" ") print(rightRotate(n, d)) # This code is contributed by # Smitha Dinesh Semwal Output : Left Rotation of 16 by 2 is 64Right Rotation of 16 by 2 is 4Time 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 Python3 Program to Rotate bits of a number kartik Follow Improve Article Tags : Bit Magic Python Python Programs DSA rotation +1 More Practice Tags : Bit Magicpython Similar Reads Python3 Program to Rotate digits of a given number by K INTRODUCTION:One important point to consider when working with the algorithm to rotate the digits of a given number by k positions is the time complexity.If we were to implement this algorithm using the approach shown in the previous example, the time complexity would be O(n), where n is the number 4 min read Python3 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 Python Program to Reverse a Number We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve 3 min read Python Program to Subtract Two Binary Numbers We are given two binary numbers, num1 and num2 and we have to subtract these two binary numbers and return the result. In this article, we will see how we can subtract two binary numbers in Python.Examples:Input: a = "1110" , b = "0110"Output: "1000"Explanation: We can see that when "1110" is subtra 3 min read Python Program to Multiply Two Binary Numbers Given two binary numbers, and the task is to write a Python program to multiply both numbers. Example: firstnumber = 110 secondnumber = 10 Multiplication Result = 1100 We can multiply two binary numbers in two ways using python, and these are: Using bin() functions andWithout using pre-defined funct 2 min read Python3 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: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations are 2 min read 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 info 3 min read Python3 Program for Count rotations divisible by 8 Given a large positive number as string, count all rotations of the given number which are divisible by 8.Examples: Input: 8Output: 1Input: 40Output: 1Rotation: 40 is divisible by 8 04 is not divisible by 8Input : 13502Output : 0No rotation is divisible by 8Input : 43262488612Output : 4Approach: For 3 min read Python program to convert Base 4 system to binary number Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1: 3 min read Python program to convert ASCII to Binary We are having a string s="Hello" we need to convert the string to its ASCII then convert the ASCII to Binary representation so that the output becomes : 01001000 01100101 01101100 01101100 0110111. To convert an ASCII string to binary we use ord() to get the ASCII value of each character and format( 2 min read Like