| name | BitsKit |
| nuget | https://www.nuget.org/packages/BitsKit/ |
| link | https://github.com/barncastle/BitsKit |
| author | barncastle |
Reading efficiently from a bit structure
This is how you can use BitsKit .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BitsKit" Version="1.0.0" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
using BitsDemo; var z = new zlib_header(0x78,0x9C); Console.WriteLine( z.FLEVEL ); Console.WriteLine(z.CM);
using BitsKit;
using BitsKit.BitFields;
using System.IO.Compression;
namespace BitsDemo;
//[BitObject(BitOrder.LeastSignificant)]
//partial struct zlib_header
//{
// public zlib_header(byte cmf,byte flg)
// {
// CMF = cmf;
// FLG = flg;
// }
// [EnumField("CM",4,typeof(CompressionMode))]
// [BitField("CINFO",4)]
// private byte CMF;
// [BitField("FCHECK",5)]
// [BooleanField("FDICT")]
// [EnumField("FLEVEL",2,typeof(CompressionLevel))]
// private byte FLG;
//}
[BitObject(BitOrder.LeastSignificant)]
partial struct zlib_header
{
public zlib_header(byte cmf,byte flg)
{
CMF = cmf;
FLG = flg;
}
[BitField("CM",4)]
[BitField("CINFO",4)]
private byte CMF;
[BitField("FCHECK",5)]
[BitField("FDICT",1)]
[BitField("FLEVEL",2)]
private byte FLG;
}
The code that is generated is
#nullable enable
#pragma warning disable IDE0005 // Using directive is unnecessary
#pragma warning disable IDE0005_gen // Using directive is unnecessary
#pragma warning disable CS8019 // Unnecessary using directive.
#pragma warning disable IDE0161 // Convert to file-scoped namespace
using System;
using System.Runtime.InteropServices;
using BitsKit.Primitives;
namespace BitsDemo
{
partial struct zlib_header
{
public Byte CM
{
get => BitPrimitives.ReadUInt8LSB(CMF,0,4);
set => BitPrimitives.WriteUInt8LSB(ref CMF,0,value,4);
}
public Byte CINFO
{
get => BitPrimitives.ReadUInt8LSB(CMF,4,4);
set => BitPrimitives.WriteUInt8LSB(ref CMF,4,value,4);
}
public Byte FCHECK
{
get => BitPrimitives.ReadUInt8LSB(FLG,0,5);
set => BitPrimitives.WriteUInt8LSB(ref FLG,0,value,5);
}
public Byte FDICT
{
get => BitPrimitives.ReadUInt8LSB(FLG,5,1);
set => BitPrimitives.WriteUInt8LSB(ref FLG,5,value,1);
}
public Byte FLEVEL
{
get => BitPrimitives.ReadUInt8LSB(FLG,6,2);
set => BitPrimitives.WriteUInt8LSB(ref FLG,6,value,2);
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/BitsKit
Leave a Reply