2
 if (boolThing[0] == true && boolThing[2] == true && !boolThing[1] && !boolThing[3] && !boolThing[4]) { Debug.Log("Hey"); } 

I want to learn different ways on how to do this.

Code does something when 0 and 3 are true while the rest are false.

Is there a 'cleaner' or 'more efficient' way to do this?

1
  • 1
    boolThing[0] == true can be only boolThing[0]. == true is redundant. Commented Jul 2, 2017 at 6:01

3 Answers 3

2

You could do this, Namespace System.Linq in Unity

if(boolThing.SequenceEqual(new bool[] { true, false, true, false, false}) { Debug.Log("Hey"); } 
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need if(boolThing[0]==true). You can just use `if (boolThing[0]))

You can also use binary operators to evaluate several boolean values together.

if((bools[0]&bools[3]) && !(bools[1]|bools[2]|bools[4]|bools[5]) 

You can use a BitArray to easily work with a collection of booleans and it will be much more efficient.

The most efficient method is going to be to use numeric types.

The easiest way to do this would to be to use an enum, with flags. You can use binary operators to set or clear the flags of the enum.

[Flags] public enum MyBoolFlag { None, Bool0 = 1<<0, Bool1 = 1 << 1, Bool2 = 1 << 2, Bool3 = 1 << 3, Bool4 = 1 << 4, Bool5 = 1 << 5, //etc } 

And you use binary operations to set the flags.

MyBoolFlag myFlag = MyBoolFlag.Bool0|MyBoolFlag.Bool1|MyBoolFlag.Bool3; 

In your code, you can now check:

if(myFlag == MyBoolFlag.Bool0|MyBoolFlag.Bool3) { } 

You can define constants for various conditions.

const MyBoolFlag myCondition1 = MyBoolFlag.Bool0|MyBoolFlag.Bool3; 

And then check:

if(myFlag == myCondition1) { } 

You can also do some more legwork and use types such as native types such byte and int directly.

You can use a single byte to save 8 boolean values as flag masks each representing a power of two. A uint32 can save 32 and a uint64 can save 64 booleans.

This has a huge performance boost because a boolean is stored as uint32 which means it uses 4 bytes of data and you can pack 32 booleans in an uint32 when you use the bits ask boolean flags.

Programs will also execute faster because the CPU evaluate myFlag == myCondition1 in a single operation using the native machine word. Otherwise many instructions are needed to put lookup boolean values in an array and compare them. .Net will also do some sanity checks making sure your array isn't null, your indexes aren't out of range and then need to move pointers around to lookup those values.

For convenience you can create a bitarray from an array of booleans and then use it to initialize an array of numeric types with their appropriate flags set.

For example:

bool[] bools = new bool[] { true, false, false, true, false, false }; BitArray bitArray = new BitArray(new bool[] { true, false, false, true, false, false }); int[] array = new int[1]; bitArray.CopyTo(array, 0); int value = array[0]; 

The point of this is 'value' will be 9 if bools[0] = true and bools[3] true.

So your code would cleanly be:

if(value==9) // bools[0]&bools[1]==true; 

You can also create constant masks to check different numerical values to check if different bits are set in your numerical values or to manipulate the bits using binary operations.

You could get more efficiency by setting the numeric values using binary operations.

uint32 myFlag= 1|(1<<3); 

5 Comments

In your example (bools[0]&bools[3]) && !(bools[1]|bools[2]|bools[4]|bools[5]) in the beginning, there is no need for the "single" & and | operators. It is wasteful, just use && and || there.
&& will work but will require three operations to evaluate whether the first group is true but & will require one. With && the first operation checks if bools[0] is true, the second if bools[1] is true and the third being the && operator inside the paranthesis. Using & a binary operation will combine bools[0] and bools[1] into a single result and then evaluate if that is true. You can code this and look at disassembly in VS to verify. There will also be many more jmps in the second half due the use of ||. You can run a loop using both millions of times to verify.
Interesting. In your million loop for verification, are the contents of bools the same each time? And why not use & in the "final" middle operation as well, if you do it inside the parentheses? A mix of short-circuiting and non-short-circuiting looks strange. If I take out the sub-expression bools[1]|bools[2]|bools[4]|bools[5]) as an example, you have to check all the array entries, even if bools[1] is already true. But checking an array entry is extremely fast, so it is a special situation. How long shall the | chain be before || is more efficient?
You are correct the entire expression could be a binary operation. The point is when you have block of code that execute several statements using binary operations it will typically perform faster than statements using the && or || even if they short circuit. The reason is the short circuit operations are actually evaluated as separate IF statements which in assembly becomes a jump statement. jump statements in loops are horrible. This is why programmers will unroll loops because the loop control itself is a jump statement which slows down code execution.
The problem is even more problematic when you are putting the if/jump statements in between index lookup operations because in .Net, unless the code is optimized will not be direct lookup of a pointer to an array the same way it would be in native code.
0

You can put your variables in appropriate lists and then test each List whether it only contains elements of a particular value (true or false):

List<bool> thingsTestedForTrue = new List<bool> { boolThing[0], boolThing[1], boolThing[2] }; List<bool> thingsTestedForFalse = new List<bool> { boolThing[3], boolThing[4], boolThing[5] }; if(thingsTestedForTrue.All(thing => thing == true) && thingsTestedForFalse.All(thing => thing == false) { // All values passed } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.