Cyclic shifts of integer N by another integer m
Last Updated :
17 Mar, 2023
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:
C++
#include<bits/stdc++.h>
using namespace std;
void Count( int N, int m, char turn)
{
string str = bitset<16>(N).to_string();
m%=( int )str.size();
if (turn== 'R' )
str = str.substr(( int )str.size()-m) + str.substr(0,( int )str.size());
else
str = str.substr(m) + str.substr(0,m);
cout << bitset<16> (str).to_ulong() << '\n' ;
}
int main()
{
int N = 7881 ;
int m = 5;
char turn = 'L' ;
Count(N,m,turn);
}
|
Java
import java.math.*;
public class CyclicShift {
static int N = 7881 ;
static int count = 5 ;
static char turn = 'L' ;
public static void main(String[] args) {
Count(N, count, turn);
}
static void Count( int N, int count, char turn) {
String N_hex = Integer.toHexString(N);
String S = String.format( "%16s" , Integer.toBinaryString(Integer.parseInt(N_hex, 16 ))).replace( ' ' , '0' );
count = count % S.length();
if (turn == 'R' ) {
S = S.substring(S.length() - count) + S.substring( 0 , S.length() - count);
} else {
S = S.substring(count) + S.substring( 0 , count);
}
System.out.println(Integer.parseInt(S, 2 ));
}
}
|
Python3
def Count(N, count, turn):
N = hex ( int (N)).split( 'x' )[ - 1 ]
S = ( bin ( int (N, 16 ))[ 2 :] ).zfill( 16 )
if (turn = = 'R' ):
S = (S[ 16 - int (count) : ] + S[ 0 : 16 - int (count)])
else :
S = (S[ int (count) : ] + S[ 0 : int (count)])
print ( int (S, 2 ))
N = 7881
count = 5
turn = 'L'
Count(N, count, turn)
|
Javascript
function Count(N, count, turn) {
N = N.toString(16).replace(/^0+/, '' );
let S = parseInt(N, 16).toString(2).padStart(16, '0' );
if (turn === 'R' ) {
S = S.slice(16 - count) + S.slice(0, 16 - count);
} else {
S = S.slice(count) + S.slice(0, count);
}
console.log(parseInt(S, 2));
}
const N = 7881;
const count = 5;
const turn = 'L' ;
Count(N, count, turn);
|
C#
using System;
public class CyclicShift {
static int N = 7881;
static int count = 5;
static char turn = 'L' ;
public static void Main( string [] args) {
Count(N, count, turn);
}
static void Count( int N, int count, char turn) {
string N_hex = N.ToString( "X" );
string S = Convert.ToString(Convert.ToInt32(N_hex, 16), 2).PadLeft(16, '0' );
count = count % S.Length;
if (turn == 'R' ) {
S = S.Substring(S.Length - count) + S.Substring(0, S.Length - count);
} else {
S = S.Substring(count) + S.Substring(0, count);
}
Console.WriteLine(Convert.ToInt32(S, 2));
}
}
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...