What are the practical use cases for the new @bitCast behavior in Zig 0.17.0?

What are some practical use cases for the new @bitCast behavior in Zig 0.17.0?

I understand what changed, but I’m curious where you would actually use it in real projects.

I think this matters for example on SIMD code, let’ say you have a mask like @Vector(16,bool) and you want to do a @ctz(mask) that means now it’s portable, if the vector was converted from an array, so it’s endian agnostic if I understand correctly. So basically this is probably useful everywhere, you interact with binary format, or where stability of binary representation, at the logical level matters, which is why extern is now disalowed, because extern is not logical but layout based.

5 Likes

I spent the past couple days parsing MIDI files, which have some bitpacked fields. being able to just bitcast an array of bytes into a packed struct came in handy several times!

1 Like