097_bit_manipulation.zig 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Bit manipulation is a very powerful tool, also from Zig.
  3. // Since the dawn of the computer age, numerous algorithms have been
  4. // developed that solve tasks solely by moving, setting, or logically
  5. // combining bits.
  6. //
  7. // Zig also uses direct bit manipulation in its standard library for
  8. // functions where possible. And it is often possible with calculations
  9. // based on integers.
  10. //
  11. // At first glance, it is often not easy to understand what exactly these
  12. // algorithms do when only "numbers" in memory areas change outwardly.
  13. // However, it should never be forgotten that the numbers only represent
  14. // the interpretation of the bit sequences.
  15. //
  16. // Quasi the reversed case we have otherwise, namely that we represent
  17. // numbers in bit sequences.
  18. //
  19. // We remember: 1 byte = 8 bits = 11111111 = 255 decimal = FF hex.
  20. //
  21. // Zig provides all the necessary functions to change the bits inside
  22. // a variable. It is distinguished whether the bit change leads to an
  23. // overflow or not. The details are in the Zig documentation in section
  24. // "Table of Operators".
  25. //
  26. // Here are some examples of how the bits of variables can be changed:
  27. //
  28. // const numOne: u8 = 15; // = 0000 1111
  29. // const numTwo: u8 = 245; // = 1111 0101
  30. //
  31. // const res1 = numOne >> 4; // = 0000 0000 - shift right
  32. // const res2 = numOne << 4; // = 1111 0000 - shift left
  33. // const res3 = numOne & numTwo; // = 0000 0101 - and
  34. // const res4 = numOne | numTwo; // = 1111 1111 - or
  35. // const res5 = numOne ^ numTwo; // = 1111 1010 - xor
  36. //
  37. //
  38. // To familiarize ourselves with bit manipulation, we start with a simple
  39. // but often underestimated function and then add other exercises in
  40. // loose order.
  41. //
  42. // The following text contains excerpts from Wikipedia.
  43. //
  44. // Swap
  45. // In computer programming, the act of swapping two variables refers to
  46. // mutually exchanging the values of the variables. Usually, this is
  47. // done with the data in memory. For example, in a program, two variables
  48. // may be defined thus (in pseudocode):
  49. //
  50. // data_item x := 1
  51. // data_item y := 0
  52. //
  53. // swap (x, y);
  54. //
  55. // After swap() is performed, x will contain the value 0 and y will
  56. // contain 1; their values have been exchanged. The simplest and probably
  57. // most widely used method to swap two variables is to use a third temporary
  58. // variable:
  59. //
  60. // define swap (x, y)
  61. // temp := x
  62. // x := y
  63. // y := temp
  64. //
  65. // However, with integers we can also achieve the swap function simply by
  66. // bit manipulation. To do this, the variables are mutually linked with xor
  67. // and the result is the same.
  68. const std = @import("std");
  69. const print = std.debug.print;
  70. pub fn main() !void {
  71. // Let us use 1101 and 1011 as values for x and y
  72. var x: u8 = 0b1101;
  73. var y: u8 = 0b1011;
  74. // Now we swap the values of the two variables by doing xor on them
  75. x ^= y;
  76. y ^= x;
  77. // What must be written here?
  78. ???;
  79. print("x = {b}; y = {b}\n", .{ x, y });
  80. }
  81. // This variable swap takes advantage of the fact that the value resulting
  82. // from the xor of two values contains both of these values.
  83. // This circumstance was (and still is) sometimes used for encryption.
  84. // Value XOR Key = Crypto. => Crypto XOR Key = Value.
  85. // Since this can be swapped arbitrarily, you can swap two variables in this way.
  86. //
  87. // For Crypto it is better not to use this, but in sorting algorithms like
  88. // Bubble Sort it works very well.