Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
281 views

Bitwise Operators in Java

The document discusses different bitwise operators in Java - bitwise AND, OR, XOR, complement, left shift and right shift. It explains what each operator does at the bit level and provides examples of operations on various binary numbers. The key bitwise operators are: - Bitwise AND returns 1 only if both bits are 1, else 0. - Bitwise OR returns 1 if either bit is 1, else 0. - Bitwise XOR returns 1 if the bits are different, else 0. - Complement inverts all bits. - Left shift multiplies the number by a power of 2. Right shift divides by a power of 2. - Right shift fills with 0 for unsigned shift,

Uploaded by

Prabir Kamila
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
281 views

Bitwise Operators in Java

The document discusses different bitwise operators in Java - bitwise AND, OR, XOR, complement, left shift and right shift. It explains what each operator does at the bit level and provides examples of operations on various binary numbers. The key bitwise operators are: - Bitwise AND returns 1 only if both bits are 1, else 0. - Bitwise OR returns 1 if either bit is 1, else 0. - Bitwise XOR returns 1 if the bits are different, else 0. - Complement inverts all bits. - Left shift multiplies the number by a power of 2. Right shift divides by a power of 2. - Right shift fills with 0 for unsigned shift,

Uploaded by

Prabir Kamila
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Bitwise operators in Java

Bitwise operators are used to perform manipulation of individual bits of a number. They
can be used with any of the integral types (char, short, int, etc). They are used when
performing update and query operations of Binary indexed tree.
Bitwise OR (|) –
This operator is binary operator, denoted by ‘|’. It returns bit by bit OR of input
values, i.e, if either of the bits is 1, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise OR Operation of 5 and 7


0101
| 0111
________
0111 = 7 (In decimal)
Bitwise AND (&) –
This operator is binary operator, denoted by ‘&’. It returns bit by bit AND of input
values, i.e, if both bits are 1, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise AND Operation of 5 and 7


0101
& 0111
________
0101 = 5 (In decimal)
Bitwise XOR (^) –
This operator is binary operator, denoted by ‘^’. It returns bit by bit XOR of input
values, i.e, if corresponding bits are different, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7


0101
^ 0111
________
0010 = 2 (In decimal)
Bitwise Complement (~) –
This operator is unary operator, denoted by ‘~’. It returns the one’s compliment
representation of the input value, i.e, with all bits inversed, means it makes every 0
to 1, and every 1 to 0.
For example,

a = 5 = 0101 (In Binary)

Bitwise Compliment Operation of 5

~ 0101
________
1010 = 10 (In decimal)
Note – Compiler will give 2’s complement of that number, i.e., 2’s compliment of 10
will be -6.
// Java program to illustrate
// bitwise operators
public class operators {
public static void main(String[] args)
{
//Initial values
int a = 5;
int b = 7;

// bitwise and
// 0101 & 0111=0101 = 5
System.out.println("a&b = " + (a & b));

// bitwise or
// 0101 | 0111=0111 = 7
System.out.println("a|b = " + (a | b));

// bitwise xor
// 0101 ^ 0111=0010 = 2
System.out.println("a^b = " + (a ^ b));

// bitwise and
// ~0101=1010
// will give 2's complement of 1010 = -6
System.out.println("~a = " + ~a);

// can also be combined with


// assignment operator to provide shorthand
// assignment
// a=a&b
a &= b;
System.out.println("a= " + a);
}
}
Output :
a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5
Shift Operators: These operators are used to shift the bits of a number left or right
thereby multiplying or dividing the number by two respectively. They can be used when
we have to multiply or divide a number by two. General format:
number shift_op number_of_places_to_shift;
Left shift operator (<<) –
Shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect
as of multiplying the number with some power of two.
For example,
a = 5 = 0000 0101
b = -10 = 1111 0110

a << 1 = 0000 1010 = 10


a << 2 = 0001 0100 = 20

b << 1 = 0000 1010 = -20


b << 2 = 0001 0100 = -40
Unsigned Left shift operator (<<<) –
There is no “<<<" operator in Java, because the logical (<<) and arithmetic left-shift
(<<<) operations are identical.
Signed Right shift operator (>>) –
Shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost
bit depends on the sign of initial number. Similar effect as of dividing the number with
some power of two.
For example,
a = 5 = 0000 0101
b = 10 = 0000 1010 (takes 2's compliment)
b = -10 = 1111 0110

b >> 1 = 1111 1011 = -5


b >> 2 = 1111 1101 (takes 2's compliment) = -3
Unsigned Right shift operator (>>>) –
Shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost
bit is set to 0. (>>>) is unsigned-shift; it’ll insert 0. (>>) is signed, and will extend the sign
bit.
For example,
a = 5 = 0000 0101
b = 10 = 0000 1010 (takes 2's compliment)
b = -10 = 1111 0110

b >> 1 = 1
b >> 2 = (takes 2's compliment) = 1073741821
// Java program to illustrate
// shift operators
public class operators {
public static void main(String args[])
{

int a = 5;
int b = -10;

// left shift operator


// 0000 0101<<2 =0001 0100(20)
// similar to 5*(2^2)
System.out.println("a<<2 = " + (a << 2));

// right shift operator


// 0000 0101 >> 2 =0000 0001(1)
// similar to 5/(2^2)
System.out.println("b>>2 = " + (b >> 2));

// unsigned right shift operator


System.out.println("b>>>2 = " + (b >>> 2));
}
}
Output :
a<<2 = -3
b>>>2 = 1073741821

You might also like