[英]Comparing two integers without any comparison
是否有可能在没有任何比较的情况下找到两个整数中的最大值? 我找到了一些解决方案:
if(!(a/b)) // if a is less than b then division result will be zero.
{
cout << " b is greater than a";
}
else if (!(a-b)) // we know a is greater than or equal to b now. check whether they are equal.
{
cout << "a and b are equal";
}
else
cout << "a is greater than b";
但是 if(c) 或 if(!c) 是与零的比较。 此外,它不适用于负数。 事实上,我需要一个避免任何 if 语句的解决方案。 相反,我应该使用 switch 语句和算术运算符。 比X。
减去它们并使用讨厌的小技巧检查符号
http://graphics.stanford.edu/~seander/bithacks.html
如果其他程序员知道您住在哪里,请不要在生产代码中这样做。
这是一个没有任何条件分支的有趣版本。
int g = (int)"greater";
int l = (int)"less";
int e = (int)"equal";
int a = 7;
int b = 10;
char *result = (char*)((((a - b) >> 31) & l) | (((b - a) >> 31) & g) | ((~((a - b) | (b - a))) >> 31) & e);
cout << result;
到目前为止,问题中提供的任何样本或任何答案都无法防止被零除。 你到底为什么要避免使用“if”语句? 我怀疑有关 ?: 运算符的作业问题。
cout << "Maximum is: " << ((a>b)?a:b)
我们走了。
没有比较就不可能比较两个数字。 您可以捏造它并进行间接操作,但在一天结束时,您正在比较某些内容。 相信编译器会优化代码并选择最佳操作。
您可能会利用这样一个事实,即计算a - b
的符号取决于哪个数字更大。 这在许多比较的实现中使用。 但我相信你永远无法完全避免比较。 在这种情况下,您至少仍然需要评估处理器上标志标志的内容。
如果您只需要显示较低的数字,您还可以使用算术技巧:
result = ((a + b) - sqrt((a - b) * (a - b))) / 2
编辑erm ……你可以使用switch
吗?
我应该使用 switch 语句和算术运算符。
switch
与 chained if
基本相同,因此它也使用比较。 这听起来好像您确实应该与零进行比较以查看a - b
具有什么符号。
char c;
c=0x3D + (!(b/a) && (a-b)) - (!(a/b) && (a-b));
printf("a %c b",c);
试试这个,测试它,效果很好。
public static int compare(int a, int b)
{
int c = a - b;
return (c >> 31) & 1 ^ 1;
}
我认为这种方法比其他方法更好,您可以使用这种逻辑 c 和 java 这两种编程语言,但是如果 int 是 2 字节,则 int 应该是 4 字节,然后右移 15 字节而不是 31 字节。
enter code here
#include<stdio.h>
main()
{
int a, b;
printf("Enter three numbers\n");
scanf("%d %d", &a, &b);
printf("Largest number is %d \n",findMax( a,b ));
}
int findMax( int x, int y)
{
int z = x - y;
int i = (z >> 31) & 0x1;
printf("i = %d shift = %d \n", i, (z>>31));
int max = x - i * z;
return max;
}
(!(a/b) ? cout << " b is greater than a" : (!(b-a) ? cout << "a and b are equal" : cout << "a is greater than b") : cout << "a is greater than b");
虽然有点乱
编辑:这是作业吗?
我只是看不出有什么好的理由这样做:谁会想在没有“if”的情况下编程?
一个可能的答案是:
( ( a + b ) + abs ( a -b ) ) / 2
我猜“abs”只是在某处隐藏了“if”,就像三元运算符只是“if”的另一个名字一样......
反常的想法:使用函数指针数组。 然后通过一些算术和按位运算获得该数组的索引。
作为一个毫无意义的练习,这里有一种实现cond
函数的方法 - 服务于if
的目的,假设它(和switch
,和?:
)以某种方式从语言中消失了,并且您正在使用 C++0x。
void cond(bool expr, std::function<void ()> ifTrue, std::function<void ()> ifFalse)
{
std::function<void ()> choices[2] = { ifTrue, ifFalse };
choices[expr == false]();
}
例如
cond(x > y,
/*then*/ [] { std::cout << "x is greater than y"; },
/*else*/ [] { std::cout << "x is not greater than y"; });
就像我说的,毫无意义。
在不使用比较/关系运算符的情况下获得最大数
void PrintGreatestNumber(int a, int b)
{
int [] x = new int[] { -1, 0, 1 };
int greatestNumber = ((a+b)+ x[ 1 + ((a-b) >> 31) - (-(a-b) >> 31)] * (a-b)) /2;
Console.WriteLine(greatestNumber);
}
您可以使用函数来检查是否使用 xor 按位运算符相等或不相等。 在这里,您可以将此函数编写为:
int Check(int a, int b){
return (a^b);
}
如果两个整数相同,此函数将返回 0,否则返回 0。
在这里,包含一个示例来理解此功能。
让两个整数作为 a = 1, b= 2
1 的位是 --> 00000001 而 2 的位是 --> 00000010
如果我们在这里应用异或运算,我们将得到结果为 00000000,即整数 0。 因为异或操作是:
1 xor 1 = 0
1 xor 0 = 1
0 xor 1 = 1
0 xor 0 = 0
另一种方法可以通过减去数字来做
int Check(int a, int b)
{
return abs(a-b);
}
这里的逻辑将和以前一样工作。 如果我们得到 0 那么它应该是相等的否则不是!
假设 X 和 Y 是两个输入。 X>Y 将是:((X+Y)+ abs(XY))/2 并且 X<Y 将是:((X+Y)- abs(XY))/2
现在你可以从#include<math.h> 得到abs(),它实际上返回的是绝对值。
干杯!
void greater(int a, int b) {
int c = a - b;
switch(c) {
case 0:
cout << "a and b are equal" << endl;
break;
default:
int d = c & (1<<31);
switch(d) {
case 0:
cout << "a is bigger than b" << endl;
break;
default:
cout << "a is less than b" << endl;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.