如何以编程方式返回两个整数的最大值,而无需使用任何比较运营商与不使用,如果,否则,等? [英] How do I programmatically return the max of two integers without using any comparison operators and without using if, else, etc?
本文介绍了如何以编程方式返回两个整数的最大值,而无需使用任何比较运营商与不使用,如果,否则,等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式返回的最大两个整数,而无需使用任何比较操作,并且不使用如果
,其他
等?
How do I programmatically return the maximum of two integers without using any comparison operators and without using if
, else
, etc?
推荐答案
最大://将放MAX(A,B)成
max: // Will put MAX(a,b) into a
a -= b;
a &= (~a) >> 31;
a += b;
和
INT A,B;
分钟://将放MIN(A,B)成
min: // Will put MIN(a,b) into a
a -= b;
a &= a >> 31;
a += b;
这篇关于如何以编程方式返回两个整数的最大值,而无需使用任何比较运营商与不使用,如果,否则,等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文