Bitwise Operators

by Gabriela C. Perez.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on java  

You are here: Categories » Computers and technology » JAVA

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
Leave a comment or ask a question
Total comments: 0

JAVA Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Why Java programmers are taking one step ahead in software development industry - Various high quality programming languages are used in the Software Industry. Some of them are expensive, while some are affordable by nature, however all of these are vastly used according to the (more...)
SELECTION STATEMENTS IN JAVA - Like other programming languages java offers the control statements to control the execution of a program. The control statements in java are the selection statements, loop constructs and the jump (more...)
JAVA and its Advantages - In today's highly competitive world, JAVA has become one of the most secure technologies for website and software development. Prime benefits of JAVA are platform independency and easy availability (more...)
Java Tactics: Earn A Fortune With Java - Get the right Knowledge Get the right Papers Get the right Direction Get the Money Hi, my name's (more...)
Introduction to Object Oriented Programming (OOP) - The transition from a procedural programming (non-OOP) language to an object-oriented programming language is a large step for many programmers. It is true that both methods of programming can ulti (more...)
Operator Precedence in programming languages - Operator precedence deciphers the order in which calculations in an expression occur. Looking at the calculation example 3 + 4 * 6, the answer could be calculated by adding 3 and 4, which gives (more...)
Arithmetic Assignment Operators - The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when ass (more...)
Invocation Chaining - Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given stateme (more...)
Regular Expressions in Java - A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharacters. Normal characters a (more...)
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 (\) character, followed by th (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.