097_bit_manipulation.zig 3.6 KB

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