In this tutorial, we will discuss how to use the bitwise AND operator “&” on integers, and the side effects that this would have such as on signed and unsigned numbers.
Before we start, I recommend that you check out our article about the best books and learning material that you could use to jump-start your Java career and to improve your skills.
Introduction
For those who are not familiar with the “AND” or “&” operator, it is a logical operator in Java that is used to AND two bits of information at a time. For those who are not familiar with the operator, it’s job is basically to check if all conditions are true. If so then the result is true for the whole operator. If one of the conditions evaluated by the operator is false, then the whole result turns to false.
For example, in natural language, we say “If you do your homework and get good grades, then you will get a cake”. Both conditions need to be true in order for the result to be true. If you fail to get good grades or if you do not do your homework, or if you do neither of them, then you will not get the cake 😉 .
For those digital logic engineers out there, here is the truth table of the AND operation:
X | Y | X & Y |
False | False | False |
False | True | False |
True | False | False |
True | True | True |
Bitwise AND in Java
In Java, there are two uses for the “&” operator. The first is to perform logical AND on booleans. The second is to perform a bitwise AND operation on integers. The operator is called bitwise since the operation is performed on corresponding bits on two integer numbers. The behavior per bit is as follows:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
Therefore, if we do an and operation between two numbers, the following would happen, let us say we will AND the numbers 3 and 2, with the corresponding binary representations 11 and 10. Performing the AND will work as follows:
“AND the most significant bit of the first number with the most significant bit of the second number (1 AND 1). AND the 2nd most significant bit of the first number with the 2nd most significant bit of the second number (1 AND 0).”
Hence:
AND (11, 10) = 10
The following code is an example on how to perform bitwise AND on integer numbers in Java:
public class BitwiseAndExample {
public static void main(String[]args) {
byte x = 7; //equivalent to binary of 00000111
byte y = -7; //equivalent to binary of 10000001
byte xANDy = (byte)(x & y);
System.out.println("X AND Y = "+xANDy); //result of AND = 00000001
int a = 13; //0000...0001101
int b = 11; //0000...0001011
int aANDb = a & b;
System.out.println("A AND B = "+aANDb);// result of AND = 1001
}
}
Notice that in Java, signed / negative numbers are represented using the 2’s compliment representation. Hence, the number -7 is represented
as 10000001. Therefore ANDing -7 and 7 produces +1, since the sign bit in the resulting number is 0. The following is the output of running
the above program:
X AND Y = 1 A AND B = 9
Notice how the AND operator is performed bit by bit on corresponding bits within the ANDed numbers. (The binary representations of the numbers are available in the comments in the code above).
Uses of the bitwise & operator
Depending on your application, you might use the operator for mathematical purposes or for logical purposes.
However, you will notice that as you interact with more and more applications, you will see that when the operator is used with integers, it is often for flag detection purposes.
For example, if you are interacting with an embedded system which has a limited amount of memory, this system will try to represent information as efficiently as possible.
One way to do that is to use a single integer value to tell you about the current status. For example, you might agree with the embedded systems team to use an integer value as follows:
- Turn on the first bit if the device is powered On.
- Turn on the 2nd bit if the device has an error.
- Turn on the 3rd bit if the device needs charging.
- And so on.
So, let us say that the device is turned on, does not have an error but needs charging. That can be represented in binary format as 101 (or with the integer 5).
Now, your program’s task is to turn on the charger if the device needs charging. How can you tell if the device needs charging from the status value?
You can do so by ANDing the status integer of the device with the charging required flag. The charging required flag is when the 3rd bit of the status integer is 1. This represents the value 4 in decimal. So now, you can AND the charging flag 4 with the device status 5, which would yield a 4 (and not a 0). From there, you see that the charging required flag is turned on, and you proceed to charge the device.
This is a very efficient way to manage devices, such as embedded systems, Bluetooth devices, etc, and a clever way to use the integers to represent the status of a system.
Summary
In this post, we discussed how to use the AND operator on integers in Java and why it is a useful operator in specific scenarios.
Leave a Reply
You must be logged in to post a comment.