064_builtins.zig 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // The Zig compiler provides "builtin" functions. You've already
  3. // gotten used to seeing an @import() at the top of every
  4. // Ziglings exercise.
  5. //
  6. // We've also seen @intCast() in "016_for2.zig", "058_quiz7.zig";
  7. // and @enumToInt() in "036_enums2.zig".
  8. //
  9. // Builtins are special because they are intrinsic to the Zig
  10. // language itself (as opposed to being provided in the standard
  11. // library). They are also special because they can provide
  12. // functionality that is only possible with help from the
  13. // compiler, such as type introspection (the ability to examine
  14. // type properties from within a program).
  15. //
  16. // Zig currently contains 101 builtin functions. We're certainly
  17. // not going to cover them all, but we can look at some
  18. // interesting ones.
  19. //
  20. // Before we begin, know that many builtin functions have
  21. // parameters marked as "comptime". It's probably fairly clear
  22. // what we mean when we say that these parameters need to be
  23. // "known at compile time." But rest assured we'll be doing the
  24. // "comptime" subject real justice soon.
  25. //
  26. const print = @import("std").debug.print;
  27. pub fn main() void {
  28. // The second builtin, alphabetically, is:
  29. // @addWithOverflow(a: anytype, b: anytype) struct { @TypeOf(a, b), u1 }
  30. // * 'T' will be the type of the other parameters.
  31. // * 'a' and 'b' are numbers of the type T.
  32. // * The return value is a tuple with the result and a possible overflow bit.
  33. //
  34. // Let's try it with a tiny 4-bit integer size to make it clear:
  35. const a: u4 = 0b1101;
  36. const b: u4 = 0b0101;
  37. const my_result = @addWithOverflow(a, b);
  38. // Check out our fancy formatting! b:0>4 means, "print
  39. // as a binary number, zero-pad right-aligned four digits."
  40. // The print() below will produce: "1101 + 0101 = 0010 (true)".
  41. print("{b:0>4} + {b:0>4} = {b:0>4} ({s})", .{ a, b, my_result[0], if (my_result[1] == 1) "true" else "false" });
  42. // Let's make sense of this answer. The value of 'b' in decimal is 5.
  43. // Let's add 5 to 'a' but go one by one and see where it overflows:
  44. //
  45. // a | b | result | overflowed?
  46. // ----------------------------------
  47. // 1101 + 0001 = 1110 | false
  48. // 1110 + 0001 = 1111 | false
  49. // 1111 + 0001 = 0000 | true (the real answer is 10000)
  50. // 0000 + 0001 = 0001 | false
  51. // 0001 + 0001 = 0010 | false
  52. //
  53. // In the last two lines the value of 'a' is corrupted because there was
  54. // an overflow in line 3, but the operations of lines 4 and 5 themselves
  55. // do not overflow.
  56. // There is a difference between
  57. // - a value, that overflowed at some point and is now corrupted
  58. // - a single operation that overflows and maybe causes subsequent errors
  59. // In practise we usually notice the overflowed value first and have to work
  60. // our way backwards to the operation that caused the overflow.
  61. //
  62. // If there was no overflow at all while adding 5 to a, what value would
  63. // 'my_result' hold? Write the answer in into 'expected_result'.
  64. const expected_result: u8 = ???;
  65. print(". Without overflow: {b:0>8}. ", .{expected_result});
  66. print("Furthermore, ", .{});
  67. // Here's a fun one:
  68. //
  69. // @bitReverse(integer: anytype) T
  70. // * 'integer' is the value to reverse.
  71. // * The return value will be the same type with the
  72. // value's bits reversed!
  73. //
  74. // Now it's your turn. See if you can fix this attempt to use
  75. // this builtin to reverse the bits of a u8 integer.
  76. const input: u8 = 0b11110000;
  77. const tupni: u8 = @bitReverse(input, tupni);
  78. print("{b:0>8} backwards is {b:0>8}.\n", .{ input, tupni });
  79. }