Java – converts two Boolean values to int

This can be very easy

If I have two Boolean values a and B, how can I get the equivalent "binary" number?

false and false = 0 
false and true  = 1 
true  and false = 2 
true  and true  = 3

Solution

(left ? 2 : 0) + (right ? 1 : 0);
(left ? 2 : 0) + (right ? 1 : 0);

I'm not sure if Java handles Boolean values like C, but if so:

2*left+right;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>