我有一组五个布尔值。如果不止一个是真的,我想执行一个特定的函数。您能想到的允许我在一个 if ()语句中检查这个条件的最优雅的方法是什么?目标语言是 C # ,但我也对其他语言的解决方案感兴趣(只要我们不讨论特定的内置函数)。
一个有趣的选择是将布尔值存储在一个字节中,进行右移位并与原始字节进行比较。类似于 if(myByte && (myByte >> 1))
,但是这需要将单独的布尔值转换为一个字节(通过 bitArray?)这似乎有点(双关语的意思)笨拙... 抱歉,应该是 if(myByte & (myByte - 1))
[/编辑]
注意: 这当然非常接近经典的“人口计数”、“横向加法”或“汉明重量”编程问题——但并不完全相同。我不需要知道设置了多少比特,只要它不止一个。我希望有一个更简单的方法来实现这一点。