Monday 3 February 2014

Bit Wise Operators

The Java programming language provides operators that perform bit wise and bit shift operations on integral types.
Operator Description
~ Inverts a bit pattern
<< Perform Left shift
>> Perform Right Shift
>>> Perform unsigned right shift
& Bit wise AND
| Bit wise OR
^ Bit wise Exclusive OR

Before discuss about the Bit wise operators, will discuss about Binary representations of a number

1's Complement
1's complement simply inverts the bits, if the bit is set to zero, it sets the bit to 1 and if the bit is set to 1 it sets the bit to zero.

Ex:
8 in binary            = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1

2's complement
Negative numbers are stored in 2's complement form. i.e., -8 is 2's complement of 8.
2's complement is sum of (1's complement of a number + 1)

Ex:
8 in binary            = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1
2's complement(8) =               (+) 1
                                             -----------------
                                             1 1 1 1 1 0 0 0
                                              ----------------

there for -8 in 2's complement form is 11111000

Inversion (~)
Inversion operator change the bit value to zero, if it is one and change the bit value to one if it is zero.

Example
byte a = 7;

a in binary format is       0 0 0 0 0 1 1 1
~(a) in binary format is  1 1 1 1 1 0 0 0
~(a) represents -8 in binary format.

Example
class BitWiseEx{
 public static void main(String args[]){
  byte byteVar = 7;
  
  System.out.println("Value of Byte variable is " + byteVar);
  System.out.println("Value of Byte variable after inversion is " + ~(byteVar));
 }
}

Output
Value of Byte variable is 7
Value of Byte variable after inversion is -8

Bit Wise Left Shift(<<)
Left shift operator simply shifts all the bits to one bit left, i.e, it simply doubles the value.

Syntax:
    variable << n

    left shift the variable by 'n' number of times.

    Variable << n is equals to variable = variable * 2 power n

        int var = 2;
        var << 1 = 2 * 2 power 1 = 4
        var << 2 = 2 * 2 power 2 = 8
        var << 9 = 2 * 2 power 9 = 1024

Example
class BitWiseEx{
 public static void main(String args[]){
  byte byteVar = 7;
  
  System.out.println("Value of Byte variable is " + byteVar);
  System.out.println("After shifting 1 bit left " + (byteVar<<1));
  System.out.println("After shifting 2 bits left " + (byteVar<<2));
  System.out.println("After shifting 3 bits left " + (byteVar<<3));
  System.out.println("After shifting 4 bits left " + (byteVar<<4));
  System.out.println("After shifting 5 bits left " + (byteVar<<5));
  System.out.println("After shifting 6 bits left " + (byteVar<<6));
  System.out.println("Value of Byte variable is " + byteVar);
 }
}

Output
Value of Byte variable is 7
After shifting 1 bit left 14 
After shifting 2 bits left 28
After shifting 3 bits left 56 
After shifting 4 bits left 112 
After shifting 5 bits left 224 
After shifting 6 bits left 448 
Value of Byte variable is 7

Bit Wise Right Shift(>>)
   Right shift operator simply shifts all the bits to one bit right, i.e, it simply half (divide by 2) the value

   Syntax:
   variable >> n
      right shift the variable by 'n' number of times.

   Variable >> n is equals to variable = variable / 2 power n

   int var = 128;
   var >> 1 = 128 / (2 power 1) = 64
   var >> 2 = 128 / (2 power 2) = 32

Example
class BitWiseEx{
 public static void main(String args[]){
  int intVar = 128;
  
  System.out.println("Value of int variable is " + intVar);
  System.out.println("After shifting 1 bit Right " + (intVar>>1));
  System.out.println("After shifting 2 bits Right " + (intVar>>2));
  System.out.println("After shifting 3 bits Right " + (intVar>>3));
  System.out.println("After shifting 4 bits Right " + (intVar>>4));
  System.out.println("After shifting 5 bits Right " + (intVar>>5));
  System.out.println("After shifting 6 bits Right " + (intVar>>6));
  System.out.println("After shifting 6 bits Right " + (intVar>>7));
  System.out.println("Value of int variable is " + intVar);
 }
}

Output
Value of int variable is 128
After shifting 1 bit Right 64
After shifting 2 bits Right 32
After shifting 3 bits Right 16
After shifting 4 bits Right 8
After shifting 5 bits Right 4
After shifting 6 bits Right 2
After shifting 6 bits Right 1
Value of int variable is 128

Bit Wise unsigned Right Shift(>>>)
   shifts all the bits to one bit right, and make the left most bit to zero
class BitWiseEx{
 public static void main(String args[]){
  int intVar = -128;
  
  System.out.println("Value of int variable is " + intVar);
  System.out.println("After shifting 1 bit Right " + (intVar>>>1));
 }
}

Output
Value of int variable is -128
After shifting 1 bit Right 2147483584

Explanation
-128 in 2's complement form is

11111111 11111111 11111111 10000000

>>> shifts the one bit right and replace leftmost bit by zero

so (-128>>>1) is like below sequence

01111111 11111111 11111111 11000000

Bit Wise AND(&), OR(|), Exclusive OR(^)

Bit 1 Bit 2 & | ^
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0



Example
class BitWiseEx{
 public static void main(String args[]){
  byte a = 8;
  byte b = 9;
  
  System.out.println("a & b is " + (a&b));
  System.out.println("a | b is " + (a|b));
  System.out.println("a ^ b is " + (a^b));
 }
}


Output
a & b is 8
a | b is 9 
a ^ b is 1 


Explanation
   a    = 0000 1000
   b    = 0000 1001
   a&b = 0000 1000

   a    = 0000 1000
   b    = 0000 1001
   a|b = 0000 10001

   a     = 0000 1000
   b     = 0000 1001
   a^b = 0000 0001

Note:
   Bit wise operators are applicable only to integer values like byte, short etc., these are not applicable to float, double, Boolean.

Logical Operators                                                 other Operators                                                 Home

No comments:

Post a Comment