Welcome to another tutorial on the "How to code in Java" tutorial series. In the previous tutorial, I write about the relational, shorthand and logical operators in programming. In this tutorial, I'll continue with another type of important operator in programming. That is the bitwise operators. One thing that I would recommend you doing first before reading this tutorial is to understand the basic of the binary number system.
Index for this tutorial:
- What are bitwise operators?
- What are the types of bitwise operators?
- Example code in Java.
- An explanation of the examples shown.
What are bitwise operators?
Bitwise operators are operators that are used to change the individual bits of a number. These operators could be used with any numeric types variable like char, short, int and many more.
What are the types of bitwise operators?
Example code in Java.
An explanation for the examples shown.
The bitwise OR operator.
The example given is 10 | 12, and the answer produced is 14. This can be explained as below:
10 in binary is 101012 in binary is 1100
The bitwise OR operator copies the number's bits to the result if it exists in either of the two operands. That's why we will get 1110 or 14 after the operation. A way of understanding it is when a value 1 is in either side of the operands, add 1 to the exact position in the result.
1010 | 1100 = 1110 (14)
The bitwise AND operator
The example given is 10 & 12, and the produced answer is 8.
10 -> 101012 -> 1100
For the bitwise AND operator, it'll copy the number's bits to the result only if the bits exist in both of the operands.
1010 & 1100 = 0100 (8)
The bitwise XOR operator.
The XOR operator works differently from the previous two operators. It will only copy bits of the number into the result if only the bits exist in only one of the operand and not in both.
10 -> 101012 -> 11001010 ^ 1100 = 0110 (6)
The bitwise complement operator.
This operator will be a litter tricky, why you ask? Because the function or this operator is flipping the bits of the number (one's complement). 1 will become 0, while 0 will become 1. But the resulted value will be displayed in two's complement number.
10 -> 1010~1010 = 11...10101 (-11)
The signed right-shift operator.
This operator works by shifting the bit pattern to the right. While shifting the bits, it'll insert bits according to its sign bit to the left. The number of bits to be inserted will depend on how many times the bit pattern has shifted. The number of times the bits need to be shifted will be specified by the operand on the right. Besides shifting bits, this operator could also be used in dividing numbers since it can preserve the sign bit of the value. The number can be divided with the number that's some power of 2 (2, 4, 8, 16 and so on).
shifting a negative number:
-10 -> 11...11011011...110110 >> 1 = 11 ...111011 (-5)
shifting a positive number:
10 -> 10101010 >> 1 = 0101 (5)
Dividing number:
8 -> 10001000 >> 1= 8 / 2 power of 1= 0100 (4)1000 >> 2= 8 / 2 power of 2= 0010 (2)
The Unsigned right-shift operator.
This operator works just like the right-shift operator, but it does not preserve the sign bit when shifting the bits. For example:
-10 -> 11...11011011...110110 >>> 1 = 01...111011 (2147483643)
10 -> 10101010 >>> 1 = 101 (5)
As you can see the number losses its sign bit during the shifting of bits. But if it's a positive number, the operation will be the same as performing a normal signed right-shift to the number.
The signed left-shift operator.
This operator works just like the signed right-shift operator, but this operator shifts the bit pattern to the left. When shifting the number, a zero (0) will be inserted to the rightmost position of the value while the leftmost bit will be replaced by the bits shifting. This operator could be used to multiply the value with a number that's some power of 2.
-10 -> 11...110110-10 << 1= -10 * (2 power of 1)= 11...101100 (-20)
10 -> 101010 << 1= 10 * (2 power of 1)= 10100 (20)10 << 2= 10 * (2 power of 2)= 101000 (40)
Bitwise operators are used in many fields like cryptography, compression, network protocols and many more. But they're not commonly used in programmers everyday job. Some other sources also said that using the bitwise right or left shift operator will reduce the operation needed to complete a division or multiplication. Fields like cryptography make use of bitwise operators heavily as cryptography deals with binary numbers, and some cryptography algorithm need access a specific stream of bits in a binary stream for the algorithm to work.
That's all for this tutorial, I hope you now know what the bitwise operators are and how they're used. As always if you find this tutorial helpful, please share it with other people that might find it interesting or they might need it. If you have any questions, comments, critics, feel free to leave a comment below. In the next tutorial, we will finally be going through the conditional statements in Java. So stay tuned.
No comments:
Post a Comment