News for hackers. Stuff that matters.
Re:Packed structs in Zig make bit/flag sets trivial by Someone on Tuesday August 30, 2022 @03:43AM (#32647701)
Swift has OptionSet. See https://developer.apple.com/documentation/swift/optionset. It allows setting, testing, or clearing multiple fields with a nice syntax.

One thing that is a bit noisy is that you have to specify the bit index when you name the individual bits, as in (from that article):

  static let secondDay = ShippingOptions(rawValue: 1 << 1)
Upside from that is that it makes it clear what bit each value specifies, and won’t easily accidentally change them when you reorder definitions, or insert or remove them. I can see why they made that choice.

I also guess OptionSet could have been implemented in Swift in a third party library, whereas this Zig feature cannot.

I also think/guess neither language guarantees multiple bits would get read or written in one instruction. If your hardware needs that, you probably have to go down a level, or look at the disassembly of your code to check what your compiler did.

There are no comments on this comment yet.