如何在不使用班次的情况下找到 TMax
仅使用
! ~ & ^ | +
如何确定 32 位数字是否为 TMax?
TMax 是最大的二进制补码数。
到目前为止,我的想法是:
int isTMax(int x)
{
int y = 0;
x = ~x;
y = x + x;
return !y;
}
这只是我尝试过的众多不成功的事情之一,但我就是想不出 TMax 的属性可以让我恢复 TMax。就像与所有其他整数相比,将 tmax 添加到自身将是唯一的。
这是实际的问题:
/*
* isTMax - return 1 if x is the maximum, two's complement number,
* and 0 return otherwise.
* Legal ops: ! ~ & ^ | +
* Max ops: 10
* Rating: 1
*/
int isTMax(int x) {
int y = 0;
x = ~x;
y = x + x;
return !y;
}
int 是 32 位,因此最大签名可能是 0x7FFFFFFF
Using ONLY
! ~ & ^ | +
How can I find out if a 32 bit number is TMax?
TMax is the maximum, two's complement number.
My thoughts so far have been:
int isTMax(int x)
{
int y = 0;
x = ~x;
y = x + x;
return !y;
}
That is just one of the many things I have unsuccessfully have tried but I just cant think of a property of TMax that would give me TMax back. Like adding tmax to itself would be unique compared to all the other integers.
Here is the actual problem:
/*
* isTMax - return 1 if x is the maximum, two's complement number,
* and 0 return otherwise.
* Legal ops: ! ~ & ^ | +
* Max ops: 10
* Rating: 1
*/
int isTMax(int x) {
int y = 0;
x = ~x;
y = x + x;
return !y;
}
int is 32 bits so the max signed would probably be 0x7FFFFFFF
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
据我所知,如果不知道该类型的最大值并进行直接比较,就无法确定特定值是否是有符号类型的最大值。这是因为签名表达式在溢出时会遇到未定义的行为。如果你的问题有答案,那就意味着存在一个严重的未解决问题的答案,这个问题已经在 SO 上流传了一段时间:如何以编程方式确定给定签名类型的最大值。
As far as I know, there is no way to determine if a particular value is the max value of a signed type without already knowing the maximum value of that type and making a direct comparison. This is because signed expressions experience undefined behavior on overflow. If there were an answer to your question, it would imply the existence of an answer to a serious unsolved problem that's been floating around on SO for some time: how to programmatically determine the max value for a given signed type.
花3个小时回答这个问题。我知道这来自 csapp 的数据实验室,其最新要求是
所以,移位运算符 (
<<
/>>
和0x7FFFFFFF
现在禁止接受接受的答案)以下是我的方式:
TDD风格:
返回应该是
0
或1
。在 c 中,所有非零的!
将返回0
。所以!
是必须的,否则我们不能保证所有数字都得到0
。第一次天真的尝试:
因为
0b0111111...1
(又名2147483647
)是唯一应该使isTmax
返回1
的参数code> 和2147483647 + 1
应该是10000000...0
(又名-2147483648
)0b011111111...1 异或 0b1000000000...0
为0b11111111111...111
。因为我们必须使用!
,所以我们希望看到的是0
(又名0b0000000000000...0
)。显然,将逻辑not(又名!
)应用于0b1111111...1
),那么我们将得到0b000000000000
):让我们打印它
现场演示
不错,只是< code>-1 无法按我们的预期工作。
第二次尝试:
让我们比较
-1
和2147483647
11111111111111111111111111111111
01111111111111111111111111111111
我们可以找到
-1 + 1 = 0
,而2147483647 + 1 = -2147483648
。再次强调,我们想要的是 diff-1
和2147483647
,因为它们都返回1
,如上所示。回顾一下c中的逻辑非属性:所有非零都会返回0,所以!-2147483648 == 0
和! (-1 + 1) != 0 。只需将
x ^ (x + 1)
(x
) 的左侧部分修改为x + !(x + 1)
即可。如果 x 为2147483647
,则x + !(x + 1)
将等于x
。再次运行:
现场演示
完成!
Spend 3 hours on this question. I know this comes from csapp's data lab and its newest requirement is
So, shift operator (
<<
/>>
and0x7FFFFFFF
from the accepted answer is forbidden now)Below is my way:
TDD-style:
the return should be either
0
or1
. In c,!
on all nonzero will return0
. So!
is a must, otherwise, we cannot guarantee to get0
for all numbers.First naive try:
because
0b0111111...1
(aka2147483647
) is the only argument that should makeisTmax
return1
and2147483647 + 1
should be10000000...0
(aka-2147483648
)0b011111111...1 xor 0b1000000000...0
is0b11111111111...111
. Because we must use!
, what we hope to see is0
(aka0b0000000000000...0
). Obviously, apply logic not(aka!
) to0b1111111...1
), then we will get0b000000000000
):let's printf it
live demo
Not bad, only
-1
doesn't work as we expected.second try:
Let's compare
-1
and2147483647
11111111111111111111111111111111
01111111111111111111111111111111
We can find
-1 + 1 = 0
while2147483647 + 1 = -2147483648
. Emphasize again, what we want is diff-1
and2147483647
, because both of them return1
as above shows. Look back to the property of logic not in c: all nonzero will return 0, so!-2147483648 == 0
and!(-1 + 1) != 0
. Just modify left part ofx ^ (x + 1)
(x
) intox + !(x + 1)
. If x is2147483647
,x + !(x + 1)
will equal tox
.Run again:
live demo
Done!
无班次解决方案。意识到利用
!(0b01111 + 1 = 0b10000) = 0
和!(0b11111 + 1 = 0b00000) = 1
的属性特别棘手。这个问题花了我很长时间。No shifts solution. Realizing to take advantage of the property that
!(0b01111 + 1 = 0b10000) = 0
and!(0b11111 + 1 = 0b00000) = 1
is particularly tricky. This problem took me a long time.也许是这样的?
0x7FFFFFFF 是最大正符号 32 位二进制补码数。
我不确定,您可能需要将其转换为未签名才能正常工作。
Something like this perhaps?
0x7FFFFFFF is the maximum positive signed 32 bit two's complement number.
I am not sure, you may need to cast it to unsigned for it to work.
如果是 Tmax : 011111.....
然后我们将其与 10000 进行异或...
我们得到 11111...
然后我们 ~ 得到所有 0 = 0 , !0 我们得到 1:
if it is Tmax : 011111.....
then we xor it with 10000....
we get 11111....
then we ~ to get all 0s = 0 , !0 we get 1:
这是基于最新要求的简短答案(即,没有变化,也没有大的常数)。
最终答案。
Here's a shorter answer based on the newest requirements (i.e., no shifts and no big constants).
Final answer.