• We Code
  • We Design
  • We Develope
  • We Write
  • We Share

menu

Sunday, August 19, 2012

Bitwise Operators In Java

Bitwise operators are used to manipulate individual bits of a data item. There are situations where individual bits of data are modified.

These operators operate only on byte,short ,int and long types 





      OPERATOR                                    DISCRIPTION  




  1.        ~                        ::             Bitwise NOT                         
  1.        &                        ::             Bitwise AND                         
  1.         |                        ::             Bitwise OR                           
  1.         ^                       ::             Bitwise exclusive OR           
  1.        <<                      ::             Left shift                                 
  1.        >>                      ::             Right shift                               
  1.        >>>                    ::             Right shift zero fill                
  • & operator  Bitwise  AND operator  compares each bit of first operand with each bit of second operand.For instance 71 binary number is 01000111 and 25 binary number is 00011001. If both the bits are 1 and 1 the result is 1 if any one of the bit is 0 the result is 0
  • | operator     Bitwise  AND operator  compares each bit of first operand with each bit of second operand. If both the bits are 1 and 1 the result is 1 ,even if any one of the operand is 1 the result is 1 and only if both of the bit is 0 the result is 0
  • ^ operator       Bitwise exclusive or operator  compares each bit of first operand with each bit of second operand. If both  the operand are 0 and 1 the result is 0 and only if any one of the bit is 0 and other is 1 the result is 0
  • ~ Operator     Bitwise NOT operator is a unary operator . This oprator is used only on a single operand.
  • << Operator  The  left shift operator is used to shift the bits of a given operand  to left for specified number of position
  • >> Operator  The  right shift operator is used to shift the bits of a given operand  to right for specified number of position
  • >>> Operator  The  right shift operator is used to shift the bits of a given operand  to right for specified number of position.The  difference in >> and >>>  is the sign bit extension is not done .Instead of 1s filled at leftmost bit is -ve integer values,0's  are filled





public class BitwiseOpertorDemo{
public static void main(String args[]) {
int firstVar = 60; /* 60 = 0011 1100 */
int secondVar = 13; /* 13 = 0000 1101 */
int ThirdVar = 0;
ThirdVar = firstVar & secondVar ; /* 12 = 0000 1100 */
System.out.println("firstVar & secondVar = " + ThirdVar );
ThirdVar = firstVar | secondVar ; /* 61 = 0011 1101 */
System.out.println("firstVar | secondVar = " + ThirdVar );
ThirdVar = firstVar ^ secondVar ; /* 49 = 0011 0001 */
System.out.println("firstVar ^ secondVar = " + ThirdVar );
ThirdVar = ~firstVar ; /*-61 = 1100 0011 */
System.out.println("~firstVar = " + ThirdVar );
ThirdVar = firstVar << 2; /* 240 = 1111 0000 */
System.out.println("firstVar << 2 = " + ThirdVar );
ThirdVar = firstVar >> 2; /* 215 = 1111 */
System.out.println("firstVar >> 2 = " + ThirdVar );
ThirdVar = firstVar >>> 2; /* 215 = 0000 1111 */
System.out.println("firstVar >>> 2 = " + ThirdVar );
}
}

0 comments:

Post a Comment

...