how boolean values are treated in java by bit wise operators
consider this example please
int i=11, j=5;
boolean b=true, c=false;
Sy开发者_如何学编程stem.out.println(b&c); // --> output=false
System.out.println(i&j); // --> output=1
How bit wise and operator is working on boolean variables ?
There are no bitwise operations on boolean
in Java.
&
and |
don't do bitwise operations in Java, but logical operations (as specified in §15.22.2 of the JLS).
&
is the logical AND (it will evaluate totrue
if and only if both arguments aretrue
)|
is the logical OR (it will evaluate totrue
if and only if at least one of the arguments istrue
).
Note that the same operators are used for bitwise operations, but those only apply when both operands are of a type that is convertible to an integral type (i.e. byte
, char
, short
, int
, long
and their respective wrappers).
Since this post led to some ... spirited discussion, I think I'll clarify my insistence on the difference between "bitwise" and "logical" operations.
First of: Yes, at some level, the two operations will work exactly the same, except for the size of their input (which might even be identical, due to optimizations).
But, there are at lest 3 levels here:
The Java language
The Java language specification defines
boolean
as a primitive type with the two valuestrue
andfalse
. It does not define numeric values for those values and there is no direct way to convert it to a numeric type or vice versa (see last sentence of §4.2.2)The Java Virtual Machine
The Java Virtual Machine Specification defines the
boolean
type but provides very little support for it.It also says this about conversions
The Java virtual machine encodes
boolean
array components using 1 to represent true and 0 to represent false. Where Java programming languageboolean
values are mapped by compilers to values of Java virtual machine typeint
, the compilers must use the same encoding.The easiest way to fullfil this requirement in a JVM is obviously to let
1
betrue
and0
be false and let the conversion operation be a no-op. That's also the most likely implementation, but it's not necessarily the only correct implementation.The Hardware
This varies a lot, but most CPUs don't have support for a
boolean
type (why should they?) so operations onboolean
will use normal bitwise operations here.
For boolean type:
The operators &
and &&
are treated as logical AND
The operators |
and ||
are treated as logical OR.
You also have ^
which performs XOR and !
which performs a NOT.
How does this work in the JVM and how does it compare with bit-wise integer operations?
At the byte code level, FALSE has the value 0 and TRUE has the value. From the javap -c java.lang.Boolean
static {};
Code:
0: new #56; //class java/lang/Boolean
3: dup
4: iconst_1
5: invokespecial #89; //Method "<init>":(Z)V
8: putstatic #86; //Field TRUE:Ljava/lang/Boolean;
11: new #56; //class java/lang/Boolean
14: dup
15: iconst_0
16: invokespecial #89; //Method "<init>":(Z)V
19: putstatic #85; //Field FALSE:Ljava/lang/Boolean;
In this code it is defining TRUE as new Boolean(true)
and you can see that true
is pushed onto the stack using iconst_1
which is like push(1) Similarly iconst_0
or 0
is used for false
If you map false
<=> 0
and true
<=> 1
you can see that &
and |
work the same way for int
and boolean
For your case, I believe the only difference b/w &
and &&
is the fact that for &
, the second operand will still be evaluated even if the first operand evaluates to false, while in the case of &&
the second operand is not evaluated if the first evaluates to false.
Mostly relevant if you have a costly method in your expression.
The first operation you do - TRUE & FALSE is taken as 1 & 0, which is false.
The second operation - 11 & 5 is taken as 1011 & 0101 (the binary values), which is 0001 when anded.
精彩评论