Tedd.BitUtils
1.0.3
See the version list below for details.
dotnet add package Tedd.BitUtils --version 1.0.3
NuGet\Install-Package Tedd.BitUtils -Version 1.0.3
<PackageReference Include="Tedd.BitUtils" Version="1.0.3" />
paket add Tedd.BitUtils --version 1.0.3
#r "nuget: Tedd.BitUtils, 1.0.3"
// Install Tedd.BitUtils as a Cake Addin
#addin nuget:?package=Tedd.BitUtils&version=1.0.3
// Install Tedd.BitUtils as a Cake Tool
#tool nuget:?package=Tedd.BitUtils&version=1.0.3
Tedd.BitUtils
Available as NuGet Package: https://www.nuget.org/packages/Tedd.BitUtils/
Bit manipulation extension methods for 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. All methods are tagged for inline compile. Ror and Rol methods should be optimized by RyuJit to use CPU implementations.
Note that CPU most likely only supports bit manipulation on Int32 and Int64, so Byte and Int16 will be treated as Int32 internally. Result will be correct, but there is no speed benefit and possibly a small penalty for using smaller types than Int32.
Extensions
i can be byte, short (Int16), ushort (UInt16), int (Int32), uint (UInt32), long (Int64) and ulong (UInt64).
Faster versions of Ror() and Rol() are hardcoded to shift 1. Faster versions of SetBit0(n) and SetBit1(n) skips the branch required for SetBit(n, true/false).
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();
Copy
- i.SetBitCopy(n, bool);
- i.SetBit0Copy(n);
- i.SetBit1Copy(n);
- i.RolCopy();
- i.RolCopy(n);
- i.RorCopy();
- i.RorCopy(n);
- i.ReverseBits();
Quick examples
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.SetBit(0);
// b == 3
a = 1;
a.Rol();
// a == 3
Note
.Net Core 3.0 and above uses hardware intrinsics for operations where available (PopCount, LeadingZeroCount). This is due to new features supported. If lack of hardware support or .Net Standard/.Net 4.x then a slower software fallback is used.
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.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
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.