how boolean values are treated in java by bit wise operators

前端 未结 4 1276
礼貌的吻别
礼貌的吻别 2021-01-14 19:48

consider this example please

int i=11, j=5;
boolean b=true, c=false;
System.out.println(b&c); // --> output=false
System.out.println(i&j); // --&g         


        
相关标签:
4条回答
  • 2021-01-14 20:21

    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

    0 讨论(0)
  • 2021-01-14 20:26

    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 to true if and only if both arguments are true)
    • | is the logical OR (it will evaluate to true if and only if at least one of the arguments is true).

    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 values true and false. 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 language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding.

      The easiest way to fullfil this requirement in a JVM is obviously to let 1 be true and 0 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 on boolean will use normal bitwise operations here.

    0 讨论(0)
  • 2021-01-14 20:37

    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.

    0 讨论(0)
  • 2021-01-14 20:37

    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.

    0 讨论(0)
提交回复
热议问题