With Java Bitwise, you can manipulate individual bits at will. The seven operators in total are always very similar and follow precise rules that are easy to learn.
What are bitwise operators and what are they used for?
Java operators are an essential tool for manipulating the programming language. Along with their many other capabilities and functions, they give you the ability to manipulate the smallest possible units of information. These bits (binary digits) have their own numbering system based on the values 0 and 1. If you now want to query binary values bit by bit, you need a Bitwise operator in Java. You can apply this to numeric data types (byte, char, int, short, long).
While you don't need to use this feature every day, it is useful in some cases: for example, if you want to save space when converting datause theXOR operator or if you really want to change a single bit. It is also the basis for all more advanced switching operations, so it is useful to look at how Java Bitwise works fundamentally.
Web hosting with personal advisor!
Powerful, flexible and high-performance web hosting with email box, personal advisor and domain included 1^{time} year !
Domain
SSL Certificate
24/7 Support
What are Java Bitwise Operators?
There are a total of seven different “Bitwise Operators in Java”. For better understanding, we start from the two operands “a” and “b”.
- ~ (complement or “bitwise NOT”) : this operator inverts each bit. A 0 becomes a 1 and a 1 becomes a 0.
- & (“bitwise AND”) : this operator outputs a 1 if both operands are also 1. If not, a 0 is emitted.
- | (“bitwise OR”) : this operator produces a 1 if one of the two operands also has this value.
- ^ (XOR or “bitwise exclusive OR”) : this operator outputs a 0 if both operands have the same value. Otherwise, it produces a 1.
- << (shift left) : this operator moves the operand a b positions to the left. If this results in blanks, they are filled with 0.
- >> (right shift with a sign, arithmetic) : This operator shifts all bits of a b positions to the right. If the bit with the highest value was set even before execution, it remains set even after execution. Negative numbers thus remain negative.
- >>> (right shift without sign, logical) : this operator shifts the bits of a by b positions to the right. Blanks are always filled with 0.
NOT bit by bit
The Java Bitwise NOT operator is represented by a tilde (~). It changes all bits to their opposites and thus transforms zeros into ones and ones into zeros. Let's take the number 20 for our example. As a binary number, it looks like this: 10100. If we now use this operator, it switches each bit of the number. From 10100, we get 01011 using this operator. This is the value of the expression “~20”. If we convert this binary number to decimal, we get the value -21. If you want to try this in code, enter the following and print the output using the Java command System.out.println
.
public class Main {
public static void main(String[] args) {
int Valeur1 = 20;
System.out.println(~Valeur1);
}
}
Java
If you entered everything correctly, the output should now show “-21”.
AND bitwise
The bitwise AND operator compares two numbers in their binary form bit by bit. So the first bit of the first number is mapped to the first bit of the second number, the second bit to the second bit, and so on. If both bits are 1, a 1 is also output. If they are not because either both bits or only one bit is 0, a 0 is output. Let's take the two decimal numbers 18 and 25 for our example. 18 is 10010 as a binary number. 25 is 11001 in binary notation. Now we compare these two numbers to get a third number. We write them one after the other for a better overview:
18 = 10010 25 = 11001
The first two bits are 1 in both cases. So the number we are looking for starts with 1. Although the second bit of 25 is also 1, the second bit of 18 is 0, which is why the third digit continues with 0. So we go through both numbers bit by bit and finally get the binary number 10000. If we convert it to decimal, our output is 16.
The code looks like this:
public class Main {
public static void main(String[] args) {
System.out.println(18&25);
}
}
Java
The output printed to the console should now be “16”.
OR bitwise
The Java Bitwise OR operator also compares two numbers bit by bit. In this case, however, only one of the two operands must have the value 1 to obtain the result 1. If we take the numbers 18 and 25 from the previous example, the operation looks like this:
18 = 10010 25 = 11001
Since all bits except the third digit contain at least one 1, this third number results in: 11011. We thus obtain 27 after conversion.
Here is the corresponding code:
public class Main {
public static void main(String[] args) {
System.out.println(18|25);
}
}
Java
XOR
XOR or the “bitwise exclusive OR” operator (^) is similar to bitwise OR. While one or both operands must be 1 for this, XOR only outputs a 1 if exactly one of the two values is also 1. Example:
18 = 10010 25 = 11001
Since the first two bits have a value of 1, Bitwise Operator in Java translates to 0. The second bit of 18 is 0, the second bit of 25 is 1. This results in a value of 1. If we continue, we get the third digit 01011. In its decimal form, it is 11.
Here is the appropriate code:
public class Main {
public static void main(String[] args) {
System.out.println(18^25);
}
}
Java
Shift left
To shift left, move the bits of value a by the distance b to the left. resulting blanks are filled with 0. This is perfectly illustrated by an int value that occupies 32 bits in a memory. Let's take the number a 20 or 10010. If we shift this by the value b 2, we get the value c 1001000. So two zeros are placed at the end. 1001000 corresponds to the decimal value 72.
Here is how this operation looks in the code:
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 2;
int c = (a << b);
System.out.println(c);
}
}
Java
Right shift with sign
Right shift works exactly the opposite way. We shift the bits of value a by value b to the right and thus obtain c. last bits are omitted. In our example, if we move the number 20 or 10010 two places to the right, the result will be 100 or 4.
Here is the code to try:
public class Main {
public static void main(String[] args) {
System.out.println(20 >> 2);
}
}
Java
It is important to note here that the blanks are filled with 0 if a is a positive number. If it is negative, the blanks are replaced with 1.
Right shift without sign
The Java Bitwise Unsigned Right Shift operator (>>>) works in principle the same way. The only difference is that the blanks created on the left by a right shift are always filled with 0. This always results in positive numbers at the end, even if the value was previously negative.