How booleans are handled in java by bitwise operators

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); // --> output=1

How much bit and operator work on boolean variables?

+3
source share
4 answers

There booleanare no bitwise operations in Java.

&and |do not perform bitwise operations in Java, but logical operations (as specified in JLS 15.22.2 ) .

  • &is logical AND (it will evaluate to trueif and only if both arguments true)
  • |is a logical OR (it will evaluate to trueif and only if at least one of the arguments true).

, , , , (.. byte, char, short, int, long ).

... , , "" "" .

: , - , ( - ).

3 :

  • Java

    Java boolean true false. , (. §4.2.2)

  • Java

    Java boolean .

    Java boolean, 1 true 0 false. Java boolean Java int, .

    JVM, , , 1 be true 0 no-op. , .

  • , boolean (?), boolean .

+11

:

& && AND

| || .

^, XOR !, NOT.


JVM ?

FALSE 0, TRUE . 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;

TRUE new Boolean(true), , true iconst_1, push (1) iconst_0 0 false

false <= > 0 true <= > 1, , & | int boolean

+9

, b/w & && , & - , false, && , false.

, .

+2

, - TRUE FALSE, 1 0, .

The second operation - 11 and 5 is taken as 1011 and 0101 (binary values), which is 0001 for anded.

0
source

All Articles