Today We will Talk About BitWise operator.
If I say "There are 2 bananas" and I want to define with some programming
syntax,
we will write int banana = 2;
Now, what do you think the computer will also understand there are 2 bananas?
Answer is
NO. The computer only operates on binary means they understand only two
bits i.e 0 and 1
The integer 2 is equivalent to 0010 in bits. Now you must be heard
32-bit/64-bit computers. What does this signify? This means the processor can work with 32-bit/64-bit binary numbers.
The bitwise operators manipulate the bits within the integer. There are two types of integers signed and unsigned integers. All of the integer types except char are signed integers. To represent negative numbers in bits there is an encoding concept called two's complement. Let's take an example.
36 => Bitwise Representation
0 0 1 0 0 1 0 0
Now how to represent -36
1st step, filps/inverts all the bits of 36
2nd step, then add 1
1 1 0 1 1 0 1 1
+ 1
____________________
1 1 0 1 1 1 0 0
=>Representation of -36
Let's take a look at different bitwise operators.
Operator | Meaning |
---|---|
~ | Bitwise Unary NOT inverts all of the bits of its operand |
& | Bitwise AND produces a 1 bit if both operands are also 1. A 0 is produced in all other cases. |
| | Bitwise OR combines bits such that if either of the bits in the operands is a 1, then the resultant bit is a 1 |
^ | Bitwise Exclusive OR combines bits such that if exactly one operand is 1, then the result is 1. Otherwise, the result is zero |
>> | Shift Right shifts all of the bits in a value to the right a specified number of times. |
>>> | Shift right zero fill, which always shifts zeros into the high-order bit. |
<< | Shift left shifts all of the bits in a value to the left a specified number of times. |
&= | Bitwise AND assignment |
|= | Bitwise OR assignment |
^= | Bitwise exclusive OR assignment |
>>= | Shift right assignment |
>>>= | Shift right zero fill assignment |
<<= | Shift left assignment |
The Bitwise Logical Operator Truth Table
A | B | A | B | A & B | A ^ B | ~A |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 |
1 | 0 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 | 0 |
Example to illustrate the above concept
public class Main
{
public static void main(String[] args) {
int a = 12;
int b = 15;
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = ~a;
int g = a>>2;
int i = b<<2;
int j = a>>>2;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println(" ~a = " + f);
System.out.println(" a>>2 = " +g);
System.out.println(" b<<2 = " +i);
System.out.println(" a>>>2 = " +j);
}
}
OUTPUT:
a = 12
b = 15
a|b = 15
a&b = 12
a^b = 3
~a = -13
a>>2 = 3
b<<2 = 60
a>>>2 = 3
Quick and Amazing Facts About Bitwise Manipulation:
1.How to find even and odd numbers using the Bitwise operator?
Bitwise AND operator (&) is used here. Because number & 1 = 0 then number is EVEN and number & 0 = 1 then number is ODD.
2.How to calculate NOT of a number?
The formula for calculating NOT is ~ number = - number - 1 For example, what will be the answer of ~49? ~49 = -49-1= -50
3.How to calculate Bit shifts quickly?
There are many ways to do that.
1st approach: In left shift number << k appends k zero (0) bits to the number and in right shift number >> k removes the last k bits from the number where k is the number of bits to be shifted.
There is another way to calculate the right shift of a negative number is, add (1) bits in the right shifted empty spaces. For Example 14(00001110) then 14 << 2 = 56(00111000) [bold 0 are appended] and 14 >> 2 = 3(0000001110) [bold 10 are discarded so the answer is 3]
2nd approach: number << k is equivalent to number * 2^k and number >> k is equivalent to number / 2^k For Example, 32 << 2 = 128 and 32 >> 2 = 8
4.When the bits are lost?
In each left shift operation, the higher-order bit is shifted out and lost and a zero is added on the right. This means if the operand is int, bits are lost once they are shifted past bit position 31. if the operand is long, then after the bit position 63 bits will be lost.
5.Can we replace modulo operator (%) and division operator (/) with some bitwise operator?
Yes, We can.
x & 1 is equivalent to x % 2
x >> 1 is equivalent to x / 2
Hope this tutorial helps you to understand the concept of bitwise operation.😀
Post a Comment
Post a Comment
You are welcome to share your thoughts with us!