064_builtins.zig 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 first builtin, alphabetically, is:
  29. //
  30. // @addWithOverflow(comptime T: type, a: T, b: T, result: *T) bool
  31. // * 'T' will be the type of the other parameters.
  32. // * 'a' and 'b' are numbers of the type T.
  33. // * 'result' is a pointer to space you're providing of type T.
  34. // * The return value is true if the addition resulted in a
  35. // value over or under the capacity of type T.
  36. //
  37. // Let's try it with a tiny 4-bit integer size to make it clear:
  38. const a: u4 = 0b1101;
  39. const b: u4 = 0b0101;
  40. var my_result: u4 = undefined;
  41. var overflowed: bool = undefined;
  42. overflowed = @addWithOverflow(u4, a, b, &my_result);
  43. //
  44. // The print() below will produce: "1101 + 0101 = 0010 (true)".
  45. // Let's make sense of this answer by counting up from 1101:
  46. //
  47. // Overflowed?
  48. // 1101 + 1 = 1110 No.
  49. // 1110 + 1 = 1111 No.
  50. // 1111 + 1 = 0000 Yes! (Real answer is 10000)
  51. // 0000 + 1 = 0001 No.
  52. // 0001 + 1 = 0010 No.
  53. //
  54. // Also, check out our fancy formatting! b:0>4 means, "print
  55. // as a binary number, zero-pad right-aligned four digits."
  56. print("{b:0>4} + {b:0>4} = {b:0>4} ({})", .{ a, b, my_result, overflowed });
  57. print(". Furthermore, ", .{});
  58. // Here's a fun one:
  59. //
  60. // @bitReverse(comptime T: type, integer: T) T
  61. // * 'T' will be the type of the input and output.
  62. // * 'integer' is the value to reverse.
  63. // * The return value will be the same type with the
  64. // value's bits reversed!
  65. //
  66. // Now it's your turn. See if you can fix this attempt to use
  67. // this builtin to reverse the bits of a u8 integer.
  68. const input: u8 = 0b11110000;
  69. const tupni: u8 = @bitReverse(input);
  70. print("{b:0>8} backwards is {b:0>8}.\n", .{ input, tupni });
  71. }