发现两个数的最大值,但没有使用的if-else或任何其他比较运算符 [英] Find the maximum of two numbers without using if-else or any other comparison operator

查看:230
本文介绍了发现两个数的最大值,但没有使用的if-else或任何其他比较运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查找两个数中的最大值。你不应该使用的if-else或任何其他比较操作。我发现在网上布告栏这个问题,所以我想我应该问的StackOverflow

例 输入:5,10 输出:10

我发现这个解决方案,有人可以帮助我了解code这些行

  INT GetMax的(INT A,INT B){
    INT C = A  -  B;
    时int k =(c取代;> 31)及为0x1;
    INT MAX =一 -  K * C;
    返回最大值;
}
 

解决方案

  INT GetMax的(INT A,INT B){
    INT C = A  -  B;
    时int k =(c取代;> 31)及为0x1;
    INT MAX =一 -  K * C;
    返回最大值;
}
 

让我们来剖析这一点。这第一行是简单的 - 它存储的 A B 的区别。这个值是负数,如果 A< b 和非负除外。

在下一行:

  INT K =(C>> 31)及为0x1;
 

这个想法是检查的 C 的值是负的。在几乎所有的现代计算机,数字存储在一个所谓的格式的二的补的,其中的数字的最高位为0,如果数是正的和1,如果数是负的。此外,大多数int为32位。 (C>> 31)转移的数量下降了31位,留在现场的最低位数字的最高位。采取这种数量和AND运算是下一步1(其二进制重新presentation为0,除了随处可见的最后一位),删除所有较高位,只是给你的最低位。由于 c取代的最低位;> 31 C 的最高位,该读取的最高位C 为任0或1日以来的最高位为1当且仅当 C 1,这是检查是否 C 的方法是负(1)或阳性(0)。以上所述, K 结合这个推理是1,如果 A< b ,否则为0。

的最后一步是要做到这一点:

  INT最大=一 -  K * C;
 

如果 A< b ,然后 K == 1 K * C = C = A - B

  A  -  K * C = A  - (A  -  B)= A  -  A + B =
 

这是正确的最大值,因为 A< b 。否则,如果 A> = B ,然后 K == 0

  A  -  K * C = A  -  0 =一
 

这也是正确的最大值。

Find the maximum of two numbers. You should not use if-else or any other comparison operator. I found this question on online bulletin board, so i thought i should ask in StackOverflow

EXAMPLE Input: 5, 10 Output: 10

I found this solution, can someone help me understand these lines of code

int getMax(int a, int b) {  
    int c = a - b;  
    int k = (c >> 31) & 0x1;  
    int max = a - k * c;  
    return max;  
}

解决方案

int getMax(int a, int b) {
    int c = a - b;
    int k = (c >> 31) & 0x1;
    int max = a - k * c;
    return max;
}

Let's dissect this. This first line is straightforward - it stores the difference of a and b. This value is negative if a < b and is nonnegative otherwise.

In the next line:

int k = (c >> 31) & 0x1;

the idea is to check if the value of c is negative. In virtually all modern computers, numbers are stored in a format called two's complement in which the highest bit of the number is 0 if the number is positive and 1 if the number is negative. Moreover, most ints are 32 bits. (c >> 31) shifts the number down 31 bits, leaving the highest bit of the number in the spot for the lowest bit. The next step of taking this number and ANDing it with 1 (whose binary representation is 0 everywhere except the last bit) erases all the higher bits and just gives you the lowest bit. Since the lowest bit of c >> 31 is the highest bit of c, this reads the highest bit of c as either 0 or 1. Since the highest bit is 1 iff c is 1, this is a way of checking whether c is negative (1) or positive (0). Combining this reasoning with the above, k is 1 if a < b and is 0 otherwise.

The final step is to do this:

int max = a - k * c;

If a < b, then k == 1 and k * c = c = a - b, and so

a - k * c = a - (a - b) = a - a + b = b

Which is the correct max, since a < b. Otherwise, if a >= b, then k == 0 and

a - k * c = a - 0 = a

Which is also the correct max.

这篇关于发现两个数的最大值,但没有使用的if-else或任何其他比较运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆