# Bit field macro

**URL:** <https://forums.swift.org/t/bit-field-macro/64951>\
**Category:** Pitches\
**Created:** [May 12, 2023, 2:32am UTC](https://forums.swift.org/t/bit-field-macro/64951 "2023-05-12T02:32:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aggie33](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aggie33/32/29896_2.png) [@aggie33](https://forums.swift.org/u/aggie33)\
**Post date:** [May 12, 2023, 2:32am UTC](https://forums.swift.org/t/bit-field-macro/64951/1 "2023-05-12T02:32:41Z")

</div>

Currently, making a bit field in Swift is difficult and requires the use of bitwise operations and special knowledge by the programmer. A macro could make this safer and easier.

```swift
@BitField
public struct ExampleBitField {
  @BitFieldStored(bits: 2) var i: Int
  @BitFieldStored(bits: 3) var j: Int
  @BitFieldStored(bits: 3) var n: Int
  
  // total bit size of 8
}

```

The macro could scan the type for each occurrence of a BitFieldStored macro, which would just expand to nothing. From there, it could use the total number of bits to determine an appropriate rawValue for the type.

```swift
// expands to:
public struct ExampleBitField {
  var _bits: UInt8

  var i: Int {
    // getter/setter code here
  }

  var j: Int {
    // code here
  }

  var n: Int {
    // code here
  }
}

```

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [May 12, 2023, 6:12am UTC](https://forums.swift.org/t/bit-field-macro/64951/2 "2023-05-12T06:12:43Z")

</div>

Would an [OptionSet](https://developer.apple.com/documentation/swift/optionset) do the job instead?

# OptionSet

A type that presents a mathematical set interface to a bit set.

```swift
protocol OptionSet : RawRepresentable, SetAlgebra

```

## [Overview](https://developer.apple.com/documentation/swift/optionset#overview)

You use the `OptionSet` protocol to represent bitset types, where individual bits represent members of a set. Adopting this protocol in your custom types lets you perform set-related operations such as membership tests, unions, and intersections on those types. What’s more, when implemented using specific criteria, adoption of this protocol requires no extra work on your part.

---

<div class="post-metadata">

**Author:** ![mgriebling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mgriebling/32/29936_2.png) [@mgriebling](https://forums.swift.org/u/mgriebling)\
**Post date:** [May 12, 2023, 8:35am UTC](https://forums.swift.org/t/bit-field-macro/64951/3 "2023-05-12T08:35:55Z")

</div>

This would be very useful when packing/unpacking data types encoded within something like UInts. For example, the IEEE Decimal/Binary number types have multiple fields within a UInt32/64/128 word that normally require masking and shifting to pack/unpack the multi-bit fields. While this messiness is normally hidden within a Double data structure having a bit field facility would make the underlying code easier to understand and perhaps give the compiler more optimization opportunities.

---

<div class="post-metadata">

**Author:** ![mgriebling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mgriebling/32/29936_2.png) [@mgriebling](https://forums.swift.org/u/mgriebling)\
**Post date:** [May 12, 2023, 8:41am UTC](https://forums.swift.org/t/bit-field-macro/64951/4 "2023-05-12T08:41:16Z")

</div>

> [@ibex](#):
>
> Would an [OptionSet](https://developer.apple.com/documentation/swift/optionset) do the job instead?

Not entirely since this is only a bit-level encoding. It doesn't address multi-bit fields that would be mapped to integers.

---

<div class="post-metadata">

**Author:** ![Diggory](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/diggory/32/4320_2.png) [@Diggory](https://forums.swift.org/u/Diggory)\
**Post date:** [May 12, 2023, 9:20am UTC](https://forums.swift.org/t/bit-field-macro/64951/5 "2023-05-12T09:20:51Z")

</div>

> [@\[Pitch\] \`@OptionSet\` macro](https://forums.swift.org/t/pitch-optionset-macro/63547):
>
> Hi Swift Evolution, Here's a proposal for a macro to facilitate declaring option sets. Note this relies on [SE-0389](https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md), currently in review. Proposal PR can be found [here](https://github.com/apple/swift-evolution/pull/1968). Option Set Declaration Macro Proposal: SE-NNNN Authors: [Doug Gregor](https://github.com/DougGregor), [Ben Cohen](https://github.com/airspeedswift) Review Manager: TBD Status: Awaiting review Implementation: [DougGregor/swift-macro-examples#17](https://github.com/DougGregor/swift-macro-examples/pull/17/)Introduction This proposal introduces an attached macro @OptionSet which takes over the boilerplate required when declaring a bitfield-based option …
