#how do i temporarily cast a u8 into a u32

1 messages · Page 1 of 1 (latest)

errant vault
#

how do i temporarily cast a u8 into a u32

dire sleet
#

@as(u32, r)

#

it has less bits than the destination type so it can coerce

spiral coyote
#

FYI zig has packed structs for this

#
const Color = packed struct {
    r: u8,
    g: u8,
    b: u8,
    a: u8.
}
#

And then use @bitCast to coerce to u32 and vice-versa

#
const col: Color = .{
    .r = @intFromFloat(v.data[0] * 255.0);
    .g = @intFromFloat(v.data[1] * 255.0);
    .b = @intFromFloat(v.data[2] * 255.0);
    .a = @intFromFloat(v.data[3] * 255.0);
}

const col32: u32 = @bitCast(col);
dire sleet
#

@as(u32, r) | (@as(u32, g) << 8) | …

#

are you shifting inside of the as?

#

ye

#

cant shift a u8 with 8

#

no worries lol

#

wouldnt be in here if i didnt wanna help