• Bitwise operators in java performs operation on bits 
  • Bitwise operators performs bit by bit operations
  • Bit-wise operators operates on individual bits of operands.



 Bit wise Operators in Java:

  1. ~      : Bit wise unary not
  2. &     : Bit wise And
  3. |       : Bit wise OR
  4. ^      : Bit wise Exclusive OR


 1.Bit wise Unary Not  ~:

  • Bit wise Unary not Inverts the bits.
  • Lets see a java program on bit wise unary not

Java Example program on bit wise unary not operator ~:

  1. package operatorsinjava;
  2. public class BitwiseUnaryNotDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7.     public static void main(String[] args) {
  8.  
  9.   int x=2;
  10.  
  11.   System.out.println(~x);
  12.         
  13.   int y= 3;
  14.  
  15.   System.out.println(~y);
  16.         
  17.   int z= 4;
  18.         
  19.   System.out.println(~z);
  20.  
  21.     }
  22.  
  23. }

Output:


  1. -3
  2. -4
  3. -5

bitwise unary operator in java


2. Bitwise AND operator:

  • Bit wise And returns 1 if both operands position values are 1 otherwise 0.
  • Bitwise operation on two numbers
  • 1 & 2
  • 001
    010
    -----
    000 =0
    -----
  • 2 & 3
  • 010
    011
    -----
    010 =2
    -----
Java Example program on bitwise AND operator &:


  1. package operatorsinjava;
  2. public class BitwiseAndDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {

  8.    int x=1;
  9.    int y=2;
  10.         
  11.   System.out.println(x&y);
  12.         
  13.   int i=2;
  14.   int j=3;
  15.         
  16.  System.out.println(i&j);
  17.         
  18.  System.out.println(4&5);
  19.  
  20. }
  21.  
  22. }

Output:


  1. 0
  2. 2
  3. 4
Bitwise And operator in java Example


3.Bitwise OR operator in Java | :

  • Bit wise OR returns 1 if  any one operands position values are 1 or both 1 s. otherwise 0.
  • 1 |2
  • 001
    010
    -----
    011 =3
    -----
  • 2 | 3
  • 010
    011
    -----
    011 =3
    -----
Java Example program on bitwise OR operator |:

  1. package operatorsinjava;
  2. public class BitwiseORDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {

  8.    int x=1;
  9.    int y=2;
  10.         
  11.   System.out.println(x|y);
  12.         
  13.   int i=2;
  14.   int j=3;
  15.         
  16.  System.out.println(i|j);
  17.         
  18.  System.out.println(4|5);
  19.  
  20. }
  21.  
  22. }

Output:

  1. 3
  2. 3
  3. 5

4.Bit wise Exclusive OR operator in java ^:

  • Bit wise XOR operator returns 1 if any of the operands bits position 1 
  • Java XOR operator returns 0 if both operands position is 1.
  • Lest see java Xor on 1 ^ 2
  • 1 ^ 2
  • 001
    010
    -----
    011 =3
    -----
  • Lest see java xor operation on 2 ^3
  • 2 ^ 3
  • 010
    011
    -----
    001 =1
    -----

Java Example program on bitwise OR operator |:

  1. package operatorsinjava;
  2. public class BitwiseXORDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {

  8.    int x=1;
  9.    int y=2;
  10.         
  11.   System.out.println(x^y);
  12.         
  13.   int i=2;
  14.   int j=3;
  15.         
  16.  System.out.println(i^j);
  17.         
  18.  System.out.println(4^5);
  19.  
  20. }
  21.  
  22. }

Output:

  1. 3
  2. 1
  3. 1


Java XOR bitwise operator

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu