To my understanding, Zig supports types u1
..u65535
.
How would I need to implement fn U(comptime bitsize: u16) type
such that U(n) == un
for u
in 1..65535?
Or is there a builtin way to do that?
I'm trying to do something like:
union (enum(u4)) {
a: void,
...
x: U(32-4),
y: I(32-4),
z: C((32-4/8))
}
For example: A tagged union with a total size of 32 bits where X bits are used for tag values and the remaining bits for data.
You can use @Type
to achieve this:
fn UInt(comptime bitsize: u16) type {
return @Type(.{ .Int = .{ .signedness = .unsigned, .bits = bitsize } });
}
pub fn main() !void {
const x: UInt(32-4) = 0;
@compileLog(@TypeOf(x));
}
This prints:
Compile Log Output:
@as(type, u28)