Skip to content

Commit e4327da

Browse files
committed
feat: add bitmasking example
1 parent 10c0e2d commit e4327da

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

src/examples/networking/http-client.zig

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@ pub fn main() !void {
44
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
55
const allocator = gpa.allocator();
66

7-
var client: std.http.Client = .{ .allocator = allocator };
8-
defer client.deinit();
7+
var client = std.http.Client{
8+
.allocator = allocator,
9+
};
910

10-
var body = std.array_list.Managed(u8).init(allocator);
11+
var body: std.Io.Writer.Allocating = .init(allocator);
1112
defer body.deinit();
13+
try body.ensureUnusedCapacity(1024);
1214

13-
var writer_buffer: [1024]u8 = undefined;
14-
var writer = std.fs.File.stdout().writer(&writer_buffer);
15-
16-
const req = try client.fetch(.{
17-
.location = .{ .url = "https://ziglang.org" },
18-
.extra_headers = &.{},
19-
.response_writer = &writer.interface,
15+
_ = try client.fetch(.{
16+
.location = .{ .url = "https://adventofcode.com/2023/day/6/input" },
17+
.extra_headers = &.{
18+
.{
19+
.name = "cookie",
20+
.value = "***REMOVED***",
21+
},
22+
},
23+
.method = .GET,
24+
.response_writer = &body.writer,
2025
});
2126

22-
if (req.status == .ok) {
23-
std.debug.print("\n200 -- Response {s}", .{body.items});
24-
} else {
25-
std.debug.print("\nSTATUS {d}", .{req.status});
26-
}
27+
const response = try body.toOwnedSlice();
28+
std.debug.print("\nResult:\n{s}\n", .{response});
2729
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
5+
const allocator = gpa.allocator();
6+
7+
var client = std.http.Client{
8+
.allocator = allocator,
9+
};
10+
11+
var body_file = try std.fs.cwd().createFile("aoc", .{});
12+
defer body_file.close();
13+
var buffer: [1]u8 = undefined;
14+
var body_writer = body_file.writerStreaming(&buffer);
15+
16+
_ = try client.fetch(.{
17+
.location = .{ .url = "https://adventofcode.com/2023/day/6/input" },
18+
.extra_headers = &.{
19+
.{
20+
.name = "cookie",
21+
.value = "***REMOVED***",
22+
},
23+
},
24+
.method = .GET,
25+
.response_writer = &body_writer.interface,
26+
});
27+
}

src/examples/number/bitmasking.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
const input: u16 = 0b100001001001;
5+
const one: u16 = 1;
6+
7+
for (0..12) |p| {
8+
const port: u4 = @intCast(p);
9+
const matches: bool = (input & (one << port)) >= 1;
10+
const str = if (matches) "●" else "○";
11+
std.debug.print("\nPort {d: >2} [{d: >8}]: {s: >2} {any: >4}", .{
12+
port,
13+
(input << port),
14+
str,
15+
matches,
16+
});
17+
}
18+
}

0 commit comments

Comments
 (0)