BitField

public struct BitField<T: FixedWidthInteger & UnsignedInteger>: RandomAccessCollection, MutableCollection,
    CustomStringConvertible

A type that uses a fixed width, unsigned integer as storage for an array of bits. The number of bits is fixed and by default are set to zero.

Using fixed width arrays of bits can be used to implement bitfield like data structures.
As an example, consider an 8-bit value that represents the following data

+---------+---------+-------+-------+
| b7:4    | b2:3    | b1    | b0    |
| Value 2 | Value 1 | Flag2 | Flag1 |
+---------+---------+-------+-------+

struct Data {
    private var bits: BitField8

    var flag1: Bool {
        get { bits[0] }
        set { bits[0] = newValue }

    var flag2: Bool { bits[1] }

    var value1: Int { bits[2...3] }

    var value2: Int {
        get { bits[4...7] }
        set { buts[4...7] = newValue }
    }
}
  • The Index type for a BitField is an Int.

    Declaration

    Swift

    public typealias Index = Int
  • The Element type for a BitArry is a Bool of value true or false.

    Declaration

    Swift

    public typealias Element = Bool
  • A sequence that represents a contiguous subrange of the collection’s elements.

    Declaration

    Swift

    public typealias SubSequence = `Self`
  • The underlying storage.

    Declaration

    Swift

    public private(set) var rawValue: T { get }
  • The position of the first element in a nonempty array. Always zero.

    Declaration

    Swift

    public var startIndex: `Self`.Index { get }
  • The array’s “past the end” position—that is, the position one greater than the last valid subscript argument. This value is always equal to the bitWidth of the underlying storage.

    Declaration

    Swift

    public var endIndex: `Self`.Index { get }
  • The number of elements in the array. This is fixed at the bitWidth of the underlying storage.

    Declaration

    Swift

    public var count: Int { get }
  • Returns the position immediately before the given index.

    Declaration

    Swift

    public func index(before index: `Self`.Index) -> `Self`.Index
  • Replaces the given index with its successor.

    Declaration

    Swift

    public func index(after index: `Self`.Index) -> `Self`.Index
  • A textual representation of the array and its elements.

    Declaration

    Swift

    public var description: String { get }
  • Creates a new, empty array. All the elements are set to zero.

    Declaration

    Swift

    public init()
  • Create a new array initialising the underlying storage to the supplied value.

    Declaration

    Swift

    public init(_ rawValue: T)

    Parameters

    rawValue

    The initial value to set the storage to.

  • Accesses the element at the specified position.

    Precondition

    The index is in the valid range of startIndex up to but not includingendIndex.

    Declaration

    Swift

    public subscript(index: Index) -> Element { get set }

    Parameters

    index

    The position of the element to access. index must be greater than or equal to startIndex and less than endIndex.

    Return Value

    The bit value of the element.

  • A subrange of the array’s elements.

    The result is a full-width array with the elements in the given range
    in the lowest bits of the returned array.
    If the original array
    
    let a = BitField16(0x0A50) // 0000101001010000
    let b = a[8..<16]          // 0000000000001010
    
    

    Precondition

    bounds.lowerBound >= 0.

    Precondition

    bounds.upperBound<= The number of elements in the array.

    Declaration

    Swift

    public subscript(bounds: Range<Index>) -> SubSequence { get set }

    Parameters

    bounds

    A range of integers. The bounds of the range must be valid indices of the array.

    Return Value

    A new array with the selected elements.

  • The integer value representing the array’s elements.

    The result is the integer value of the selected elements (bits) of the array.
    
    let a = BitField16(0x0A50) // 0000101001010000
    let b = a[8..<16]          // b = 0xA
    print(b)                   // 10
    

    Precondition

    bounds.lowerBound >= 0.

    Precondition

    bounds.upperBound< The number of elements in the array.

    Declaration

    Swift

    public subscript(bounds: ClosedRange<Index>) -> T { get set }

    Parameters

    bounds

    A range of integers. The bounds of the range must be valid indices of the array.

    Return Value

    An integer representation of the selected elements.