summaryrefslogtreecommitdiff
path: root/patches
diff options
context:
space:
mode:
authorJustus Klausecker <justus@klausecker.de>2026-03-12 22:26:04 +0100
committerJustus Klausecker <justus@klausecker.de>2026-03-13 11:21:03 +0100
commit16a794fbeef0c1d98069dc825e55af38945b5165 (patch)
treeb1d2c108a0fcb0c88c4f388d44aa9bb769bb12bc /patches
parent973ec410977b5fd15cf25b4385a7f5b6250ad1c6 (diff)
111/112: Add exercises for packed structs/unions
The first exercise introduces the `packed` keyword as an alternative for bitwise operations. Its main goals are establishing a solid understanding of field order and conveying the fact that packed containers are basically integers. It introduces the concept of container layouts and briefly explains the default `auto` layout before introducing the `packed` layout (but doesn't touch `extern` at all). The exercise also presents a real-world use case for packed containers, namely LZ4 frame descriptors. Furthermore it covers equality comparisons between packed containers. The second exercise talks about switch statements with packed containers and goes into some more detail on packed unions.
Diffstat (limited to 'patches')
-rw-r--r--patches/patches/111_packed.patch57
-rw-r--r--patches/patches/112_packed2.patch32
2 files changed, 89 insertions, 0 deletions
diff --git a/patches/patches/111_packed.patch b/patches/patches/111_packed.patch
new file mode 100644
index 0000000..d38ac68
--- /dev/null
+++ b/patches/patches/111_packed.patch
@@ -0,0 +1,57 @@
+--- exercises/111_packed.zig 2026-03-13 11:18:44
++++ answers/111_packed.zig 2026-03-13 11:18:57
+@@ -41,7 +41,7 @@
+
+ const PackedStruct = packed struct {
+ a: u2,
+- b: u?,
++ b: u4,
+ };
+
+ comptime {
+@@ -50,7 +50,7 @@
+
+ const PackedUnion = packed union {
+ a: bool,
+- b: u?,
++ b: u1,
+ };
+
+ comptime {
+@@ -113,31 +113,31 @@
+ pub fn main() void {
+ {
+ const expected: Bits = @bitCast(@as(u4, 0b1000));
+- const my_bits: Bits = .{};
++ const my_bits: Bits = .{ .d = 1 };
+ if (my_bits != expected) complain(my_bits, expected, @src());
+ }
+
+ {
+ const expected: Bits = @bitCast(@as(u4, 0b0001));
+- const my_bits: Bits = .{};
++ const my_bits: Bits = .{ .a = 1 };
+ if (my_bits != expected) complain(my_bits, expected, @src());
+ }
+
+ {
+ const expected: Bits = @bitCast(@as(u4, 0b0010));
+- const my_bits: Bits = .{};
++ const my_bits: Bits = .{ .b = 1 };
+ if (my_bits != expected) complain(my_bits, expected, @src());
+ }
+
+ {
+ const expected: Bits = @bitCast(@as(u4, 0b0011));
+- const my_bits: Bits = .{};
++ const my_bits: Bits = .{ .a = 1, .b = 1 };
+ if (my_bits != expected) complain(my_bits, expected, @src());
+ }
+
+ {
+ const expected: Bits = @bitCast(@as(u4, 0b1101));
+- const my_bits: Bits = .{};
++ const my_bits: Bits = .{ .a = 1, .c = 1, .d = 1 };
+ if (my_bits != expected) complain(my_bits, expected, @src());
+ }
+ }
diff --git a/patches/patches/112_packed2.patch b/patches/patches/112_packed2.patch
new file mode 100644
index 0000000..9b01eb3
--- /dev/null
+++ b/patches/patches/112_packed2.patch
@@ -0,0 +1,32 @@
+--- exercises/112_packed2.zig 2026-03-13 11:14:08
++++ answers/112_packed2.zig 2026-03-13 11:14:16
+@@ -13,9 +13,9 @@
+ const s: S = .{ .a = true, .b = -1 };
+ switch (s) {
+ .{ .a = true, .b = -1 } => {}, // ok!
+- .{ .a = true, .b = ??? },
+- .{ .a = ???, .b = 0 },
+- .{ .a = ???, .b = ??? },
++ .{ .a = true, .b = 0 },
++ .{ .a = false, .b = 0 },
++ .{ .a = false, .b = -1 },
+ => @compileError("We don't want to end up here!"),
+ }
+ }
+@@ -39,7 +39,6 @@
+ .{ .a = 3 } => {}, // ok!
+ .{ .a = 2 },
+ .{ .b = 1 },
+- .{ .b = -1 },
+ .{ .a = 0 },
+ => @compileError("We don't want to end up here!"),
+ }
+@@ -65,7 +64,7 @@
+ // Reminder: if the sign bit of a float is set, the number is negative!
+
+ var number: Float = .{ .value = 2.34 };
+- number.bits.??? = ???;
++ number.bits.sign = 1;
+ if (number.value != -2.34) {
+ std.debug.print("Make it negative!\n", .{});
+ }