https://github.com/rmja/bite
Fast bit access
https://github.com/rmja/bite
Last synced: over 1 year ago
JSON representation
Fast bit access
- Host: GitHub
- URL: https://github.com/rmja/bite
- Owner: rmja
- License: mit
- Created: 2022-11-14T07:14:04.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-15T14:19:00.000Z (over 3 years ago)
- Last Synced: 2025-03-02T01:26:33.382Z (over 1 year ago)
- Language: C#
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Bite
[](https://nuget.org/packages/Bite)
[](LICENSE)
Bite provides fast bit access with the types `BitReader`, `BitWriter`, and `BitView`.
It supports read and write of different memorepresentations in the form of `Lsb0` and `Msb0`.
## Examples
`BitReader` example:
```C#
var reader = new BitReader(new byte[] { 0x1A }, BitOrder.Lsb0);
// or var reader = new BitReader(new byte[] { 0xB0 }, BitOrder.Msb0);
Assert.Equal(0b10u, reader.ReadBits(2));
Assert.Equal(0b110u, reader.ReadBits(3));
```
`BitWriter` example:
```C#
var buffer = new ArrayBufferWriter();
var writer = new BitWriter(buffer, BitOrder.Lsb0);
writer.WriteBits(2, 0b10);
writer.WriteBits(3, 0b110);
writer.Pad(); // Write zeros up to the next byte boundary
writer.Flush();
Assert.Equal(new byte[] { 0x1A }, buffer.WrittenSpan.ToArray()); // Or 0xB0 for BitOrder.Msb0
```
`BitView` (or equivalently `ReadOnlyBitView`) example:
```C#
var view = new BitView(new byte[] { 0x03, 0x81 }, BitOrder.Lsb0);
// or var view = new BitView(new byte[] { 0xC0, 0x81 }, BitOrder.Msb0);
Assert.True(view[0]);
Assert.True(view[1]);
Assert.False(view[2]);
// etc.
```