Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Bitwise and Bit Shift Operators

                         

In Java the bitwise and bit shift operators are used to manipulate the contents of variables at a bit level according to binary format. These operators perform bitwise and bit shift operations on integral type variables. There are different types of bitwise and bit shift operators available in the Java language summarized  in the table.

Symbol Name of the Operator Example
 ~   Unary bitwise complement ~op2
 & Bitwise AND op1 & op2
 |  Bitwise inclusive OR op1 | op2
 ^   Bitwise exclusive OR op1 ^ op2
 <<  Signed left shift op1 << op2
 >> Signed right sift op1 >> op2
 >>> Unsigned right shift op1 >>> op2

 

Lets understand these operators in brief :

I. Unary Bitwise Complement ("~") :

The unary bitwise complement ("~") operator  takes a single bit and inverts the level of that bit pattern and can be applied to any of the integral types. In this case, the value of a bit which is 0 become 1 and vice versa. For example the value 7 to a variable "x" is represented in binary as 0111. But after applying "~" operator, the operation will be performed on each bit pattern which will return 1000 to the variable and the value 8 in the decimal format. Lets use the table to understand bitwise complement operation.

Operand  Result
 0  1
 1  0
 1  0
 0  1

II. Bitwise AND (&):

The Bitwise AND (&) operator performs the bitwise AND operation on each parallel pair of bits of two operands. The result is 1, if corresponding bits are 1 in both operands. Otherwise, the result is 0. Lets understand the AND operations using truth table:

             (AND)

 A  B  Result
 0  0  0
 1  0  0
 0  1  0
 1  1   1

III. Bitwise inclusive OR ( | ):

The Bitwise inclusive OR ( | ) operator performs the bitwise inclusive OR operation on each parallel pair of bits of two operands. In each pair, the result is 1, if either first or second bit is 1 (or both are 1). Otherwise the result is 0. Lets see the table of using inclusive operations.
Lets understand the inclusive OR operations using truth table:

               (OR)

 A  B  Result
 0  0  0
 1  0  1
 0  1  1
 1  1  1

IV. Bitwise exclusive OR (^):

The Bitwise exclusive OR (^) performs the exclusive or (XOR) operation i.e.  The result in each position is 1 if the two bits are different, and 0 if they are the same. Lets understand the exclusive OR operations using truth table:

 A  B  Result
 0  0  0
 1  0  1
 0  1  1
 1  1  0

Bit Shifts Operators:

The computer processor has the registers including a fixed number of available bits for storing numerals. So it is possible to "shift out" some bits of the register at one end, and "shift in" from the other end. The number of bits are shifted within the range mode of  32.

The bit shifts operators are used to perform bitwise operations on the binary representation of an integer instead of its numerical value. In this operation, the bit shifts operators don't operate the pairs of corresponding bits rather the digits are moved, or shifted in a computer register either to the left or right according to the distance specified by a number.

Sign Bit:

A sign bit is found in the left most position of the number and is know as most significant bit (MSB) which indicates the status of a number i.e. the number is positive or negative. If the value of the sign bit is 0 then the number is positive; otherwise the number is negative, if the value of the sign bit is 1.

Now lets understand these operators in brief. 

I. Signed Left Shift ("<<") :

The signed left shift ("<<") operator shifts a  bit (or bits) to the left by the distance specified in the right operand. In this case, the leftmost digit is shifted at the end of the register, and a new 0 is shifted into the rightmost position. No matter, the number is positive or negative; In both of case the leading bit position is always filled with a zero.

This diagram shows that, all bits of the upper position were shifted to the left by the distance of 1; and the Zero was shifted to the right most position. Thus the result is returned as 11100.

Another expression "2<<2";  shifts all bits of the number 2 to the left placing a zero to the right for each blank place.  Thus the value 0010 becomes 1000 or 8 in decimal.

II. Signed Right Shift (">>") :

The signed right shift (">>") operator shifts a  bit (or bits) to the right by the distance specified in the right operand and fills the left most bit by the sign bit. In this case the rightmost bit (or bits) is shifted out, and a new 0 is filled with the sign bit into the high-order bits to the left position if the left operand is positive; otherwise 1, if the left operand is negative. This technique is known as sign extension.

This diagram shows that, all bits of the upper position were shifted to the right distance specified by 1;  Since the sign bit of this number indicates it as a positive number so the 0 is shifted to the right most position. Thus the result is returned as 00011 or 3 in decimal.

Another expression "2>>2";  shifts all bits of the number 2 to the right placing a zero to the left for each blank place.  Thus the value 0010 becomes 0000 or 0 in decimal.

When signed left or signed right shifting operation is performed then  the sign bit is ignored i.e. all the bits except the sign bit can be moved but the sign bit stays the same. Thus a signed left or signed right shift (<< and >>) operator never causes a number to change its sign. A positive number will always stay positive and a negative number will always stay negative. But the result for a negative number is different.

For example, if we take a negative number as -50 then this  value is represented in binary as 11001110 then the expression "-50>>2"; will return the result as 11110011 or -13 in decimal.

III. Unsigned Right Shift (">>>")

The unsigned right shift (">>>") operator behave like the signed right shift operator. i.e. it shifts a bit (or bits) to the right. But unlike ">>" operator, this operator always shifts zeros into the leftmost position by the distance specified in the right operand. So the result of applying the >>>operator is always positive.

For example, the expression "14>>>2";  shifts all bits of the number 14 to the right placing a zero to the left for each blank place  Thus the value 1110 becomes 0011 or 3 in decimal.

An unsigned shift operation of a negative number generally returns the result in a positive number, because any unsigned right shift operation replaces the leading sign bit with a zero which indicates a positive number.

Lets have an example where two variables "x" and "y" contain the value 11 and 12 respectively. Thus the binary value for each variable are as :

x = 1 0 1 1
y = 1 1 0 0

public class BitwiseOperators {
   public BitwiseOperators( ) {

  int a = 11//1 0 1 1
  int b = 12//1 1 0 0
  
      System.out.println("a & b : "+(a & b));

      System.out.println("a | b : "+(a | b));

      System.out.println("a ^ b : "+(a ^ b));

      System.out.println("~a : "+(~a));

      System.out.println("a << b : "+(a << b));

      System.out.println("a >> b : "+(a >> b));

      System.out.println("a >>> b : "+(a >>> b));
        }

   public static void main(String args[]){
     new BitwiseOperators();
   }
}

Output of the Program: 

C:\nisha>java BitwiseOperators
a & b : 8
a | b : 15
a ^ b : 7
~a : -12
a << b : 45056
a >> b : 0
a >>> b : 0

Download this Program

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

For example, if we take a negative number as -50 then this value is represented in binary as 11001110 then the expression "-50>>2"; will return the result as 11110011 or -13 in decimal.


the above example is not very clear
the binary value of 50 is 110010
-50>>2 =10001100 why do you convert it ones complemt method it is MSB method?

Posted by rani on Monday, 04.28.08 @ 20:23pm | #58075

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.