From 6ad9774189fbd64b2f2c9519f4513ab34b0c3809 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Fri, 12 Mar 2021 18:59:46 -0500 Subject: "999 is enough for anybody" triple-zero padding (#18) When I hit 999 exercises, I will finally have reached the ultimate state of soteriological release and no more exercises will be needed. The cycle will be complete. All that will be left is perfect quietude, freedom, and highest happiness. --- exercises/016_for2.zig | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 exercises/016_for2.zig (limited to 'exercises/016_for2.zig') diff --git a/exercises/016_for2.zig b/exercises/016_for2.zig new file mode 100644 index 0000000..0a62a1a --- /dev/null +++ b/exercises/016_for2.zig @@ -0,0 +1,33 @@ +// +// For loops also let you store the "index" of the iteration - a +// number starting with 0 that counts up with each iteration: +// +// for (items) |item, index| { +// +// // Do something with item and index +// +// } +// +// You can name "item" and "index" anything you want. "i" is a popular +// shortening of "index". The item name is often the singular form of +// the items you're looping through. +// +const std = @import("std"); + +pub fn main() void { + // Let's store the bits of binary number 1101 in + // 'little-endian' order (least significant byte first): + const bits = [_]u8{ 1, 0, 1, 1 }; + var value: u32 = 0; + + // Now we'll convert the binary bits to a number value by adding + // the value of the place as a power of two for each bit. + // + // See if you can figure out the missing piece: + for (bits) |bit, ???| { + var place_value = std.math.pow(u32, 2, @intCast(u32, i)); + value += place_value * bit; + } + + std.debug.print("The value of bits '1101': {}.\n", .{value}); +} -- cgit v1.2.3-ZIG