Bitwise Operators in JAVA

Post on 06-May-2015

115 views 0 download

description

Bitwise operators in java by ali asghar manjotho

transcript

Bitwise Operators in JAVA

Miscellaneous Lecture by Ali Asghar Manjotho

Lecturer, Department of Computer Systems Engineering, MUET-Jamshoro.

1

Contents

Bitwise Operators

Bitwise Logical Operators

AND ( & )

OR ( | )

XOR ( ^ )

NOT ( ~ )

Bitwise Shift Operators

Shift Left ( << )

Shift Right Zero Fill ( >>> )

Shit Right ( >> ) 2

Bitwise Operators

Bitwise operators, unlike the other operators, act up on individual bits of their operands.

Can be performed on Boolean and Integer data types as: byte, char, short, int and long.

3

Bitwise Logical Operators

4

Bitwise Logical Operators

They perform logical operation on the corresponding bits of the two operands.

Following are the bitwise logical operators:

AND (&)

OR ( | )

NOT ( ~ )

XOR ( ^ ) 5

AND ( & )

It performs logical AND operation on each of the corresponding bits of the two operands as shown below:

A B A & B

0 0 0

0 1 0

1 0 0

1 1 1

6

AND ( & ) : Example 01

7

AND ( & ) : Example 01

A 0 0 0 0 1 1 0 0 +12

B 0 0 0 0 0 1 0 1 +5

C = A & B 0 0 0 0 0 1 0 0 +4

A = +12 = 00001100

B = +5 = 00000101

8

AND ( & ) : Example 02

9

AND ( & ) : Example 02

A 0 0 0 1 1 1 0 0 +28

B 0 0 1 1 0 0 1 0 +50

C = A & B 0 0 0 1 0 0 0 0 +16

A = +28 = 00011100

B = +50 = 00110010

10

AND ( & ) : Example 03

11

AND ( & ) : Example 3

A 1 1 1 1 0 1 1 0 -10

B 1 1 1 1 1 0 1 1 -5

C = A & B 1 1 1 1 0 0 1 0 -14

For A+10 = 0001010

1110101 1’s Complement+1

-10 = 1110110 2’s Complement-10 = 11110110

For B+5 = 0000101

1111010 1’s Complement+1

-5 = 1111011 2’s Complement-5 = 11111011

For C11110010 (Sign + Magnitude)

1110010 (Magnitude)0001101 1’s Complement

+10001110 2’s Complement

0001110 = 14

(C = -14)

12

OR ( | )

It performs logical OR operation on each of the corresponding bits of the two operands as shown below:

A B A | B

0 0 0

0 1 1

1 0 1

1 1 1

13

OR ( | ) : Example 01

14

OR ( | ) : Example 01

A 0 0 0 0 1 1 0 0 +12

B 0 0 0 0 0 1 0 1 +5

C = A | B 0 0 0 0 1 1 0 1 +13

A = +12 = 00001100

B = +5 = 00000101

15

OR ( | ) : Example 02

16

OR ( | ) : Example 02

A 0 0 0 1 1 1 0 0 +28

B 0 0 1 1 0 0 1 0 +50

C = A | B 0 0 1 1 1 1 1 0 +62

A = +28 = 00011100

B = +50 = 00110010

17

OR ( | ) : Example 03

18

OR ( | ) : Example 03

A 1 1 1 1 0 1 1 0 -10

B 1 1 1 1 1 0 1 1 -5

C = A | B 1 1 1 1 1 1 1 1 -1

For A+10 = 0001010

1110101 1’s Complement+1

-10 = 1110110 2’s Complement-10 = 11110110

For B+5 = 0000101

1111010 1’s Complement+1

-5 = 1111011 2’s Complement-5 = 11111011

For C11111111 (Sign + Magnitude)

1111111 (Magnitude)0000000 1’s Complement

+10000001 2’s Complement

0000001 = 1

(C = -1)

19

XOR ( ^ )

It performs logical XOR operation on each of the corresponding bits of the two operands as shown below:

A B A ^ B

0 0 0

0 1 1

1 0 1

1 1 0

20

XOR ( ^ ) : Example 01

21

XOR ( ^ ) : Example 01

A 0 0 0 0 1 1 0 0 +12

B 0 0 0 0 0 1 0 1 +5

C = A ^ B 0 0 0 0 1 0 0 1 +9

A = +12 = 00001100

B = +5 = 00000101

22

XOR ( ^ ) : Example 02

23

XOR ( ^ ) : Example 02

A 0 0 0 1 1 1 0 0 +28

B 0 0 1 1 0 0 1 0 +50

C = A ^ B 0 0 1 0 1 1 1 0 +46

A = +28 = 00011100

B = +50 = 00110010

24

XOR ( ^ ) : Example 03

25

XOR ( ^ ) : Example 03

A 1 1 1 1 0 1 1 0 -10

B 1 1 1 1 1 0 1 1 -5

C = A ^ B 0 0 0 0 1 1 0 1 +13

For A+10 = 0001010

1110101 1’s Complement+1

-10 = 1110110 2’s Complement-10 = 11110110

For B+5 = 0000101

1111010 1’s Complement+1

-5 = 1111011 2’s Complement-5 = 11111011

For C00001101 (Sign + Magnitude)

0001101 (Magnitude)

0001101 = 13

(C = +13)

26

NOT ( ~ )

It performs logical NOT operation on each of the bit of its operand as shown below:

It inverts the bits (Converts all 1s in to 0s and all 0s in to 1s).

A ~A

0 1

1 0

27

NOT ( ~ ) : Example 01

28

NOT ( ~ ) : Example 01

A 0 0 0 0 1 1 0 0 +12

B = ~A 1 1 1 1 0 0 1 1 -13

A = +12 = 00001100

For B11110011 (Sign + Magnitude)1110011 (Magnitude)0001100 1’s Complement

+10001101 2’s Complement

0001101 = 13

(C = -13) 29

NOT ( ~ ) : Example 02

30

NOT ( ~ ) : Example 02

A 0 0 0 1 1 1 0 0 +28

B = ~A 1 1 1 0 0 0 1 1 -29

A = +28 = 00011100

For B11100011 (Sign + Magnitude)1100011 (Magnitude)0011100 1’s Complement

+10011101 2’s Complement

0011101 = 29

(C = -29) 31

NOT ( ~ ) : Example 03

32

NOT ( ~ ) : Example 03

A 1 1 1 1 0 1 1 0 -10

B = ~A 0 0 0 0 1 0 0 1 +9

For A+10 = 0001010

1110101 1’s Complement+1

-10 = 1110110 2’s Complement-10 = 11110110

For B00001001 (Sign + Magnitude)0001001 (Magnitude)

0001001 = 9

(C = +9)

33

Bitwise Shift Operators

34

Bitwise Shift Operators

They shift the corresponding bits of the operand left or right.

First operand is the number to be shifted.

Second operand tells the number of bits to shift.

Following are the bitwise shift operators:

Shift Left (<<)

Shift Right Zero Fill ( >>> )

Shift Right ( >> ) 35

Shift Left ( << )

It shifts the bits of first operand to left.

The second operand tells the number of bits to be shifted.

After performing shift left operation, the empty bits left at the right side will always be filled with 0s.

The result of shifting one bit to left will result multiplication by 2.

36

Shift Left ( << ) : Example 01

37

Shift Left ( << ) : Example 01

A 0 0 0 0 1 1 0 0 +12

0 0 1 1 0 0 Shifting left 2 bits

0 0 1 1 0 0 0 0 Fill empty bits with 0s

B = A<<2 0 0 1 1 0 0 0 0 +48

A = +12 = 00001100

38

Shift Left ( << ) : Example 02

39

Shift Left ( << ) : Example 02

A 0 0 0 1 0 1 1 0 +22

1 0 1 1 0 Shifting left 3 bits

1 0 1 1 0 0 0 0 Fill empty bits with 0s

B = A<<3 1 0 1 1 0 0 0 0 -80

A = +22 = 00010110

For B:101100000110000 (Magnitude)1001111 1’s Complement

+1 2’s Complement1010000

1010000 = 80(B = -80)

40

Shift Left ( << ) : Example 03

41

Shift Left ( << ) : Example 03

A 1 1 1 1 0 1 1 0 -10

1 1 0 1 1 0 Shifting left 2 bits

1 1 0 1 1 0 0 0 Fill empty bits with 0s

B = A<<2 1 1 0 1 1 0 0 0 -40

For A+10 = 0001010

1110101 1’s Complement+1

-10 = 1110110 2’s Complement-10 = 11110110

For B11011000 (Sign + Magnitude)1011000 (Magnitude)0100111 1’s Complement

+10101000 2’s Complement

0101000 = 40

(B = -40)

42

Shift Right Zero Fill ( >>> )

It shifts the bits of first operand to right.

The second operand tells the number of bits to be shifted.

After performing shift right zero fill operation, the empty bits left at the left side will always be filled with 0s.

It does not take care about the sign of the number. 43

Shift Right Zero Fill ( >>> ) : Example 01

44

Shift Right Zero Fill ( >>> ) : Example 01

A 0 0 0 0 1 1 0 0 +12

0 0 0 0 1 1 Shifting right 2 bits

0 0 0 0 0 0 1 1 Fill empty bits with 0s

B = A>>>2 0 0 0 0 0 0 1 1 +3

A = +12 = 00001100

For B:00000011 = +3

45

Shift Right Zero Fill ( >>> ) : Example 02

46

Shift Right Zero Fill ( >>> ) : Example 02

A 0 0 0 1 0 1 1 0 +22

0 0 0 1 0 Shifting right 3 bits

0 0 0 0 0 0 1 0 Fill empty bits with 0s

B = A>>>3 0 0 0 0 0 0 1 0 +2

A = +22 = 00010110

For B:00000010 = +2

(B = +2)

47

Shift Right ( >> )

It shifts the bits of first operand to right.

The second operand tells the number of bits to be shifted.

After performing shift right operation, the empty bits left at the left side will be filled according to the sign bit of the number.

If sign bit is 0 all the empty bits will be filled with 0s.

If sign bit is 1 all the empty bits will be filled with 1s.

It does care about the sign of the number.

The result of shifting one bit to right will result division by 2. 48

Shift Right ( >> ) : Example 01

49

Shift Right ( >> ) : Example 01

A 0 0 1 1 1 1 0 0 +60

0 0 1 1 1 Shifting right 3 bits

0 0 0 0 0 1 1 1 Fill empty bits with sign bit

B = A>>3 0 0 0 0 0 1 1 1 +7

A = +60 = 00111100

For B:00000111 = +7

50

Shift Right ( >> ) : Example 02

51

Shift Right ( >> ) : Example 02

A 1 1 1 1 0 1 1 0 -10

1 1 1 1 0 1 Shifting right 2 bits

1 1 1 1 1 1 0 1 Fill empty bits with sign bit

B = A>>2 1 1 1 1 1 1 0 1 -3

For A:+10 = 0001010

1110101 1’s Complement+1

1110110 2’s Complement-10 = 11110110

For B:111111011111101 (Magnitude)0000010 1’s Complement

+10000011 2’s Complement

0000011 = 3

(B = -3)

52