C# Reference ยท Numbers and Math
C# BitOperations class
Reference for System.Numerics.BitOperations: count set bits, find leading and trailing zeros, rotate bits and work with powers of two.
System.Numerics.BitOperations is a static class of small, fast helpers for working with the bits of uint, ulong, int, long and nuint. It covers the operations you normally write with tricky loops: counting set bits, finding the highest or lowest set bit, rotating, and rounding to a power of two. The JIT turns most of them into single CPU instructions where the hardware supports it.
You need using System.Numerics; for the class (it is not one of the implicit global usings).
using System.Numerics; int ones = BitOperations.PopCount(0b1011_0110u); // 5 int log = BitOperations.Log2(1000); // 9
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static int BitOperations.PopCount(uint value) // also ulong, nuint public static int BitOperations.LeadingZeroCount(uint value) public static int BitOperations.TrailingZeroCount(int value) // also uint, long, ulong, nint, nuint public static int BitOperations.Log2(uint value) public static uint BitOperations.RotateLeft(uint value, int offset) public static uint BitOperations.RotateRight(uint value, int offset) public static bool BitOperations.IsPow2(int value) // also uint, long, ulong, nint, nuint public static uint BitOperations.RoundUpToPowerOf2(uint value) public static uint BitOperations.Crc32C(uint crc, byte data)
| Name | Description |
|---|---|
PopCount |
Number of bits set to 1. |
LeadingZeroCount |
Zero bits above the highest set bit. 32 (or 64) for zero. |
TrailingZeroCount |
Zero bits below the lowest set bit. 32 (or 64) for zero. |
Log2 |
Index of the highest set bit, which is the floor of the base-2 logarithm. Returns 0 for 0. |
RotateLeft / RotateRight |
Circular shift by offset bits. The offset is masked to the width of the type. |
IsPow2 |
true for 1, 2, 4, 8 and so on. |
RoundUpToPowerOf2 |
Smallest power of two greater than or equal to value. |
Crc32C |
One step of a CRC-32C checksum (hardware accelerated when available). |
Errors: none of these throw for ordinary values. RoundUpToPowerOf2 returns 0 when the result would not fit in the type.
Usage notes
- Signed arguments.
PopCountandLeadingZeroCounttakeuintorulong. Passing anintconverts it implicitly only when it is a non-negative constant, so cast with(uint)orunchecked((uint)value)for runtime values. - Prefer the type methods in new code.
int.PopCount,uint.LeadingZeroCount,uint.IsPow2,uint.RotateLeftand friends come from the generic-math interfaces (.NET 7+) and work in generic algorithms.BitOperationsremains the simplest call for concrete types. - Clear the lowest bit.
x & (x - 1)removes the lowest set bit, andx & -xisolates it. Combined withTrailingZeroCountthis iterates the set bits of a mask in time proportional to their count. - Hash tables and buffers.
RoundUpToPowerOf2andIsPow2are the usual tools for sizing power-of-two tables so thathash & (size - 1)can replace%. - Hardware hints. On x86 and ARM the JIT uses
popcnt,lzcnt,tzcntor equivalents when present and falls back to a software routine otherwise. The result is identical either way. - Bit vectors. For many bits, use
System.Collections.BitArrayor aSpan<ulong>and apply these helpers per word.