$30 off During Our Annual Pro Sale. View Details »

Mastering Bitwise Operations: Shifting, Masking...

Mastering Bitwise Operations: Shifting, Masking, and More!

Learn about Bit shifting, Bit Masking and the various Bitwise operations and don't be afraid of them!

https://youtu.be/kIzhNtVQ4yw

Matteo Bertozzi

June 02, 2024
Tweet

More Decks by Matteo Bertozzi

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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 };
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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?
  58. 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?
  59. 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?
  60. 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
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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;
  77. 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;
  78. 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
  79. 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;
  80. 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
  81. 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;
  82. 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
  83. 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
  84. 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
  85. 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
  86. 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
  87. 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
  88. 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
  89. BitWise Operators 04 BD 87 43 C5 3D 05 82

    8 16 24 32 40 48 56 64 8 bytes
  90. 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
  91. 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
  92. 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
  93. 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
  94. 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
  95. 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
  96. 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
  97. 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
  98. 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
  99. 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
  100. 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
  101. Bitwise NOT Inverts all the bits 0 0 0 0

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

    0 1 0 0 1 2 3 4 5 6 7 ~0b1010 0b1010
  103. 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
  104. 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
  105. 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
  106. 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
  107. 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
  108. 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
  109. 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
  110. 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
  111. 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
  112. 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
  113. 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
  114. 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
  115. 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
  116. 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
  117. 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
  118. 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
  119. 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
  120. 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
  121. 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
  122. 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
  123. 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
  124. 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
  125. 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
  126. 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
  127. 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
  128. 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
  129. 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
  130. 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
  131. 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
  132. 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
  133. 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
  134. 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
  135. 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