Slide 1

Slide 1 text

Bit Manipulation 101 0 1 0 1 1 0 1 1 0 0 0 1 2 3 4 5 6 7 8 0 0 0 0 1 1 1 1 Left << Right >> Shift AND & OR | NOT ~ XOR ^ Bitwise

Slide 2

Slide 2 text

Binary Representation …and Decimal, Hexadecimal Conversions 0 0 0 0 0 0 0 0 Decimal: 0 Hex: 0x00 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8

Slide 3

Slide 3 text

Binary Representation 0 0 0 0 0 0 0 1 Decimal: 1 Hex: 0x01 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 4

Slide 4 text

Binary Representation 0 0 0 0 0 0 1 0 Decimal: 2 Hex: 0x02 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 5

Slide 5 text

Binary Representation 0 0 0 0 0 0 1 1 Decimal: 3 Hex: 0x03 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 6

Slide 6 text

Binary Representation 0 0 0 0 0 1 0 0 Decimal: 4 Hex: 0x04 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 7

Slide 7 text

Binary Representation 0 0 0 0 0 1 0 1 Decimal: 5 Hex: 0x05 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 8

Slide 8 text

Binary Representation 0 0 0 0 0 1 1 0 Decimal: 6 Hex: 0x06 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 9

Slide 9 text

Binary Representation 0 0 0 0 0 1 1 1 Decimal: 7 Hex: 0x07 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 10

Slide 10 text

Binary Representation 0 0 0 0 1 0 0 0 Decimal: 8 Hex: 0x08 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 11

Slide 11 text

Binary Representation 0 0 0 0 1 0 0 1 Decimal: 9 Hex: 0x09 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 12

Slide 12 text

Binary Representation 0 0 0 0 1 0 1 0 Decimal: 10 Hex: 0x0A 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 13

Slide 13 text

Binary Representation 0 0 0 0 1 0 1 1 Decimal: 11 Hex: 0x0B 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 14

Slide 14 text

Binary Representation 0 0 0 0 1 1 0 0 Decimal: 12 Hex: 0x0C 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 15

Slide 15 text

Binary Representation 0 0 0 0 1 1 0 1 Decimal: 13 Hex: 0x0D 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 16

Slide 16 text

Binary Representation 0 0 0 0 1 1 1 0 Decimal: 14 Hex: 0x0E 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 17

Slide 17 text

Binary Representation 0 0 0 0 1 1 1 1 Decimal: 15 Hex: 0x0F 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 18

Slide 18 text

Binary Representation 0 0 0 1 0 0 0 0 Decimal: 16 Hex: 0x10 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 …and Decimal, Hexadecimal Conversions

Slide 19

Slide 19 text

Binary Representation 0 0 0 1 0 0 0 1 Decimal: 17 Hex: 0x11 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 $ python >>> hex(17) # decimal to hex 0x11 >> 0x11 # Hex to decimal 17 >>> bin(17) # decimal to binary 0b10001 >>> 0b10001 # binary to decimal 17 …and Decimal, Hexadecimal Conversions

Slide 20

Slide 20 text

Binary Representation 0 0 0 1 0 0 1 0 Decimal: 18 Hex: 0x12 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 $ python >>> hex(18) # decimal to hex 0x12 >> 0x12 # Hex to decimal 18 >>> bin(18) # decimal to binary 0b10010 >>> 0b10010 # binary to decimal 18 …and Decimal, Hexadecimal Conversions

Slide 21

Slide 21 text

Binary Representation 0 0 0 1 0 0 1 1 Decimal: 19 Hex: 0x13 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 $ python >>> hex(19) # decimal to hex 0x13 >> 0x13 # Hex to decimal 19 >>> bin(19) # decimal to binary 0b10011 >>> 0b10011 # binary to decimal 19 …and Decimal, Hexadecimal Conversions

Slide 22

Slide 22 text

Binary Representation 0 0 1 0 0 0 0 0 Decimal: 32 Hex: 0x20 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 $ python >>> hex(32) # decimal to hex 0x20 >> 0x20 # Hex to decimal 32 >>> bin(32) # decimal to binary 0b100000 >>> 0b100000 # binary to decimal 32 …and Decimal, Hexadecimal Conversions

Slide 23

Slide 23 text

Binary Representation 0 1 0 0 0 0 0 0 Decimal: 64 Hex: 0x40 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 $ python >>> hex(64) # decimal to hex 0x40 >> 0x40 # Hex to decimal 64 >>> bin(64) # decimal to binary 0b1000000 >>> 0b1000000 # binary to decimal 64 …and Decimal, Hexadecimal Conversions

Slide 24

Slide 24 text

Binary Representation 1 0 0 0 0 0 0 0 Decimal: 128 Hex: 0x80 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 $ python >>> hex(128) # decimal to hex 0x80 >> 0x80 # Hex to decimal 128 >>> bin(128) # decimal to binary 0b10000000 >>> 0b10000000 # binary to decimal 128 …and Decimal, Hexadecimal Conversions

Slide 25

Slide 25 text

Binary Representation 1 1 1 1 1 1 1 1 Decimal: 255 Hex: 0xFF 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 $ python >>> hex(255) # decimal to hex 0xff >> 0xff # Hex to decimal 255 >>> bin(255) # decimal to binary 0b11111111 >>> 0b11111111 # binary to decimal 255 …and Decimal, Hexadecimal Conversions

Slide 26

Slide 26 text

Shift Left/Right Bit shifting moves the bits in a binary number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 0 0 0 1 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 1 = 2 * 1 = 1

Slide 27

Slide 27 text

Shift Left/Right Bit shifting moves the bits in a binary number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 0 0 1 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 2 = 2 * 2 = 4

Slide 28

Slide 28 text

Shift Left/Right Bit shifting moves the bits in a binary number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 0 1 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 3 = 2 * 4 = 8

Slide 29

Slide 29 text

Shift Left/Right Bit shifting moves the bits in a binary number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 1 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 4 = 2 * 8 = 16

Slide 30

Slide 30 text

Shift Left/Right Bit shifting moves the bits in a binary number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 1 0 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 5 = 2 * 16 = 32

Slide 31

Slide 31 text

Shift Left/Right Bit shifting moves the bits in a binary number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 1 0 0 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 6 = 2 * 32 = 64

Slide 32

Slide 32 text

Shift Left/Right Bit shifting moves the bits in a binary number left or right. This is useful for tasks like multiplying or dividing by powers of two. 1 0 0 0 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 7 = 2 * 64 = 128

Slide 33

Slide 33 text

0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 2 4 8 16 32 64 128 1 value = 1 << 0 value = 1 * 1 value = 1 The Value to Shift
 0bit to the left 0 Shift Left Multiply by Powers of Two 8

Slide 34

Slide 34 text

Shift Left 0 0 0 0 0 0 1 0 2 4 8 16 32 64 128 1 value = 1 << 1 value = 1 * 2 value = 2 The Value to Shift
 1bit to the left Multiply by Powers of Two 0 1 2 3 4 5 6 7 8

Slide 35

Slide 35 text

0 0 0 0 0 1 0 0 2 4 8 16 32 64 128 1 value = 1 << 2 value = 1 * 4 value = 4 The Value to Shift
 2bit to the left Shift Left Multiply by Powers of Two 0 1 2 3 4 5 6 7 8

Slide 36

Slide 36 text

0 0 0 0 1 0 0 0 2 4 8 16 32 64 128 1 value = 1 << 3 value = 1 * 8 value = 8 The Value to Shift
 3bit to the left Shift Left Multiply by Powers of Two 0 1 2 3 4 5 6 7 8

Slide 37

Slide 37 text

0 0 0 1 0 0 0 0 2 4 8 16 32 64 128 1 value = 1 << 4 value = 1 * 16 value = 16 The Value to Shift
 4bit to the left Shift Left Multiply by Powers of Two 0 1 2 3 4 5 6 7 8

Slide 38

Slide 38 text

0 0 1 0 0 0 0 0 2 4 8 16 32 64 128 1 value = 1 << 5 value = 1 * 32 value = 32 The Value to Shift
 5bit to the left Shift Left Multiply by Powers of Two 0 1 2 3 4 5 6 7 8

Slide 39

Slide 39 text

0 1 0 0 0 0 0 0 2 4 8 16 32 64 128 1 value = 1 << 6 value = 1 * 64 value = 64 The Value to Shift
 6bit to the left Shift Left Multiply by Powers of Two 0 1 2 3 4 5 6 7 8

Slide 40

Slide 40 text

1 0 0 0 0 0 0 0 2 4 8 16 32 64 128 1 value = 1 << 7 value = 1 * 128 value = 128 The Value to Shift
 7bit to the left Shift Left Multiply by Powers of Two 0 1 2 3 4 5 6 7 8

Slide 41

Slide 41 text

0 0 0 0 1 1 0 0 2 4 8 16 32 64 128 1 value = 3 << 2 value = 3 * 4 value = 12 The Value to Shift
 2bit to the left Shift Left Multiply by Powers of Two 3 = 0b11 0 1 2 3 4 5 6 7 8

Slide 42

Slide 42 text

0 0 1 1 0 0 0 0 value = 3 << 4 value = 3 * 16 value = 48 The Value to Shift
 4bit to the left Shift Left Multiply by Powers of Two 2 4 8 16 32 64 128 1 3 = 0b11 0 1 2 3 4 5 6 7 8

Slide 43

Slide 43 text

0 0 0 1 0 1 0 0 2 4 8 16 32 64 128 1 value = 5 << 2 value = 5 * 4 value = 20 Shift Left Multiply by Powers of Two 5 = 0b101 The Value to Shift
 2bit to the left 0 1 2 3 4 5 6 7 8

Slide 44

Slide 44 text

0 1 0 1 0 0 0 0 value = 5 << 4 value = 5 * 16 value = 80 The Value to Shift
 4bit to the left Shift Left Multiply by Powers of Two 2 4 8 16 32 64 128 1 5 = 0b101 0 1 2 3 4 5 6 7 8

Slide 45

Slide 45 text

A Practical Examples On which days is the shop open? 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 46

Slide 46 text

A Practical Examples On which days is the shop open? 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 Bit Shifting openDays[] = { MONDAY, TUESDAY, WEDNESDAY, FRIDAY, SATURDAY };

Slide 47

Slide 47 text

A Practical Examples 0 0 0 0 0 0 1 0 Monday value |= (1 << 1); // Monday On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 48

Slide 48 text

A Practical Examples 0 0 0 0 0 1 1 0 Monday Tuesday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 49

Slide 49 text

A Practical Examples 0 0 0 0 1 1 1 0 Monday Tuesday Wednesday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 50

Slide 50 text

A Practical Examples 0 0 0 0 1 1 1 0 Monday Tuesday Wednesday Thursday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday value |= (0 << 4); // Thursday (Closed) On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 51

Slide 51 text

A Practical Examples 0 0 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday value |= (0 << 4); // Thursday (Closed) value |= (1 << 5); // Friday On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 52

Slide 52 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday value |= (0 << 4); // Thursday (Closed) value |= (1 << 5); // Friday value |= (1 << 6); // Saturday On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 53

Slide 53 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday value |= (0 << 4); // Thursday (Closed) value |= (1 << 5); // Friday value |= (1 << 6); // Saturday value |= (0 << 7); // Sunday (Closed) On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 54

Slide 54 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday value |= (0 << 4); // Thursday (Closed) value |= (1 << 5); // Friday value |= (1 << 6); // Saturday value |= (0 << 7); // Sunday (Closed) 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1 Bitwise OR 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 1 0 1 1 1 0 1 0 The result in each position is: - 0 if both bits are 0 - otherwise the result is 1 | On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 55

Slide 55 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday value |= (0 << 4); // Thursday (Closed) value |= (1 << 5); // Friday value |= (1 << 6); // Saturday value |= (0 << 7); // Sunday (Closed) 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1 Bitwise OR 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 1 0 1 1 1 0 1 0 The result in each position is: - 0 if both bits are 0 - otherwise the result is 1 | On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 56

Slide 56 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday value |= (0 << 4); // Thursday (Closed) value |= (1 << 5); // Friday value |= (1 << 6); // Saturday value |= (0 << 7); // Sunday (Closed) 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1 Bitwise OR 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 1 0 1 1 1 0 1 0 The result in each position is: - 0 if both bits are 0 - otherwise the result is 1 | On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting

Slide 57

Slide 57 text

Bit Masking A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday 0 1 2 3 4 5 6 7 8 Is the shop open on Mondays?

Slide 58

Slide 58 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday Bitwise AND The result in each position is: - 1 if both bits are 1 - otherwise the result is 0 & 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 1 2 3 4 5 6 7 8 Bit Masking Is the shop open on Mondays?

Slide 59

Slide 59 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday Bitwise AND The result in each position is: - 1 if both bits are 1 - otherwise the result is 0 & 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 1 2 3 4 5 6 7 8 Bit Masking Is the shop open on Mondays?

Slide 60

Slide 60 text

A Practical Examples Is the shop open on Mondays? 0 1 1 0 1 1 1 0 Monday Tuesday Wednesday Thursday Friday Saturday Sunday Bitwise AND The result in each position is: - 1 if both bits are 1 - otherwise the result is 0 & 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 1 2 3 4 5 6 7 8 Bit Masking

Slide 61

Slide 61 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Is the shop open on Mondays? 0 1 2 3 4 5 6 7 8 Bit Masking

Slide 62

Slide 62 text

A Practical Examples 0 1 1 0 1 1 1 0 Monday Is the shop open on Mondays? 0 0 0 0 0 0 1 0 if ((value & (1 << 1)) == (1 << 1)) { // OPEN on mondays } else { // closed on mondays } 0 1 2 3 4 5 6 7 8 Bit Masking 0 0 0 0 0 0 1 0

Slide 63

Slide 63 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Tuesdays? 0 0 0 0 0 1 0 0 if ((value & (1 << 2)) == (1 << 2)) { // OPEN on Tuesdays } else { // closed on Tuesdays } Tuesday 0 1 2 3 4 5 6 7 8 Bit Masking 0 0 0 0 0 1 0 0

Slide 64

Slide 64 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Wednesdays? 0 0 0 0 1 0 0 0 if ((value & (1 << 3)) == (1 << 3)) { // OPEN on Wednesday } else { // closed on Wednesday } Wednesday 0 1 2 3 4 5 6 7 8 Bit Masking 0 0 0 0 1 0 0 0

Slide 65

Slide 65 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Thursdays? 0 0 0 1 0 0 0 0 if ((value & (1 << 4)) == (1 << 4)) { // Open on Thursdays } else { // CLOSED on Thursdays } Thursday 0 1 2 3 4 5 6 7 8 Bit Masking 0 0 0 0 0 0 1 0

Slide 66

Slide 66 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Fridays? 0 0 1 0 0 0 0 0 if ((value & (1 << 5)) == (1 << 5)) { // OPEN on Friday } else { // Closed on Friday } Friday 0 1 2 3 4 5 6 7 8 Bit Masking 0 0 1 0 0 0 0 0

Slide 67

Slide 67 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Saturdays? 0 1 0 0 0 0 0 0 if ((value & (1 << 6)) == (1 << 6)) { // OPEN on Saturday } else { // Closed on Saturday } Saturday 0 1 2 3 4 5 6 7 8 Bit Masking 0 1 0 0 0 0 0 0

Slide 68

Slide 68 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Sundays? 1 0 0 0 0 0 0 0 if ((value & (1 << 7)) == (1 << 7)) { // Open on Sunday } else { // CLOSED on Sunday } Sunday 0 1 2 3 4 5 6 7 8 Bit Masking 0 0 0 0 0 0 0 0

Slide 69

Slide 69 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Weekends? Saturday Sunday 0 1 2 3 4 5 6 7 8 Bit Masking

Slide 70

Slide 70 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Weekends? 1 1 0 0 0 0 0 0 Saturday Sunday r = value & (3 << 6); 0 1 2 3 4 5 6 7 8 Bit Masking

Slide 71

Slide 71 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Weekends? 1 1 0 0 0 0 0 0 Saturday Sunday r = value & (3 << 6); if (r == (3 << 6)) { // Open on Weekends (Saturday and Sunday) } 0 1 2 3 4 5 6 7 8 Bit Masking 0 1 0 0 0 0 0 0

Slide 72

Slide 72 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Weekends? 1 1 0 0 0 0 0 0 Saturday Sunday r = value & (3 << 6); if (r == (3 << 6)) { // Open on Weekends (Saturday and Sunday) } else if (r != 0) { // Open Saturday or Sunday } 0 1 2 3 4 5 6 7 8 Bit Masking 0 1 0 0 0 0 0 0

Slide 73

Slide 73 text

A Practical Examples 0 1 1 0 1 1 1 0 Is the shop open on Weekends? 1 1 0 0 0 0 0 0 Saturday Sunday r = value & (3 << 6); if (r == (3 << 6)) { // Open on Weekends (Saturday and Sunday) } else if (r != 0) { // Open Saturday or Sunday } else { // Closed on Weekends } 0 1 2 3 4 5 6 7 8 Bit Masking 0 1 0 0 0 0 0 0

Slide 74

Slide 74 text

Shift Right Divide by Powers of Two 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 // two small numbers // range 0-15 (4bit) int valueA = 9; // 0b1001 int valueB = 11 // 0b1011

Slide 75

Slide 75 text

Shift Right Divide by Powers of Two 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 // two small numbers // range 0-15 (4bit) int valueA = 9; // 0b1001 int valueB = 11 // 0b1011

Slide 76

Slide 76 text

0 0 0 0 Shift Right Divide by Powers of Two 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 r = 0; r |= 9 << 0;

Slide 77

Slide 77 text

Shift Right Divide by Powers of Two 1 0 1 1 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 Second Value: 11 r = 0; r |= 9 << 0; r |= 11 << 4;

Slide 78

Slide 78 text

Shift Right Divide by Powers of Two 1 0 1 1 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 Second Value: 11

Slide 79

Slide 79 text

Shift Right Divide by Powers of Two 1 0 1 1 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 Second Value: 11 0 0 0 0 1 1 1 1 r = 185 // 0b10111001 valueA = r & 15;

Slide 80

Slide 80 text

Shift Right Divide by Powers of Two 1 0 1 1 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 Second Value: 11 0 0 0 0 1 1 1 1 0 0 0 0 1 0 0 1 r = 185 // 0b10111001 valueA = r & 15; // valueA = 9

Slide 81

Slide 81 text

Shift Right Divide by Powers of Two 1 0 1 1 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 Second Value: 11 1 1 1 1 0 0 0 0 r = 185 // 0b10111001 valueA = r & 15; // valueA = 9 valueB = r & 240;

Slide 82

Slide 82 text

Shift Right Divide by Powers of Two 1 0 1 1 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 Second Value: 11 1 1 1 1 0 0 0 0 1 0 1 1 0 0 0 0 r = 185 // 0b10111001 valueA = r & 15; // valueA = 9 valueB = r & 240; // valueB = 176

Slide 83

Slide 83 text

Shift Right Divide by Powers of Two 1 0 1 1 1 0 0 1 0 1 2 3 4 5 6 7 8 First Value: 9 Second Value: 11 1 1 1 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 r = 185 // 0b10111001 valueA = r & 15; // valueA = 9 valueB = (r & 240) >> 4; // valueB = 11

Slide 84

Slide 84 text

0 1 0 1 1 0 1 1 value = 91 >> 0 value = 91 / 1 value = 91 The Value to Shift
 0bit to the right Shift Right Divide by Powers of Two 91 = 0b1011011 1 2 3 4 5 6 7 8

Slide 85

Slide 85 text

0 1 0 1 1 0 1 1 value = 91 >> 1 value = 91 / 2 value = 45 The Value to Shift
 1bit to the right Shift Right Divide by Powers of Two 91 = 0b1011011 0 1 2 3 4 5 6 7 8

Slide 86

Slide 86 text

0 1 0 1 1 0 1 1 value = 91 >> 2 value = 91 / 4 value = 23 The Value to Shift
 2bit to the right Shift Right Divide by Powers of Two 91 = 0b1011011 0 0 1 2 3 4 5 6 7 8

Slide 87

Slide 87 text

0 1 0 1 1 0 1 1 value = 91 >> 3 value = 91 / 8 value = 11 The Value to Shift
 3bit to the right Shift Right Divide by Powers of Two 91 = 0b1011011 0 0 0 1 2 3 4 5 6 7 8

Slide 88

Slide 88 text

BitWise Operators 8 bits 16 bits 64 bits 32 bits 8 bytes 7 bytes 6 bytes 5 bytes 4 bytes 3 bytes 2 bytes 1 bytes 8bit 16bit 24bit 32bit 40bit 48bit 56bit 64bit 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 8, 16, 32, 64bits

Slide 89

Slide 89 text

BitWise Operators 04 BD 87 43 C5 3D 05 82 8 16 24 32 40 48 56 64 8 bytes

Slide 90

Slide 90 text

BitWise Operators 04 BD 87 43 C5 3D 05 82 8 16 24 32 40 48 56 64 8 bytes 1 1 0 0 0 1 0 1 1 2 3 4 5 6 7 8 0

Slide 91

Slide 91 text

BitWise Operators 0 0 0 04 BD 87 43 C5 8 16 24 32 40 48 56 64 1 1 0 0 0 1 0 1 3D 05 82 1 2 3 4 5 6 7 8 0

Slide 92

Slide 92 text

BitWise Operators 00 00 00 04 BD 87 43 C5 8 16 24 32 40 48 56 64 1 1 0 0 0 1 0 1 3D 05 82 00 00 00 00 00 00 00 FF 1 2 3 4 5 6 7 8 0 v = 0x04BD8743C53D0582 v4 = (v >> 24) & 0xff

Slide 93

Slide 93 text

BitWise Operators 00 00 00 04 BD 87 43 C5 8 16 24 32 40 48 56 64 1 1 0 0 0 1 0 1 3D 05 82 00 00 00 00 00 00 00 FF 00 00 00 00 00 00 00 C5 1 2 3 4 5 6 7 8 0 v = 0x04BD8743C53D0582 v4 = (v >> 24) & 0xff // v4 = 0xC5

Slide 94

Slide 94 text

BitWise Operators 00 00 00 04 BD 87 43 C5 8 16 24 32 40 48 56 64 1 1 0 0 0 1 0 1 3D 05 82 00 00 00 00 00 00 00 0F 00 00 00 00 00 00 00 05 1 2 3 4 5 6 7 8 0 v = 0x04BD8743C53D0582 v4 = (v >> 24) & 0x0f // v4 = 0x05

Slide 95

Slide 95 text

Mega, Giga, Tera …and the power of Twos 48128 >> 10 // 47 (KiB) 28311552 >> 20 // 27 (MiB) 8589934592 >> 30 // 8 (GiB) From bytes to “human size” 1 << 10 // 1KiB 57 << 10 // 57KiB 1 << 20 // 1MiB 32 << 20 // 32MiB 1 << 30 // 1GiB 16 << 30 // 16GiB 1 << 40 // 1TiB 12 << 40 // 12TiB easy way to calculate bytes with the specified unit 10:KiB, 20:MiB, 30:GiB, 40:TiB, 50:PiB, 60EiB

Slide 96

Slide 96 text

BitMask 1 0 0 0 0 0 0 0 2 4 8 16 32 64 128 1 value = (1 << 7) value = (1 * 128) value = 128 0 1 2 3 4 5 6 7 8

Slide 97

Slide 97 text

BitMask 0 1 1 1 1 1 1 1 2 4 8 16 32 64 128 1 value = (1 << 7) - 1 value = (1 * 128) - 1 value = 127 0 1 2 3 4 5 6 7 8

Slide 98

Slide 98 text

BitMask 0 0 0 1 0 0 0 0 2 4 8 16 32 64 128 1 value = (1 << 4) value = (1 * 16) value = 16 0 1 2 3 4 5 6 7 8

Slide 99

Slide 99 text

BitMask 0 0 0 0 1 1 1 1 2 4 8 16 32 64 128 1 value = (1 << 4) - 1 value = (1 * 16) - 1 value = 15 0 1 2 3 4 5 6 7 8

Slide 100

Slide 100 text

BitWise XOR Invert The Bit Bitwise XOR The result in each position is: - 0 if both bits are the same - otherwise the result is 1 ^ 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 1 0 0 1 0 0 1 0 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0 v = 0b101010 v ^= 1 << 2 // 0b101110 v ^= 1 << 2 // 0b101010

Slide 101

Slide 101 text

Bitwise NOT Inverts all the bits 0 0 0 0 1 0 1 0 0 1 2 3 4 5 6 7 0b1010

Slide 102

Slide 102 text

BitMask Inverts all the bits 0 0 0 0 1 0 1 0 0 1 2 3 4 5 6 7 ~0b1010 0b1010

Slide 103

Slide 103 text

BitMask Inverts all the bits 0b1010 0 0 0 0 1 0 1 0 0 1 2 3 4 5 6 7 ~0b1010 1 1 1 1 0 1 0 1

Slide 104

Slide 104 text

Bitmap (BitSet) 0 1 2 3 4 5 6 7 uint8_t bitmap[8] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Unlimited Bits

Slide 105

Slide 105 text

Bitmap (BitSet) Set Bit 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 uint8_t bitmap[8] Unlimited Bits

Slide 106

Slide 106 text

Bitmap (BitSet) Set Bit 11 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] wordIndex = (11 >> 3) Unlimited Bits

Slide 107

Slide 107 text

Bitmap (BitSet) Set Bit 11 // same as (11 / 8) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] wordIndex = (11 >> 3) Unlimited Bits

Slide 108

Slide 108 text

Bitmap (BitSet) Set Bit 11 wordIndex = (11 >> 3) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] bitIndex = (11 & 7) Unlimited Bits

Slide 109

Slide 109 text

Bitmap (BitSet) Set Bit 11 wordIndex = (11 >> 3) // same as (11 % 8) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] bitIndex = (11 & 7) Unlimited Bits

Slide 110

Slide 110 text

Bitmap (BitSet) Set Bit 11 wordIndex = (11 >> 3) bitIndex = (11 & 7) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 uint8_t bitmap[8] bitmap[wordIndex] |= (1 << bitIndex) Unlimited Bits

Slide 111

Slide 111 text

Bitmap (BitSet) Set Bit 11 wordIndex = (11 >> 3) bitIndex = (11 & 7) bitmap[wordIndex] |= (1 << bitIndex) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] Unlimited Bits

Slide 112

Slide 112 text

Bitmap (BitSet) Set Bit 63 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] Unlimited Bits

Slide 113

Slide 113 text

Bitmap (BitSet) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] Set Bit 63 wordIndex = (63 >> 3) Unlimited Bits

Slide 114

Slide 114 text

Bitmap (BitSet) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 uint8_t bitmap[8] Set Bit 63 wordIndex = (63 >> 3) bitIndex = (63 & 7) Unlimited Bits

Slide 115

Slide 115 text

Bitmap (BitSet) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 uint8_t bitmap[8] Set Bit 63 wordIndex = (63 >> 3) bitIndex = (63 & 7) bitmap[wordIndex] |= (1 << bitIndex) Unlimited Bits

Slide 116

Slide 116 text

Bitmap (BitSet) Get Bit 37 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] Unlimited Bits

Slide 117

Slide 117 text

Bitmap (BitSet) Get Bit 37 wordIndex = (37 >> 3) bitIndex = (37 & 7) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] Unlimited Bits

Slide 118

Slide 118 text

Bitmap (BitSet) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] 0 Get Bit 37 wordIndex = (37 >> 3) bitIndex = (37 & 7) v = bitmap[wordIndex] & (1 << bitIndex) // v is 0: so, is not set… Unlimited Bits

Slide 119

Slide 119 text

Bitmap (BitSet) Get Bit 11 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] Unlimited Bits

Slide 120

Slide 120 text

Bitmap (BitSet) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] Get Bit 11 wordIndex = (11 >> 3) bitIndex = (11 & 7) Unlimited Bits

Slide 121

Slide 121 text

Bitmap (BitSet) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] Get Bit 11 wordIndex = (11 >> 3) bitIndex = (11 & 7) v = bitmap[wordIndex] & (1 << bitIndex) // v is 8: so, is set… 1 Unlimited Bits

Slide 122

Slide 122 text

Bitmap (BitSet) Clear Bit 11 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] Unlimited Bits

Slide 123

Slide 123 text

Bitmap (BitSet) Clear Bit 11 wordIndex = (11 >> 3) bitIndex = (11 & 7) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] Unlimited Bits

Slide 124

Slide 124 text

Bitmap (BitSet) Clear Bit 11 wordIndex = (11 >> 3) bitIndex = (11 & 7) bitmap[wordIndex] &= ~(1 << bitIndex) 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 uint8_t bitmap[8] 0 Unlimited Bits

Slide 125

Slide 125 text

0 0 0 0 0 0 0 0 2 4 8 16 32 64 128 1 value = 0 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 126

Slide 126 text

0 0 0 1 0 0 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1 Bitwise OR 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 127

Slide 127 text

0 0 1 0 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1 Bitwise OR 1 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 128

Slide 128 text

0 0 0 1 1 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true Bitwise AND 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 129

Slide 129 text

0 0 1 1 0 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true is_bit5_set = (value & (1 << 4)) != 0 // false Bitwise AND 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 130

Slide 130 text

0 0 1 0 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true is_bit5_set = (value & (1 << 4)) != 0 // false are_bit3and4_set = (value & (3 << 2)) == (3 << 2) // true 0 1 2 3 4 5 6 7 8 1 Bitwise Operations Set, Clear, Check, Invert Bitwise AND 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1

Slide 131

Slide 131 text

0 0 1 0 0 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true is_bit5_set = (value & (1 << 4)) != 0 // false are_bit3and4_set = (value & (3 << 2)) == (3 << 2) // true value &= ~(1 << 3) // clear bit 4 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 132

Slide 132 text

0 0 1 0 1 0 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true is_bit5_set = (value & (1 << 4)) != 0 // false are_bit3and4_set = (value & (3 << 2)) == (3 << 2) // true value &= ~(1 << 3) // clear bit 4 is_bit4_set = (value & (1 << 3)) != 0 // false 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 133

Slide 133 text

0 0 1 0 1 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true is_bit5_set = (value & (1 << 4)) != 0 // false are_bit3and4_set = (value & (3 << 2)) == (3 << 2) // true value &= ~(1 << 3) // clear bit 4 is_bit4_set = (value & (1 << 3)) != 0 // false value ^= (1 << 3) // invert bit 4, from 0 to 1 Bitwise XOR 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 134

Slide 134 text

0 0 1 0 1 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true is_bit5_set = (value & (1 << 4)) != 0 // false are_bit3and4_set = (value & (3 << 2)) == (3 << 2) // true value &= ~(1 << 3) // clear bit 4 is_bit4_set = (value & (1 << 3)) != 0 // false value ^= (1 << 3) // invert bit 4 is_bit4_set = (value & (1 << 3)) != 0 // true 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert

Slide 135

Slide 135 text

0 0 1 0 1 1 0 0 2 4 8 16 32 64 128 1 value = 0 value |= (1 << 5) // set bit 6 value |= (3 << 2) // set bit 3 and 4 is_bit6_set = (value & (1 << 5)) != 0 // true is_bit5_set = (value & (1 << 4)) != 0 // false are_bit3and4_set = (value & (3 << 2)) == (3 << 2) // true value &= ~(1 << 3) // clear bit 4 is_bit4_set = (value & (1 << 3)) != 0 // false value ^= (1 << 3) // invert bit 4 is_bit4_set = (value & (1 << 3)) != 0 // true 0 1 2 3 4 5 6 7 8 Bitwise Operations Set, Clear, Check, Invert