Bit — a binary-digit value type with .zero / .one cases — and the complete two-element Boolean algebra (&, |, ^, ~, plus NAND / NOR / XNOR / AND-NOT) and Z₂ field (+ is XOR, × is AND) defined over it. @frozen, Sendable, and UInt8-backed, with conformances to the institute's Comparison.Protocol, Equation.Protocol, and Hash.Protocol alongside the matching Swift stdlib protocols.
A dedicated Bit type makes the {0, 1} domain a compile-time fact rather than a convention layered over Bool or a raw integer, and carries the algebraic structure that swift-bit-vector-primitives and GF(2) linear algebra build on.
- Binary-digit value type — A two-case
Bitenum (.zero/.one) instead ofBoolor a rawUInt8: the type system enforces the domain, and the operations carry algebraic structure that neitherBoolnorIntexpresses directly. - Full Boolean operation set —
&/|/^/~and prefix!operators, method forms (.and,.or,.xor,.flipped/.toggled), and the compound operations NAND, NOR, XNOR, and AND-NOT. - Z₂ field arithmetic —
.adding(XOR) and multiplication (AND) realize the two-element field ⟨{0, 1}, +, ×⟩ — the algebraic foundation for bit-vectors and GF(2) work. - Bit ordering —
Bit.Ordernames the most-significant-first vs least-significant-first convention (withmsb/lsbaliases) for packing bits into wider words. - Stdlib-fluent —
ExpressibleByBooleanLiteralandExpressibleByIntegerLiterallet you writelet b: Bit = trueorlet b: Bit = 1;Comparable,Codable,CaseIterable, andCustomStringConvertibleround out the surface.
import Bit_Primitives
let a: Bit = .one
let b: Bit = .zero
a ^ b // .one — XOR, addition in Z₂
a & b // .zero — AND, multiplication in Z₂
a | b // .one — OR
~a // .zero — complementMethod forms read left-to-right and the Z₂ field operations name the algebra explicitly:
a.xor(b) // .one
a.and(b) // .zero
a.adding(b) // .one — Z₂ field addition (XOR)
a.flipped // .zero — NOTLiterals and checked construction:
let on: Bit = true // .one (ExpressibleByBooleanLiteral)
let off: Bit = 0 // .zero (ExpressibleByIntegerLiteral)
Bit(2) // nil — only 0 and 1 are validAdd the dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/swift-primitives/swift-bit-primitives.git", branch: "main")
]Add the umbrella product to your target:
.target(
name: "App",
dependencies: [
.product(name: "Bit Primitives", package: "swift-bit-primitives")
]
)For just the value type and its operations without the stdlib bridge or institute-protocol conformances, depend on Bit Primitive alone.
Requires Swift 6.3.1 and macOS 26 / iOS 26 / tvOS 26 / watchOS 26 / visionOS 26 (or the corresponding Linux / Windows toolchain).
Three library products plus a Test Support target:
| Product | Contents | When to import |
|---|---|---|
Bit Primitives |
Umbrella — Bit value type, all operations, the Comparison / Equation / Hash protocol conformances, and the stdlib integration |
Most consumers |
Bit Primitive |
The Bit enum, Bit.Order, and the Boolean / compound / Z₂ / bitwise operations (no stdlib bridge, no institute-protocol conformances) |
Minimal surface |
Bit Primitives Standard Library Integration |
Comparable, Codable, CaseIterable, ExpressibleBy*Literal, CustomStringConvertible, and Cardinal-typed shift operators on FixedWidthInteger |
Pulled in transitively by the umbrella |
Bit Primitives Test Support |
Re-export of upstream Test Support modules | Test target only |
Pre-1.0. The 0.1.0 surface — the Bit enum, its Boolean / compound / Z₂ operations, Bit.Order, and the Comparison / Equation / Hash conformances — is committed to source-compatibility. Bit is @frozen: its two-case, UInt8-backed layout is permanent.
| Platform | CI | Status |
|---|---|---|
| macOS 26 | Yes | Full support |
| Linux | Yes | Full support |
| Windows | Yes | Full support |
| iOS/tvOS/watchOS | — | Supported |
| Swift Embedded | — | Supported |
swift-comparison-primitives—Comparison.Protocol, whichBitconforms to.swift-equation-primitives—Equation.Protocol, the equality protocolComparison.Protocolrefines.swift-hash-primitives—Hash.Protocol, whichBitconforms to.swift-cardinal-primitives—Cardinal, used by the typed shift operators in the Standard Library Integration product.swift-carrier-primitives—Carrier, the phantom-typed wrapper those shift operators range over.
Apache 2.0. See LICENSE.md.