如何找到最高温度,而无需使用移

问题描述:

只使用如何找到最高温度,而无需使用移

! ~ &^| + 

我如何才能知道一个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的

+0

什么是TMAX?最大的无符号(或有符号)整数? – GWW

+2

我想你需要详细说明什么是TMax。 – NPE

+0

最大二进制补码数。对不起,不详细。 – David

也许这样的事情? 0x7FFFFFFF是最大正符号32位二进制补码数。

int isTMax(int x){ 
    return !(x^0x7FFFFFFF); 
} 

我不确定,您可能需要将其转换为未签名才能工作。

+0

铸造是不允许的。 – David

+0

这个作品非常完美,非常感谢你,我不知道为什么我试图去过时。 – David

+0

这个答案可以接受吗?我假设常量也是不允许的。如果是这样,那么是的,这是最好的解决方案。 –

据我所知,目前还没有办法确定一个特定值是签署的类型的最大值,而不知道该类型的最大值并进行直接比较。这是因为签名的表达式在溢出时遇到未定义的行为。如果对你的问题有答案,这意味着存在一个在SO上浮动一段时间的严重问题的答案:如何以编程方式确定给定签名类型的最大值。

+0

溢出未处理,所以只有32位将被表示,如果它超过了它的丢失。 – David

+0

不,它调用未定义的行为,并且不能假定程序输出的任何内容。谁提出这项任务显然不知道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; 
}