Elegantly determine if more than one boolean is “t

2019-01-13 05:26发布

I have a set of five boolean values. If more than one of these are true I want to excecute a particular function. What is the most elegant way you can think of that would allow me to check this condition in a single if() statement? Target language is C# but I'm interested in solutions in other languages as well (as long as we're not talking about specific built-in functions).

One interesting option is to store the booleans in a byte, do a right shift and compare with the original byte. Something like if(myByte && (myByte >> 1)) But this would require converting the separate booleans to a byte (via a bitArray?) and that seems a bit (pun intended) clumsy... [edit]Sorry, that should have been if(myByte & (myByte - 1)) [/edit]

Note: This is of course very close to the classical "population count", "sideways addition" or "Hamming weight" programming problem - but not quite the same. I don't need to know how many of the bits are set, only if it is more than one. My hope is that there is a much simpler way to accomplish this.

22条回答
欢心
2楼-- · 2019-01-13 05:57

I was going to write the Linq version, but five or so people beat me to it. But I really like the params approach to avoid having to manually new up an array. So I think the best hybrid is, based on rp's answer with the body replace with the obvious Linqness:

public static int Truth(params bool[] booleans)
{
    return booleans.Count(b => b);
}

Beautifully clear to read, and to use:

if (Truth(m, n, o, p, q) > 2)
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-13 05:59

If there were millions instead of just 5 you could avoid Count()and do this instead ...

public static bool MoreThanOne (IEnumerable<bool> booleans)
{
    return booleans.SkipWhile(b => !b).Skip(1).Any(b => b);
}
查看更多
做个烂人
4楼-- · 2019-01-13 05:59
if (NumberOfTrue(new List<bool> { bool1, bool2, bool3, bool4 }) >= 2)
{
    // do stuff
}

int NumberOfTrue(IEnumerable<bool> bools)
{
    return bools.Count(b => b);
}
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-13 05:59

You mentioned

One interesting option is to store the booleans in a byte, do a right shift and compare with the original byte. Something like if (myByte && (myByte >> 1))

I don't think that expression will give you the result you want (at least using C semantics, since the expression is not valid C#):

If (myByte == 0x08), then the expression will return true even though there's only one bit set.

If you meant "if (myByte & (myByte >> 1))" then if (myByte == 0x0a) the expression will return false even though there are 2 bits set.

But here are some techniques for counting the number of bits in a word:

Bit Twiddling Hacks - Counting bits

A variation you might consider is to use Kernighan's counting method, but bail out early since you only need to know if there's more than one bit set:

int moreThanOneBitSet( unsigned int v)
{
    unsigned int c; // c accumulates the total bits set in v

    for (c = 0; v && (c <= 1); c++)
    {
      v &= v - 1; // clear the least significant bit set
    }

    return (c > 1);
}

Of course, using a lookup table's not a bad option either.

查看更多
登录 后发表回答