c – 使用位操作查找最小值
任何人都可以向我解释以下代码行.它用于查找最少两个数字.
int min(int x,int y) { return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1))); } 提前致谢. 解决方法
对于许多int组合来说,这个代码很不幸失败,因为它的价值非常低. @harold一个好的编译器会识别x<你? x:y;并生成正确和快速的代码. @David C. Rankin 确定它是如何工作的并不像它失败那样有趣. >未定义的行为:如果x-y溢出,兼容的编译器可能会生成任何输出 – 甚至崩溃.优化编译器利用这一点使新程序员感到懊恼. 对于x,y的许多组合,OP的代码失败 – 121个测试案例中的31个失败 – 见下文. “It accomplishes this by an arithmetic shift”是实现定义的行为. x-y的潜在溢出是未定义的行为.如果不解决这些问题,任何答案都不完整 拐角情况“it works for any other sizes”通常是正确的,但罕见的平台可能在int中使用填充,使sizeof(int)* CHAR_BIT – 1成问题. #include <stdio.h> int minz(int x,int y) { return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1))); } void testmin(int x,int y) { static unsigned count = 0; static unsigned fail = 0; int min0 = x < y ? x: y; int min1 = minz(x,y); count++; if (min0 != min1) { fail++; printf("%u/%u min(%d,%d)--> %d,should be %dn",fail,count,x,y,min1,min0); } } int main(void) { const int i[]={INT_MIN,INT_MIN+1,INT_MIN+2,-2,-1,1,2,INT_MAX-2,INT_MAX-1,INT_MAX}; int x,y; for (x=0; x<sizeof i/sizeof i[0]; x++) { for (y=0; y<sizeof i/sizeof i[0]; y++) { testmin(i[x],i[y]); } } } 输出(故障) 1/7 min(-2147483648,1)--> 1,should be -2147483648 2/8 min(-2147483648,2)--> 2,should be -2147483648 3/9 min(-2147483648,2147483645)--> 2147483645,should be -2147483648 4/10 min(-2147483648,2147483646)--> 2147483646,should be -2147483648 5/11 min(-2147483648,2147483647)--> 2147483647,should be -2147483648 6/19 min(-2147483647,should be -2147483647 7/20 min(-2147483647,should be -2147483647 8/21 min(-2147483647,should be -2147483647 9/22 min(-2147483647,should be -2147483647 10/31 min(-2147483646,should be -2147483646 11/32 min(-2147483646,should be -2147483646 12/33 min(-2147483646,should be -2147483646 13/44 min(-2,should be -2 14/56 min(0,-2147483648)--> 0,should be -2147483648 15/67 min(1,-2147483648)--> 1,should be -2147483648 16/68 min(1,-2147483647)--> 1,should be -2147483647 17/78 min(2,-2147483648)--> 2,should be -2147483648 18/79 min(2,-2147483647)--> 2,should be -2147483647 19/80 min(2,-2147483646)--> 2,should be -2147483646 20/89 min(2147483645,-2147483648)--> 2147483645,should be -2147483648 21/90 min(2147483645,-2147483647)--> 2147483645,should be -2147483647 22/91 min(2147483645,-2147483646)--> 2147483645,should be -2147483646 23/100 min(2147483646,-2147483648)--> 2147483646,should be -2147483648 24/101 min(2147483646,-2147483647)--> 2147483646,should be -2147483647 25/102 min(2147483646,-2147483646)--> 2147483646,should be -2147483646 26/103 min(2147483646,-2)--> 2147483646,should be -2 27/111 min(2147483647,-2147483648)--> 2147483647,should be -2147483648 28/112 min(2147483647,-2147483647)--> 2147483647,should be -2147483647 29/113 min(2147483647,-2147483646)--> 2147483647,should be -2147483646 30/114 min(2147483647,-2)--> 2147483647,should be -2 31/115 min(2147483647,-1)--> 2147483647,should be -1 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |