Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4071906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:48:59+00:00 2026-05-20T16:48:59+00:00

I am preparing for a programming interview. So, I tried to solve some of

  • 0

I am preparing for a programming interview. So, I tried to solve some of the outdated interview questions just to get prepared for the coming interview. I was stuck in solving the following question:

A cyclic right shift of 30-bit unsigned integer X is a number obtained from shifting all bits of a binary representation of X right by one position and moving the most significant bit to the least significant bit. Precisely, if a binary representation of X is

X29 X28 … X2 X1 X0

the binary representation of X’s right cyclic shift is

X28 … X2 X1 X0 X29

For example, given the number X (intra-digit spaces added for readability):

00 0000 0000 0000 0000 0100 1100 0001BIN = 1217DEC

its right cyclic shift is

00 0000 0000 0000 0000 1001 1000 0010BIN = 2434DEC.

The cyclic shift operation may be repeated, for example given the same value of X, its right cyclic shift by 3 is:

00 0000 0000 0000 0010 0110 0000 1000BIN = 9736DEC

Write a function

int largest_right_cyclic_shift(int n);

which given a 30-bit unsigned integer X finds its right cyclic shift which has the maximum value. For example, the right cyclic shift by 52 of the value X=1217DEC is 809500676DEC, and this is the largest possible 30-bit right cyclic shift of X:

11 0000 0100 0000 0000 0000 0000 0100BIN = 809500676DEC

Consequently, given X=1217, the function should return 52. If there are many right cyclic shift yielding the maximum value, the function should return ANY of them. You may assume that the argument X is always a 30-bit unsigned integer.

This is my answer:

#include <algorithm>  
int largest_right_cyclic_shift ( int n ) {  
    long m = n;  
    long max = n;  
    int shift = 0;  
    for (int i = 1; i < 30; i++){  
        m = n << i;  
        if (m > max){   
             max = m;  
             shift = i;  
         }  
     }  
     return shift;  
}    

The expected answer for input number 1217 is 22. But the above code gave me a value of 23. I know the mistake is because I am representing the input as 32 bit integers, while in the question it is specified that I have to represent the input as 30 bit integers. Using 32 bit integers in representing 30 bit integers will leave the 2 left most digits on the left to be 0. Therefore when we do a left shift operator it will shift the bit 31, instead of shifting bit 29.

What is the best way to solve this problem? I am sure that the solution is simple, it just need some knowledge about bit shifting.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:49:00+00:00Added an answer on May 20, 2026 at 4:49 pm

    What is the best way to solve this problem?

    m = (n << i) & 0x3fffffff;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am preparing for a programming competition in witch we solve programming problems in
I'm preparing for a job interview. I was stuck at one of the binary
I am preparing a lecture on files for the more junior students in programming
I am preparing myself for programming competitions and i would like to know how
I'm new to programming, but I'm preparing to write a Java program. As I'm
I'm preparing some educational/training material with respect to Unit Testing, and want to double
I'm a beginner in iPhone programming. I am preparing an iPhone app where I
I'm preparing to deploy my Django app and I noticed that when I change
I've been preparing to deliver a presentation on SQL Server Compact Edition 3.5 (SP1
My application is preparing a cover-sheet for submitting documents by fax. I identify the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.