如何找到最高温度,而无需使用移
! ~ &^| +
我如何才能知道一个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的
据我所知,目前还没有办法确定一个特定值是签署的类型的最大值,而不知道该类型的最大值并进行直接比较。这是因为签名的表达式在溢出时遇到未定义的行为。如果对你的问题有答案,这意味着存在一个在SO上浮动一段时间的严重问题的答案:如何以编程方式确定给定签名类型的最大值。
溢出未处理,所以只有32位将被表示,如果它超过了它的丢失。 – David
不,它调用未定义的行为,并且不能假定程序输出的任何内容。谁提出这项任务显然不知道C ... –
#include <stdio.h>
#include <stdlib.h>
int test(int n) {
return !(n & 0x80000000) & !~(n | (n + 1));
}
// or just effectively do a comparison
int test2(int n) {
return !(n^0x7fffffff);
}
int main(int ac, char **av) {
printf("is%s TMax\n", test(atoi(av[1])) ? "" : " not");
return 0;
}
int isTmax(int x) {
//add one to x if this is Tmax. If this is Tmax, then this number will become Tmin
//uses Tmin = Tmax + 1
int plusOne = x + 1;
//add to x so desired input becomes 0xFFFFFFFF, which is Umax and also -1
//uses Umax = 2Tmax + 1
x = x + plusOne;
plusOne = !(plusOne);
//is x is 0xffffffff, then this becomes zero when ~ is used
x = ~x;
x = x | plusOne;
x = !x;
return x;
}
什么是TMAX?最大的无符号(或有符号)整数? – GWW
我想你需要详细说明什么是TMax。 – NPE
最大二进制补码数。对不起,不详细。 – David