Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ of 12, meaning it will take up 12 bits in the packed struct). Bools also take up

```zig
const MovementState = packed struct {
running: bool,
crouching: bool,
jumping: bool,
in_air: bool,
running: bool = false,
crouching: bool = false,
jumping: bool = false,
in_air: bool = false,
};

test "packed struct size" {
Expand All @@ -32,3 +32,22 @@ test "packed struct size" {
_ = state;
}
```

## Binary Bitwise Expressions

You can perform binary bitwise operations on integers, but not on packed structs.

```zig
test "packed struct binary OR" {
var state = MovementState{ .jumping = true };

// Convert to bits and perform the binary operation
const backing_type = @typeInfo(MovementState).@"struct".backing_integer.?;
const new_state_bits = @as(backing_type, @bitCast(state)) | @as(backing_type, @bitCast(MovementState{ .running = true }));

// Convert back to original type
state = @bitCast(new_state_bits);

try expect(state == MovementState{ .jumping = true, .running = true });
}
```