Bitwise Operators


The following table shows the standard bitwise operators in Java and a description of them.

Operator Description
& Bitwise AND
| Bitwise inclusive-OR (generally known as OR)
^ Bitwise exclusive-OR (generally known as XOR)
~ Bitwise NOT

To illustrate the function of these bitwise operators, we can use two byte values, A and B, which in java could be represented by a variable of type byte. The following table shows the binary notation of A and B (as there are 8 bits in a byte).

Byte Binary Value
A 01101010
B 11110000

The AND (&) operator tests two bits and returns the resulting bit true if both test bits are true; otherwise, the return bit is false. The following table shows the result of A AND B.

Byte Bits
A 0 1 1 0 1 0 1 0
B 1 1 1 1 0 0 0 0
A AND B 0 1 1 0 0 0 0 0

The OR (|) operator tests two bits and returns the resulting bit true if any or both of the test bits are true; if they are both false, the return bit is also false. The following table shows the result of A OR B.

Byte Bits
A 0 1 1 0 1 0 1 0
B 1 1 1 1 0 0 0 0
A AND B 1 1 1 1 1 0 1 0

The XOR (^) operator tests two bits and returns the resulting bit true if one, and only one, of the bits is true; otherwise, if the two values are equal, the return bit is false. The following table shows the result of A XOR B.

Byte Bits
A 0 1 1 0 1 0 1 0
B 1 1 1 1 0 0 0 0
A AND B 1 0 0 1 1 0 1 0

The NOT (~) operator will invert all of the bits, where ones becomes zeros and zeros become ones, and is therefore a unary operator used with only one operand, whereas the other bitwise operators we have just seen were tested against two operands (binary operators), A and B. The following table shows the result of a NOT operation on byte A.

Byte Bits
A 0 1 1 0 1 0 1 0
NOT A 1 0 0 1 0 1 0 0

The bitwise AND, OR, and XOR operators can also be used with boolean expressions, as Boolean values effectively only contain one bit that is either true or false. This can be implemented in Java as follows:

boolean musicOn = true;  
boolean televisionOn = true; 
boolean areBothOn = musicOn & televisionOn;     // true  
boolean areAnyOn = musicOn | televisionOn;      // true  
boolean isOnlyOneOn = musicOn ^ televisionOn;   // false

There are also assignment operators for these three bitwise operators, as shown in the following table.

Operator Description
&= Bitwise AND assignment
|= Bitwise inclusive-OR assignment
^= Bitwise exclusive-XOR assignment

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Gabriela C. Perez at 01032008

Related Articles

1. What are Java Listeners
A listener in Java is an object that is used to handle events. In effect, it is implemented to listen for events and then tell the program the required informati...

2. Java EE Platform
Java is a programming language. The Java EE Platform provides a runtime environment (also known as JRE or Java Runtime Environment) as well as a development kit (also known...

3. Java EE Architecture
The Java EE application program interface (API) consists of a suite of technology components and services that are used to build enterprise applications. It includes compon...

4. ISO Management Elements in Java EE .NET Platforms
In a heterogeneous application and platform environment, IT managers are faced with different and often incompatible management frameworks. IT organizations often partiti...

5. Importing Java Packages
To use a package within a Java application or applet, we need to import it. We do this by means of the import keyword. So, for example, if we wish to include the I/O packag...

6. What is a Java Package
A Java package is a collection of related classes that can be imported into your program to support your software. They also provide namespace management, as wel...

7. Character Escape Sequences
Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) ch...

8. Conditional Statements
The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statemen...