bitwise operators in java

52
Bitwise Operators in JAVA Miscellaneous Lecture by Ali Asghar Manjotho Lecturer, Department of Computer Systems Engineering, MUET-Jamshoro. 1

Upload: ali-asghar-manjotho

Post on 06-May-2015

115 views

Category:

Engineering


0 download

DESCRIPTION

Bitwise operators in java by ali asghar manjotho

TRANSCRIPT

Page 1: Bitwise Operators in JAVA

Bitwise Operators in JAVA

Miscellaneous Lecture by Ali Asghar Manjotho

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

1

Page 2: Bitwise Operators in JAVA

Contents

Bitwise Operators

Bitwise Logical Operators

AND ( & )

OR ( | )

XOR ( ^ )

NOT ( ~ )

Bitwise Shift Operators

Shift Left ( << )

Shift Right Zero Fill ( >>> )

Shit Right ( >> ) 2

Page 3: Bitwise Operators in JAVA

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

Page 4: Bitwise Operators in JAVA

Bitwise Logical Operators

4

Page 5: Bitwise Operators in JAVA

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

Page 6: Bitwise Operators in JAVA

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

Page 7: Bitwise Operators in JAVA

AND ( & ) : Example 01

7

Page 8: Bitwise Operators in JAVA

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

Page 9: Bitwise Operators in JAVA

AND ( & ) : Example 02

9

Page 10: Bitwise Operators in JAVA

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

Page 11: Bitwise Operators in JAVA

AND ( & ) : Example 03

11

Page 12: Bitwise Operators in JAVA

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

Page 13: Bitwise Operators in JAVA

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

Page 14: Bitwise Operators in JAVA

OR ( | ) : Example 01

14

Page 15: Bitwise Operators in JAVA

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

Page 16: Bitwise Operators in JAVA

OR ( | ) : Example 02

16

Page 17: Bitwise Operators in JAVA

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

Page 18: Bitwise Operators in JAVA

OR ( | ) : Example 03

18

Page 19: Bitwise Operators in JAVA

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

Page 20: Bitwise Operators in JAVA

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

Page 21: Bitwise Operators in JAVA

XOR ( ^ ) : Example 01

21

Page 22: Bitwise Operators in JAVA

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

Page 23: Bitwise Operators in JAVA

XOR ( ^ ) : Example 02

23

Page 24: Bitwise Operators in JAVA

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

Page 25: Bitwise Operators in JAVA

XOR ( ^ ) : Example 03

25

Page 26: Bitwise Operators in JAVA

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

Page 27: Bitwise Operators in JAVA

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

Page 28: Bitwise Operators in JAVA

NOT ( ~ ) : Example 01

28

Page 29: Bitwise Operators in JAVA

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

Page 30: Bitwise Operators in JAVA

NOT ( ~ ) : Example 02

30

Page 31: Bitwise Operators in JAVA

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

Page 32: Bitwise Operators in JAVA

NOT ( ~ ) : Example 03

32

Page 33: Bitwise Operators in JAVA

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

Page 34: Bitwise Operators in JAVA

Bitwise Shift Operators

34

Page 35: Bitwise Operators in JAVA

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

Page 36: Bitwise Operators in JAVA

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

Page 37: Bitwise Operators in JAVA

Shift Left ( << ) : Example 01

37

Page 38: Bitwise Operators in JAVA

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

Page 39: Bitwise Operators in JAVA

Shift Left ( << ) : Example 02

39

Page 40: Bitwise Operators in JAVA

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

Page 41: Bitwise Operators in JAVA

Shift Left ( << ) : Example 03

41

Page 42: Bitwise Operators in JAVA

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

Page 43: Bitwise Operators in JAVA

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

Page 44: Bitwise Operators in JAVA

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

44

Page 45: Bitwise Operators in JAVA

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

Page 46: Bitwise Operators in JAVA

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

46

Page 47: Bitwise Operators in JAVA

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

Page 48: Bitwise Operators in JAVA

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

Page 49: Bitwise Operators in JAVA

Shift Right ( >> ) : Example 01

49

Page 50: Bitwise Operators in JAVA

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

Page 51: Bitwise Operators in JAVA

Shift Right ( >> ) : Example 02

51

Page 52: Bitwise Operators in JAVA

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