Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/hughbe/swiftbitfield
- Owner: hughbe
- Created: 2020-11-11T10:46:16.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-11-11T10:51:47.000Z (about 4 years ago)
- Last Synced: 2024-04-26T04:41:49.956Z (9 months ago)
- Language: Swift
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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))
```