Java Bitwise Logical Operators

This tutorial will take you step by step through the process of understanding and using operators that act on individual bits. The best way to learn is to compile and run these programs yourself (copy, paste, compile and run !). Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

The bitwise logical operators are : ~ , | , & , ^

~ the NOT operator

| the OR operator

& the AND operator

^ the XOR operator

These opearators act on each individual bit within each opearand according to this table

A       B       A|B     A&B     A^B     ~A
0       0        0       0       0       1
1       0        1       0       1       0
0       1        1       0       1       1
1       1        1       1       0       0
Example 1: (~) the NOT opearator

This example shows the effect of using the ~ opearator.

  
class Bits1{ 
   public static void main(String args[]){
      System.out.println(" ~ NOT opeartor");

// invert 0
      System.out.println("~ 0 = "+~0); 

// invert 11
      System.out.println("~ 11 = "+~11);
    }
}

Explanations of ~0 = -1.

0 in binary is: 00000000000000000000000000000000

invert all the bits: 11111111111111111111111111111111

This is a negative number since its bit on the left is 1. To find its value we use the two's complement method.

invert all bits and add 1 and this gives

00000000000000000000000000000001, this is the binary form of 1. so 11111111111111111111111111111111 is the binary form of -1

Explanation of ~11 = -12

11 in binary is: 00000000000000000000000000001011

invert all bits: 11111111111111111111111111110100, this is a negative number and we use the two's complement method to fint its value.

invert all bits

00000000000000000000000000001011

add 1: 00000000000000000000000000001100 this is 12 (1*2^3+1*2^2+0*2^1+0*2^0)

and so 11111111111111111111111111110100 corresponds to -12

Example 2: ( | ) the OR opearator

This example shows the effect of using the | opearator.

  
class Bits2{ 
   public static void main(String args[]){
      System.out.println(" | OR opeartor");

// apply the | operator
      int x = 11|10;
      System.out.println("11|10 = "+x);
 }   }

Explanation of 11|10 = 11

11 in binary is: 00000000000000000000000000001101

10 in binary is: 00000000000000000000000000001100

apply the | operator to each individual bit and you obtain

00000000000000000000000000001101
in decimal form this number is: 1*2^3+1*2^2+0*2^1+1*2^0 = 11

Example 3: ( & ) the AND opearator

This example shows the effect of using the AND opearator.

  
class Bits3{ 
   public static void main(String args[]){
      System.out.println(" & OR opeartor");

// apply the & operator
      int x = 11&10;
      System.out.println("11&10 = "+x);
 }   }

Explanation of 11&10 = 10

11 in binary is: 00000000000000000000000000001101

10 in binary is: 00000000000000000000000000001100

apply the & operator to each individual bit and you obtain

00000000000000000000000000001100
in decimal form this number is: 1*2^3+1*2^2+0*2^1+0*2^0 = 10

Example 4: ( ^ ) the XOR opearator

This example shows the effect of using the XOR opearator.

  
class Bits3{ 
   public static void main(String args[]){
      System.out.println(" ^ XOR opeartor");

// apply the ^ operator
      int x = 11^10;
      System.out.println("11^10 = "+x);
 }   }

Explanation of 11^10 = 1

11 in binary is: 00000000000000000000000000001101

10 in binary is: 00000000000000000000000000001100

apply the ^ operator to each individual bit and you obtain

00000000000000000000000000000001
in decimal form this number is: 1*2^0 = 1

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.