问题描述
仅使用
! ~ & ^ | +
如何确定32位数字是否为TMax?
How can I find out if a 32 bit number is TMax?
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;
}
那只是我尝试过的许多尝试中的一件事,但是我只是想不起来TMax的一个特性,它会使我回到TMax.像将tmax添加到自身相比,与所有其他整数相比,它是唯一的.
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.
这是实际的问题:
/*
* 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
int is 32 bits so the max signed would probably be 0x7FFFFFFF
推荐答案
也许是这样的?0x7FFFFFFF是最大正符号32位二进制补码.
Something like this perhaps?0x7FFFFFFF is the maximum positive signed 32 bit two's complement number.
int isTMax(int x){
return !(x ^ 0x7FFFFFFF);
}
我不确定,您可能需要将其强制转换为未签名状态.
I am not sure, you may need to cast it to unsigned for it to work.
这篇关于如何在不使用班次的情况下找到TMax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!