Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hughbe/swiftbitfield

Package for defining bit fields in Swift.
https://github.com/hughbe/swiftbitfield

Last synced: 2 days ago
JSON representation

Package for defining bit fields in Swift.

Awesome Lists containing this project

README

        

# Swift BitField

Package for defining bit fields in Swift.

## Usage

In `package.swift` add the following line of code under the `dependencies` section

```swift
.package(name: "BitField", url: "https://github.com/hughbe/SwiftBitField", from: "1.0.0")
```

To use the library in your library, `import BitField`.

## Example

Use `getBit(_ index:)` to get the boolean value of a bit at a zero-based index into the raw value.

```swift
let field = BitField(rawValue: 0b10101010)
XCTAssertFalse(field.getBit(0))
XCTAssertTrue(field.getBit(1))
XCTAssertFalse(field.getBit(2))
XCTAssertTrue(field.getBit(3))
XCTAssertFalse(field.getBit(4))
XCTAssertTrue(field.getBit(5))
XCTAssertFalse(field.getBit(6))
XCTAssertTrue(field.getBit(7))
```

Use `getBit(offset:count:)` to get the integer value of a series of bit starting at a zero-based index into the raw value.

```swift
let field = BitField(rawValue: 0b10101010)
XCTAssertEqual(0b10101010, field.getBits(offset: 0, count: 8))
XCTAssertEqual(0b101, field.getBits(offset: 1, count: 3))
XCTAssertEqual(0b1010101, field.getBits(offset: 1, count: 7))
XCTAssertEqual(0b1, field.getBits(offset: 7, count: 1))
```