Java Fundamentals
Bitwise Operators
A bitwise operator manipulates individual bits of its operands.
All bitwise operators work with only integers.
The bitwise AND (&) operator
operates on corresponding bits of its two operands and returns 1 if both bits are 1, and 0
otherwise.
Note that the bitwise AND (&) operates on each bit of the respective operands, not on the
operands as a whole.
The following is the result of all bit combination using the bitwise AND (&) operator:
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
Consider the following piece of code in Java:
int i = 13 & 3;
The value of 13 & 3 is computed as follows.
The 32 bits have been shown in 8-bit chunks for clarity.
In memory, all 32 bits are contiguous.
13 00000000 00000000 00000000 00001101
3 00000000 00000000 00000000 00000011
-------------------------------------------------------
13 & 3 - 00000000 00000000 00000000 00000001 (Equal to decimal 1)
Therefore, 13 & 3 is 1, which is assigned to i in the above piece of code.
The bitwise OR (|)
operates on corresponding bits of its operands and returns 1 if either bit is 1, and 0 otherwise.
The following is the result of all bit combinations using bitwise OR (|) operator:
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
DTTH Page 1
Java Fundamentals
13 00000000 00000000 00000000 00001101
3 00000000 00000000 00000000 00000011
----------------------------------------------------------
13 | 3 00000000 00000000 00000000 00001111 (Equal to decimal 15)
The bitwise XOR (^)
operates on corresponding bits of its operands and returns 1 if only one of the bits is 1.
Otherwise, it returns 0. The following is the result of all bit combinations using bitwise XOR (^)
operator:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
The value of 13 ^ 3 can be computed as follows. The result of 13 ^ 3 is 14.
13 00000000 00000000 00000000 00001101
3 00000000 00000000 00000000 00000011
--------------------------------------------------------
13 ^ 3 00000000 00000000 00000000 00001110 (Equal to decimal 14)
Bitwise operators compare two variables bit-by-bit.
class BitwiseTest
{
public static void main(String[] args)
{
byte b1 = 6&8; //0110 & 1000
System.out.println(b1);
byte b2 = 7|9; //0111 | 1001
System.out.println(b2);
byte b3 = 5^4; //0101 0100
System.out.println(b3);
byte b4 = 6;
byte b5 = 8;
//b1 = b4&b5;
b1 = (byte)(b4&b5);
System.out.println(b1);
}
}
The bitwise NOT (~)
operates on each bit of its operand. It inverts the bits, that is, 1 is changed to 0 and 0 is changed to
1.
It is also called a bitwise complement operator.
It computes 1’s complement of its operand.
The following is the result of all bit combinations using bitwise NOT (~) operator:
~1 = 0
~0 = 1
The value of ~13 can be computed as follows. The result of ~13 is -14.
13 00000000 00000000 00000000 00001101
-------------------------------------------------------
~13 11111111 11111111 11111111 11110010 (Equal to decimal -14)
DTTH Page 2
Java Fundamentals
The bitwise left shift operator (<<)
shifts all the bits to the left by the number of bits specified as its right-hand operand.
It inserts zeros at the lower-order bits.
The effect of shifting 1 bit to left is same as multiplying the number by 2.
Therefore, 9 << 1 will produce 18, whereas 9 << 2 produces 36.
13 << 35 can be considered as 13 << (35 % 32) which is the same as 13 << 3.
long val = 13;
long result;
result = val << 35; //val is a long variable, ,val<<35 ,
val<<35%64
System.out.println(result);
The bitwise signed right shift operator (>>)
shifts all the bits to the right by the number specified as its right-hand operand.
If the most significant digit of the left-hand operand is 1 (for negative numbers), all higher order
bits are filled with 1s after the shift operation.
If the most significant bit is 0 (for positive numbers), all higher order bits are filled with 0s.
Because the sign bit after right shift operation (>>) remains the same, it is called a signed right
shift operator.
For example, 13 >> 4 results in zero, as depicted in Figure. Also note that in the case of -13 >> 4
all four higher order bits are filled with 1s because in -13, the most significant digit is 1. The
result of -13 >> 4 is -1.
With the left shift operator, the result is exactly the same as multiplying the number being shifted
by 2 to the power of the number of bits to shift.
shifting x<<3 is the same as saying x*23
A right shift operator is actually causing the number being shifted to be divided by 2 to the power
of the number of bits to shift.
shifting x>>4 is exactly the same as saying x/24. And x>>8 is exactly the same as x/28.
DTTH Page 3
Java Fundamentals
The unsigned right shift operator (>>>)
works the same as the signed right shift operator (>>), except for one difference.
It always fills the higher order bits with zero.
The result of 13 >>> 4 is zero whereas the result of -13 >>> 4 is 268435455, as shown below.
There is no unsigned left shift operator.
13 00000000 00000000 00000000 00001101
13 >>> 4 00000000 00000000 00000000 00000000 1101
-13 11111111 11111111 11111111 11110011
-13 >>> 4 00001111 11111111 11111111 11111111 0011
class BinaryTest
{
public static void main(String[] args)
{
int a = 60; //0011 1100
int b = 13; //0000 1101
System.out.println("a=" + a + " : " + Integer.toBinaryString(a));
System.out.println("b=" + b + " : " + Integer.toBinaryString(b));
int c = 0;
c = a & b; //12=0000 1100
System.out.println("a & b =" + c + " : " + Integer.toBinaryString(c));
c = a | b; //61=0011 1101
System.out.println("a | b =" + c + " : " + Integer.toBinaryString(c));
c = a ^ b; //49=0011 0001
System.out.println("a ^ b =" + c + " : " + Integer.toBinaryString(c));
c = ~a; //-61=1100 0011
System.out.println("~a =" + c + " : " + Integer.toBinaryString(c));
c = a<<2; //240=1111 0000
System.out.println("a<<2 =" + c + " : " + Integer.toBinaryString(c));
a = -10;
System.out.println("a=" + a + " : " + Integer.toBinaryString(a));
c = a>>2; //-3 -3 : 1111 1111 1111 1111 1111 1111 1111 1101
System.out.println("a>>2 =" + c + " : " + Integer.toBinaryString(c));
}
Compound bitwise assignment operator
operand1 op= operand2
op is one of the bitwise operators of &, |, ^, <>, and >>>.
operand1 and operand2 are of primitive integral data type where operand1 must be a variable.
Assuming that there are two int variables, i and j, Table 4-8 lists the equivalent expression for
compound bitwise assignment operators.
DTTH Page 4
Java Fundamentals
public class CompoundBitwiseAssignment
{
public static void main(String args[]) {
int a = 10;
int b = 20;
int c = 0;
c = a + b;
System.out.println("c = a + b = " + c );
c += a ;
System.out.println("c += a = " + c );
c -= a ;
System.out.println("c -= a = " + c );
c *= a ;
System.out.println("c *= a = " + c );
a = 10;
c = 15;
c /= a ;
System.out.println("c /= a = " + c );
a = 10;
c = 15;
c %= a ;
System.out.println("c %= a = " + c );
c <<= 2 ;
System.out.println("c <<= 2 = " + c );
c >>= 2 ;
System.out.println("c >>= 2 = " + c );
c >>= 2 ;
System.out.println("c >>= 2 = " + c );
DTTH Page 5
Java Fundamentals
c &= a ;
System.out.println("c &= a = " + c );
c ^= a ;
System.out.println("c ^= a = " + c );
c |= a ;
System.out.println("c |= a = " + c );
}
}
/*
Output
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a = 0
c ^= a = 10
c |= a = 10
*/
Operator Precedence
DTTH Page 6
Java Fundamentals
public class OperatorPrecedence
{
public static void main(String args[])
{
System.out.println("I will now count my chickens:");
System.out.println("Hens "+(25+30/6)); //30
System.out.println("Roosters "+(100-25*3%4)); //97
System.out.print("Now I will count the eggs: ");
System.out.println(3+2+1-5+4%2-1/4+6); //7
System.out.print("Is it true that 3+2<5-7? ");
System.out.println(3+2<5-7); //false
System.out.println(11/5%2*3); //0
System.out.println(11*5/5%3); //2
System.out.println(2+3%2-1*8/6); //2
System.out.println(true==false!=true); //true
System.out.println(true!=false==false); //false
System.out.println(true^false|true&false);//true
System.out.println(true^true&true|false);//false
DTTH Page 7
Java Fundamentals
int x = 3+5*6; //33
int y= (3+5)*6; //48
System.out.println(x);
System.out.println(y);
int z = ((3+5)*(6)); //48
System.out.println(z);
int w = 3*4/2*6; //36
System.out.println(w);
x = 3*4/(2*6); //1
System.out.println(x);
y = 3*4+2*6; //24
System.out.println(y);
z = 3*(4+2)*6; //108
System.out.println(z);
}
}
DTTH Page 8