计算给出最大值的 30 位整数中的位移位数

发布于 2024-10-25 06:19:59 字数 1454 浏览 1 评论 0原文

我正在准备编程面试。所以,我试图解决一些过时的面试问题,只是为即将到来的面试做好准备。我一直在解决以下问题:

30 位无符号整数 X 的循环右移是通过将 X 的二进制表示形式的所有位右移一位并将最高有效位移动到最低有效位而获得的数字。准确地说,如果 X 的二进制表示为

X29 X28 ... X2 X1 X0,

则 X 的右循环移位的二进制表示为

X28 ... X2 X1 X0 X29

例如,给定数字 X(为了可读性添加了数字内空格) :

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

其右循环移位为

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

循环移位操作可以重复进行,例如给定相同的X值,其右循环移位3为:

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

写一个函数

intlargest_right_circlic_shift(int n);

给定一个 30 位无符号整数 X,找到具有最大值的右循环移位。例如,值 X=1217DEC 右循环移位 52 为 809500676DEC,这是 X 的最大可能的 30 位右循环移位:

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

因此,给定 X=1217,该函数应返回 52。如果有许多右循环移位产生最大值,则该函数应返回其中的任何一个。您可以假设参数 X 始终是 30 位无符号整数。

这是我的答案:

#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;  
}    

输入数字 1217 的预期答案是 22。但是上面的代码给了我一个值 23。我知道错误是因为我将输入表示为 32 位整数,而在问题中指定了我必须将输入表示为 30 位整数。使用 32 位整数表示 30 位整数将使左边最左边的 2 位数字为 0。因此,当我们执行左移运算符时,它将移位 位 31,而不是移位 >位29

解决这个问题的最佳方法是什么?我确信解决方案很简单,只需要一些有关位移位的知识。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不咬妳我踢妳 2024-11-01 06:19:59

解决这个问题的最佳方法是什么?

m = (n << i) & 0x3fffffff;

What is the best way to solve this problem?

m = (n << i) & 0x3fffffff;
谁许谁一生繁华 2024-11-01 06:19:59

FredOverflow 的评论应该是正确的答案。

def largest(n) :
    mx = n
    p = 0
    for i in range(30) :
        m = n<<i & 0x3fffffff | n>>(30-i)
        if m > mx :
            p = i
            mx = m
    print(mx)
    return p

>>> largest(1217)
809500676
22
>>> 

FredOverflow's comment should be the right answer.

def largest(n) :
    mx = n
    p = 0
    for i in range(30) :
        m = n<<i & 0x3fffffff | n>>(30-i)
        if m > mx :
            p = i
            mx = m
    print(mx)
    return p

>>> largest(1217)
809500676
22
>>> 
£冰雨忧蓝° 2024-11-01 06:19:59

这与 FredOverflow 的第二次尝试相同,尽管我确实添加了一组额外的括号,因为我总是忘记 &| 优先级。

m = ((n << i) & 0x3fffffff) | (n >> (30-i));

1217 测试 (VS2010 SP1) 的值为 22

This is the same as FredOverflow's 2nd attempt, although I did add an extra set of parentheses because I always forget the & and | precedence.

m = ((n << i) & 0x3fffffff) | (n >> (30-i));

Gives me a value of 22 for the 1217 test (VS2010 SP1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文