# Programming Tutorials

## The Bitwise Operators

By: aathishankaran

The Bitwise Operators

Java defines several Bitwise operations, which can be applied to the integer types, long, int, short, char, and byte. These operators act upon the individual bits of their operands. They are summarized in the following table:

Operator                     Result

~                                  Bitwise unary NOT

&                                 Bitwise AND

!                                   Bitwise OR

^                                  Bitwise exclusive OR

>>                                Shift right

>>>                             Shift right zero fill

<<                                Shift left

&=                               Bitwise AND assignment

!=                                 Bitwise OR assignment

^=                                Bitwise exclusive OR assignment

>>=                             Shift right assignment

>>>=                           Shift right zero fill assignment

<<=                             Shift left assignment

Since the Bitwise operators manipulate the bits within an integer, it is important to understand what effects such manipulations may have on a value. Specifically, it is useful to know how java stores integer values and how it represents negative numbers. So, before continuing, letâ€™s briefly review these two topics.

The entire integer types are represented by binary numbers of varying bit widths.

All of the integer types (expect char) are signed integers. This means that they can represent negative values as well as positive ones. Java uses an encoding known as twoâ€™s complement, which means that negative numbers are represented by inverting (changing 1â€™s to 0â€™s and vice versa) all of the bits in a value, then adding 1 to the result. For example, -42 us represented by inverting all of the bits in 42, or 00101010, which yields 11010101, then adding 1, which results in 11010110, or â€“42. to decode a negative number, first invert all of the bits, then add 1. â€“42, or 11010110 inverted yields 00101001, or 41, so when you add 1 you get 42.

The reason java (and most other computer languages) uses twoâ€™s complement is easy to see when you consider the issue of zero crossing. Assuming a byte value, zero is represented by 00000000. in oneâ€™s complement, simply inverting all of the bits creates 11111111, which creates negative zero. The trouble is that negative zero is invalid in integer math. Using twoâ€™s complement to represent negative values solves this problem. When using twoâ€™s complement, 1 is added to the complement, producing 10000000. this produces a 1 bit too far to the left to fit back into the byte value, resulting in the desired behavior, where â€“0 is the same as 0, and 11111111 is the encoding for  -1. although we used a byte value in the preceding example, the same basic principle applies to all of javaâ€™s integer types.

Because java uses twoâ€™s complement to store negative numbers and because all integers are signed values in java applying the Bitwise operators can easily produce unexpected results. For example, turning on the high-order bit will cause the resulting value to be interpreted as a negative number, whether this is what you intended or not. To avoid unpleasant surprises, just remember that the high-order bit determines the sign of an integer no matter how that high-order bit gets set.

Note: This is an extract from the book "Java: The Complete Reference" by Herbert Schildts

1. please correct things....and plz dont misguide
View Tutorial          By: amit verma at 2015-02-19 08:44:55

2. Bitwise OR is a pipe (|), not a logical NOT (!)
View Tutorial          By: Martijn at 2011-06-21 10:49:29

3. hello, that was very useful
but i have a byte array that i need to left/right shift.the reaso

View Tutorial          By: echo at 2011-04-05 11:50:41

4. Seems like the Bitwise Operators part in the Book "Java: The Complete Reference" by Herber
View Tutorial          By: Amit Singh at 2009-03-11 21:53:00

 Most Viewed Articles (in Java ) Latest Articles (in Java)

Comment on this tutorial