Java - Bitwise Operators (&,|,^,~,!) / Chapter -7/Operator & Assignments

In JAVA there are five bitwise operators available. 

  1. Bitwise AND ( & )     [both boolean and integral type]
  2. Bitwise OR    (  |  )      [both boolean and integral type]
  3. Bitwise XOR  ( ^ )      [both boolean and integral type]
  4. Bitwise Complement Operator ( ~ )   [only for integral types]
  5. Bitwise Complement Operator ( ! )   [only for boolean types]



1. Bitwise AND ( & ):-

If we apply bitwise AND (&) between two arguments then if and only if it will return true if both arguments are true otherwise it will return false.

example for boolean:-


System.out.println(true & true );         output  is    true



System.out.println(true & false);         output  is    false



System.out.println(false & false);        output  is    false


example for integrals:-

System.out.println( 5  &  4);         output  is    4

reason:   5 in binary  101
              4 in binary  100        now applying AND Operation if both are true then only true.
            ――――――――




result:                        100   means   4  that's we are getting 4 as the output. 







2.Bitwise OR( | ) :-
If we apply bitwise OR( | ) between two arguments then if and only if it will return true if one argument is true .

example for boolean:-


System.out.println(true | false);           output  is    true



System.out.println(true | true );           output  is    true


example for integral:-


System.out.println( 5  |  6);         output  is    7

reason:   5 in binary  101
              6 in binary  110        now applying OR Operation if one is  true then true.
            ――――――――
result:                        111   means   7 that's we are getting 7 as the output. 






3. Bitwise XOR( ^ ):-
 If we apply bitwise XOR( ^ ) between two arguments then if and only if it will return true if both arguments are different otherwise it will return false.

example for booleans:



System.out.println(true ^ false);         output  is    true

System.out.println(true ^ true );         output  is    false


example for integrals:


System.out.println( 5  ^  6);         output  is    3

reason:   5 in binary  101
                6 in binary  110        now applying XOR Operation if both are different then only true.
            ――――――――
result:                          011   means   3 that's we are getting 3 as the output








4.Bitwise Complement Operator(~);-
This operator can be applied to the integral type only not for boolean type. If we are applying then we will get Compile-Time Error.

example:-


System.out.println(~4);         output is -5 

System.out.println(~true);         
Compile-Time Error : Operator ~ cannot be applied to boolean







5.Bitwise Complement Operator(!);-
This operator can be applied to the boolean type only not for integral type. If we are applying then we will get Compile-Time Error.



example:-


System.out.println(!true);         output is  false

System.out.println(!4);         
Compile-Time Error : Operator ! cannot be applied to int


Next:Chapter -8/Operator & Assignments / Short Circuit Operators

Back:Chapter -6/Operator & Assignments / instanceof Operators 

Home:





No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com