https://github.com/menaver/menaver.netbitset
Inspired by std::bitset from C++, NetBitSet represents a .NET implementation of an operable sequence of bits.
https://github.com/menaver/menaver.netbitset
bit bitset dotnet encryption
Last synced: about 1 month ago
JSON representation
Inspired by std::bitset from C++, NetBitSet represents a .NET implementation of an operable sequence of bits.
- Host: GitHub
- URL: https://github.com/menaver/menaver.netbitset
- Owner: Menaver
- Created: 2024-04-15T15:16:24.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-10T11:13:27.000Z (over 1 year ago)
- Last Synced: 2025-07-07T01:57:06.006Z (12 months ago)
- Topics: bit, bitset, dotnet, encryption
- Language: C#
- Homepage: https://www.nuget.org/packages/Menaver.NetBitSet
- Size: 171 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
Inspired by [std::bitset](https://en.cppreference.com/w/cpp/utility/bitset) from C++, `NetBitSet` represents a .NET implementation of an operable sequence of bits. `NetBitSet` gives you an ability to operate with data on a bit level, it can be manipulated by standard logic operators, cloned, converted to and from basic CTS numeric data types, strings and even abstract objects. See [examples](#examples) below for more use-cases.
## Keynotes
- Platform: netstandard2.0;
- Distributed via NuGet (check out [Menaver.NetBitSet](https://www.nuget.org/packages/Menaver.NetBitSet) and [Menaver.NetBitSet.LFSR](https://www.nuget.org/packages/Menaver.NetBitSet.LFSR))
- The implementation is covered with both Unit (800+) and Performance (25+) tests;
- This repository [leverages](https://github.com/Menaver/menaver.netbitset/actions) Github Actions (GHA);
## Features
- Compact storing of each individual bit in RAM;
- Implementation of the following basic interfaces: `IEnumerable`, `ICloneable`;
- Large data support (technically, it's up to ~18446.74 petabytes, as long as your machine's RAM can handle it :D);
- Implicit and explicit casting operators that provide more convenient interaction with data;
- And many others.
## Supported bitwise operations
- And
- Or
- Xor
- Not (Invert)
- Arithmetic Shift
- Logical Shift
- Circular Shift
- Invert Endianess
## Supported data conversions
| Type | From | To | Implicit Cast | Explicit Cast |
|-----------------------------------------------|-----:| --:|--------------:|--------------:|
| 1-bit (`bool` / `bool[]`) | ✅ | ✅ | ✅ | ✅ |
| 8-bits (`(s)byte` / `(s)byte[]`) | ✅ | ✅ | ✅ | ✅ |
| 32-bits (`(u)int` / `(u)int[]`) | ✅ | ✅ | ✅ | ✅ |
| 64-bits (`(u)long` / `(u)long[]`) | ✅ | ✅ | ✅ | ✅ |
| 64-bits floating-point (`double` / `double[]`) | ✅ | ✅ | ✅ | ✅ |
| String (`string`) | ✅ | ✅ | ✅ | ❌ |
| Binary string (`string`) | ✅ | ✅ | ✅ | ❌ |
| `Object` of any type (serializable) | ✅ | ✅ | ❌ | ❌ |
## Performance
The Performance testing is driven by [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet).
Its run results can be found in repo's GHA [here](https://github.com/Menaver/menaver.netbitset/actions/workflows/run-perf-tests.yml).
### Initialization
```cs
// from bool
NetBitSet netBitSet = false; // or new NetBitSet(false);
// from byte
NetBitSet netBitSet = (byte)42; // or new NetBitSet((byte)42);
// from int
NetBitSet netBitSet = 873; // or new NetBitSet(873);
// from double
NetBitSet netBitSet = 873.4; // or new NetBitSet(873.4);
// from bytes
NetBitSet netBitSet = new bool[] { true, false, false, true };
// from bytes
NetBitSet netBitSet = new byte[] { 34, 45, 124, 46 };
// from ints
NetBitSet netBitSet = new int[] { 34, 45, 124, 46 };
// from doubles
NetBitSet netBitSet = new double[] { 122.4, 433.2, 446.2 };
// from string
NetBitSet netBitSet = "Hello, world!"; // or new NetBitSet("Hello, world!");
// from string with encoding
NetBitSet netBitSet = new NetBitSet("Hello, world!", Encoding.ASCII);
// from binary string
NetBitSet netBitSet = "11100110011100000"; // or new NetBitSet("11100110011100000");
// from object
NetBitSet netBitSet = new NetBitSet(DateTimeOffset.UtcNow);
```
### Conversion
```cs
var @bool = (bool)netBitSet; // or netBitSet.ToBool()
var @byte = (byte)netBitSet; // or netBitSet.ToByte()
var @int = (int)netBitSet; // or netBitSet.ToInt()
var bools = (bool[])netBitSet; // or netBitSet.ToBools()
var bytes = (byte[])netBitSet; // or netBitSet.ToBytes()
var ints = (int[])netBitSet; // or netBitSet.ToInts()
var @string = netBitSet.ToString();
var @string = netBitSet.ToString(Encoding.ASCII);
var binaryString = netBitSet.ToBinaryString();
var dataTimeOffset = netBitSet.ToObject();
```
### Bitwise operations
```cs
// And
netBitSetA &= netBitSetB;
// Or
netBitSetA |= netBitSetB;
// Xor
netBitSetA ^= netBitSetB;
// Not
netBitSetA = ~netBitSetB;
// logical shift left
netBitSet <<= 3;
// logical shift right
netBitSet >>= 3;
```
### Basic use-case: visualize data
```cs
// file data to bitset
var bytes = File.ReadAllBytes("picture.png");
NetBitSet bits = new NetBitSet(bytes);
// output binary data as string
var binaryString = bits.ToBinaryString();
Console.WriteLine(binaryString);
```
### Basic use-case: invert data bits
```cs
// file data to bitset
var bytes = File.ReadAllBytes("picture.png");
NetBitSet bits = new NetBitSet(bytes);
// invert data bits
bits.InvertAll();
// save to file
bytes = bits.ToBytes();
File.WriteAllBytes("picture.png", bytes);
```
### Basic use-case: xor every bit by 0
```cs
// file data to bitset
var bytes = File.ReadAllBytes("picture.png");
NetBitSet bits = new NetBitSet(bytes);
// xor data bits
var count = bits.Count;
for (ulong i = 0; i < count; i++)
{
bits[i].Xor(Bit.False);
}
// save to file
bytes = bits.ToBytes();
File.WriteAllBytes("picture.png", bytes);
```
### Another use-case: symmetric bit-level data encryption using [linear-feedback shift register (LFSR)](https://en.wikipedia.org/wiki/Linear-feedback_shift_register)
```cs
var password = "password123";
var polynomial = new ulong[] { 11, 9, 8, 0 };
// password to LFSR
var passwordBits = new NetBitSet(password);
var passwordLfsr = new Menaver.NetBitSet.LFSR.LFSR(passwordBits, polynomial);
// file data to bitset
var dataBytes = File.ReadAllBytes("picture.png");
NetBitSet dataBits = new NetBitSet(dataBytes);
// encrypt data
var count = dataBits.Count;
for (ulong i = 0; i < count; i++)
{
var outBit = passwordLfsr.Shift();
dataBits[i] = dataBits[i].Xor(outBit);
}
// save to file
dataBytes = dataBits.ToBytes();
File.WriteAllBytes("picture.png", dataBytes);
```