Given an integer N represented as a binary representation of X = 16 bits. We are also given a number ‘m’ and a character c which is either L or R. The task is to determine a number M that is generated after cyclically shifting the binary representation of N by m positions either left if c = L or right if c = R.
Examples:
Input : N = 7881, m = 5, c = L
Output : 55587
Explanation:
N in binary is 0001 1110 1100 1001 and shifting it left by 5 positions, it becomes 1101 1001 0010 0011 which in the decimal system is 55587.Input : N = 7881, m = 3, c = R
Output : 9177
Explanation:
N in binary is 0001 1110 1100 1001 and shifted 3 positions to right, it becomes 0010 0011 1101 1001 which in the decimal system is 9177.
Approach:
To solve the problem mentioned above we observe that we have to right shift the number by m if the char is R, else we will do a left shift by m if the char is L where left shifts is equivalent to multiplying a number by 2, right shifts is equivalent to dividing a number by 2.
Below is the implementation of the above approach:
# Python implementation to make # Cyclic shifts of integer N by another integer m def Count(N, count, turn): # Convert N into hexadecimal number and # remove the initial zeros in it N = hex(int(N)).split('x')[-1] # Convert hexadecimal term binary # string of length = 16 with padded 0s S = ( bin(int(N, 16))[2:] ).zfill(16) # rotate the string by a specific count if(turn == 'R'): S = (S[16 - int(count) : ] + S[0 : 16 - int(count)]) else: S = (S[int(count) : ] + S[0 : int(count)]) # Convert the rotated binary string # in decimal form, here 2 means in binary form. print(int(S, 2)) # driver code N = 7881count = 5turn = 'L' Count(N, count, turn) |
55587
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count number of ways to convert string S to T by performing K cyclic shifts
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Cyclic Redundancy Check and Modulo-2 Division
- Cyclic Iterator for K variable length vectors
- Sort permutation of N natural numbers using triple cyclic right swaps
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Find missing number in another array which is shuffled copy
- Minimum swaps required to convert one binary string to another
- Number of sub-strings which are anagram of any sub-string of another string
- Sort the array of strings according to alphabetical order defined by another string
- Minimum cost to traverse from one index to another in the String
- Check if one string can be converted to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Check if a string is a scrambled form of another string
- Check if Array elements can be maximized upto M by adding all elements from another array
- Convert one array to another using adjacent swaps of elements
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimize cost to convert a given matrix to another by flipping columns and reordering rows
- Count set bits in an integer
- Compute the integer absolute value (abs) without branching
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

