Quantcast
Channel: Can I check a small array of bools in one go? - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Can I check a small array of bools in one go?

$
0
0

There was a similar question here, but the user in that question seemed to have a much larger array, or vector. If I have:

bool boolArray[4];

And I want to check if all elements are false, I can check [ 0 ], [ 1 ] , [ 2 ] and [ 3 ] either separately, or I can loop through it. Since (as far as I know) false should have value 0 and anything other than 0 is true, I thought about simply doing:

if ( *(int*) boolArray) { }

This works, but I realize that it relies on bool being one byte and int being four bytes. If I cast to (std::uint32_t) would it be OK, or is it still a bad idea? I just happen to have 3 or 4 bools in an array and was wondering if this is safe, and if not if there is a better way to do it.

Also, in the case I end up with more than 4 bools but less than 8 can I do the same thing with a std::uint64_t or unsigned long long or something?


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images