Tedd.BitUtils
1.0.4
See the version list below for details.
dotnet add package Tedd.BitUtils --version 1.0.4
NuGet\Install-Package Tedd.BitUtils -Version 1.0.4
<PackageReference Include="Tedd.BitUtils" Version="1.0.4" />
paket add Tedd.BitUtils --version 1.0.4
#r "nuget: Tedd.BitUtils, 1.0.4"
// Install Tedd.BitUtils as a Cake Addin #addin nuget:?package=Tedd.BitUtils&version=1.0.4 // Install Tedd.BitUtils as a Cake Tool #tool nuget:?package=Tedd.BitUtils&version=1.0.4
Tedd.BitUtils
Available as NuGet Package: https://www.nuget.org/packages/Tedd.BitUtils/
Bit manipulation extension methods for:<br />byte, short (Int16), ushort (UInt16), int (Int32), uint (UInt32), long (Int64) and ulong (UInt64).
Types can be manipulated in-place or by returning a modified copy.
Extension methods
Methods are implemented as extension methods. This should give you a list in editor when you type dot after a supported type. Note: Ror and Rol is only implemented for 32-bit and 64-bit integers.
Get info
- bool a = i.IsBitSet(n);
- int a = i.PopCount();
- int a = i.LeadingZeroCount();
- string a = i.ToBinaryString();
- string a = i.ToBinaryStringPadded();
In-place
- i.SetBit(n, bool);
- i.SetBit0(n);
- i.SetBit1(n);
- i.Rol();
- i.Rol(n);
- i.Ror();
- i.Ror(n);
- i.ReverseBits();
- i.ReverseEndianness();
Copy
- i2 = i.SetBitCopy(n, bool);
- i2 = i.SetBit0Copy(n);
- i2 = i.SetBit1Copy(n);
- i2 = i.RolCopy();
- i2 = i.RolCopy(n);
- i2 = i.RorCopy();
- i2 = i.RorCopy(n);
- i2 = i.ReverseBitsCopy();
- i2 = i.ReverseEndiannessCopy();
Simple example of usage
var a = 0;
a.SetBit(0, true);
// a == 1
a.SetBit(1, true);
// a == 3
a.SetBit0(0);
// a == 2
// a.IsBitSet(0) == false
var b = a.SetBitCopy(0);
// b == 3
a = 1;
a.Rol();
// a == 3
Performance
Ror() and Rol() are faster versions of Ror(1) and Ror(1). If you only need to roll one, use these.<br/> The same goes for SetBit1(n) and SetBit0(n) which are faster versions of SetBit(n, bool) where branching is left out. If you don't need to pass true/false as parameter use the faster versions.
All methods are tagged for inline compile.
Hardware intrinsics
For .Net Core 3.0 and above library uses hardware intrinsics for operations where available. This is possible because of new features supported in .Net Core 3.0 and aboce. For frameworks (.Net 4.x and .Net Standard) and processors (i.e. non-x86/x64 CPU) where this is not supported a slower software implementation is used.
Command | .Net 4.x | .Net Standard | .Net Core 2.x | .Net Core 3.x |
---|---|---|---|---|
SetBit | Y | Y | Y | Y |
SetBit0 | Y | Y | Y | Y |
SetBit1 | Y | Y | Y | Y |
Rol | RyuJit | RyuJit | Y | Y |
Ror | RyuJit | RyuJit | Y | Y |
ReverseBits | N | N | N | Y |
PopCount | N | N | N | Y |
LeadingZeroCount | N | N | N | Y |
ToBinaryString | N/A | N/A | N/A | N/A |
ToBinaryStringPadded | N/A | N/A | N/A | N/A |
ReverseEndianness | N | N | N | N |
Note that for Byte, Int16 and UInt16 the CPU uses 32-bit word size anyway so there is no speed gained in smaller datatypes. Decompile to MSIL shows extra assembly instructions handling conversions.
Note
ReverseEndianness uses simple binary operations, but instructions such as BSWAP is not available in .Net yet. However the BinaryPrimitives implementation of the same methods are tagged as [Intrinsic], which means they may provide better implementation than what this library can offer.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.0
- No dependencies.
-
.NETCoreApp 2.1
- No dependencies.
-
.NETCoreApp 3.0
- No dependencies.
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.6.1
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Tedd.BitUtils:
Package | Downloads |
---|---|
schema
Library for converting classes to and from binary. Provides a C# Roslyn generator that automatically implements conversion logic for simple classes. |
GitHub repositories
This package is not used by any popular GitHub repositories.