Reputation: 28592
I have four integer variables a
, b
, c
and d
. I want to know if all of them are of values 1 or 0. Apparently I can use if statement to check this like:
if((a == 0 || a == 1) && (b == 0 || b == 1) &&
(c == 0 || c == 1) && (d == 0 || d == 1))
{
print(true)
}
else
{
print(false)
}
It's just a bit boring to write all of that. I'm thinking if there are any methods to use bit operations to solve my issue. But for now I don't have any clue about it. Anyone can point me to the right direction? Or is there any other simpler way to check that?
Upvotes: 0
Views: 69
Reputation: 178461
One approach is to query for max and min value of these variables, and verify the min value is 0/1 and max value is 0/1. Max and Min are supported in SQL.
Upvotes: 1
Reputation: 4431
In C, take the bitwise or, and test that:
int e = a|b|c|d;
return e==0 || e==1;
Upvotes: 2