What are Java bitwise operators and how to use them
With Java bitwise operators, you can manipulate individual bits in whatever way you wish. The seven operators are structured similarly and follow fixed rules that are easy to learn.
What are bitwise operators and what are they used for?
Java operators are an important tool when working with the programming language. In addition to the many other possibilities and functions, they give you the option of manipulating the smallest possible units of information: bits. Bits (binary digits) have their own number system based on the values 0 and 1. If you want to query binary values bit by bit, you need a bitwise operator in Java. You can apply this to the numeric data types (byte, char, int, short, long).
Although you won’t need the function on a daily basis, it can be useful in a lot of situations. For example, it can come in handy if you want to save space when converting data, if you want to use the XOR operator or if you just want to change a bit. They’re also the basis for all higher circuit operations. That’s why it’s important to understand the basic functionality of Java bitwise operators.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
Which Java bitwise operators are there?
There are a total of seven different bitwise operators in Java. To gain an all-round understanding, we’ll start with the operands a and b.
- ~ (bitwise NOT, complement): This operator inverts bits. A 0 becomes a 1 and a 1 becomes a 0.
- & (bitwise AND): This operator outputs a 1 if both operands are 1. Otherwise, a 0 is output.
- | (bitwise OR): This operator produces a 1 if one of the two operands is also 1.
- ^ (XOR or bitwise exclusive OR): This operator outputs a 0 if both operands have the same value. Otherwise, it outputs a 1.
- << (left shift): This operator shifts operand a to the left by b positions. If this results in spaces, the spaces will be filled with 0.
- >> (right shift with sign, arithmetic): This operator shifts all bits of a by b positions to the right. If the bit with the highest value was set before execution, it remains set afterwards. Negative numbers remain negative.
- >>>> (unsigned right shift, logical): This operator shifts the bits of a to the right by b positions. Spaces are always filled with 0.
Bitwise NOT
The Java bitwise operator bitwise NOT is represented by a tilde (~). It negates all bits, turning zeros into ones and ones into zeros.
Let’s take, for example, the number 20. In binary, it looks like this: 10100. If we apply the bitwise NOT operator, every bit of the number will be switched: 10100 becomes 01011. This is the value of the expression ~20. If we convert this binary number back to a decimal number, we get the value -21. If you want to try this process out in Java, enter the following code and the Java command System.out.println
to output the result.
public class Main {
public static void main(String[] args) {
int value1 = 20;
System.out.println(~value1);
}
}
javaIf you’ve entered everything correctly, the output should read “-21”.
Bitwise AND
Bitwise AND compares two numbers in their binary form bit by bit. The first bit of the first number is compared with the first bit of the second number, the second with the second bit and so on. If both bits are 1, a 1 is output. If this is not the case (both bits are 0 or one of the bits is 0), a 0 is output. In the example below, we’ll take a look at the two decimal numbers 18 and 25. As a binary number, 18 is 10010, and 25 in binary notation is 11001. Now let’s compare these two numbers and determine a third number from them.
18 = 10010 25 = 11001
The first two bits in the numbers are both 1, so the resulting number also starts with 1. The second bit of 25 is also 1, but the second bit of 18 is 0, making the digit in the third number a 0. After going through both numbers bit by bit, we end up with the binary number 10000. If we convert this to a decimal number, the output will be 16.
The code looks like this:
public class Main {
public static void main(String[] args) {
System.out.println(18&25);
}
}
javaThe output in the console should be 16.
Bitwise OR
The Java operator bitwise OR also compares two numbers bit by bit. In this case, however, only one of the two operands has to have the value 1 to end up with the result 1. If we take the numbers from the previous example, it will look like this:
18 = 10010 25 = 11001
Since all bits except the third digit contain at least one 1, the resulting number is: 11011. Converted, we get 27.
Here’s what this example looks like in code:
public class Main {
public static void main(String[] args) {
System.out.println(18|25);
}
}
javaXOR
XOR or bitwise exclusive OR (^) is similar to bitwise OR. With bitwise OR, one or both operands must be 1 in order to output 1. However, with XOR, a 1 is only output if exactly one of the two values is 1. To get a better idea of how XOR works, let’s take a look at an example:
18 = 10010 25 = 11001
The first two bits have the value 1, so with this bitwise operator in Java, the result is 0. The second bit of 18 is 0, but the second bit of 25 is 1. This results in the value 1. If we continue through, we get the number 01011. In decimal form, this is 11.
Here’s the code:
public class Main {
public static void main(String[] args) {
System.out.println(18^25);
}
}
javaLeft shift
Left shift shifts the bits of value a to the left by the distance b. The resulting blanks are filled with 0. An int value that occupies 32 bits in a memory illustrates this clearly. Let’s take the number 20 or 10010 for a again and shift it by the b-value 2 to get the c-value 1001000. Two zeros are placed at the end. 1001000 corresponds to the decimal value 72.
Here’s what this process looks like in code:
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 2;
int c = (a << b);
System.out.println(c);
}
}
javaRight shift with sign
Right shift works the other way round. Here, bits of the value a are shifted to the right by the value b, which produces the c-value. As a result, the last bits are left out. If we shift 20 or 10010 two places to the right, the result is 100 or 4.
Here’s the code:
public class Main {
public static void main(String[] args) {
System.out.println(20 >> 2);
}
}
javaIt’s important to note that if a is a positive number, the spaces are filled with 0. If it is negative, the spaces are replaced with 1.
Right shift without sign
In principle, the Java bitwise operator right shift without sign (>>>) works in the same way. The only difference is that the spaces created on the left-hand side by the shift to the right are always filled with 0. This always results in a positive number, even if the starting value was negative.