Skip to content
Tutorials Center
My learning

Topics

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).

C#
using System.Numerics;

int ones = BitOperations.PopCount(0b1011_0110u);   // 5
int log = BitOperations.Log2(1000);                // 9

Try it

C# Counting and locating bits

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.

C# Rotation and a bit-set loop

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

C#
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. PopCount and LeadingZeroCount take uint or ulong. Passing an int converts it implicitly only when it is a non-negative constant, so cast with (uint) or unchecked((uint)value) for runtime values.
  • Prefer the type methods in new code. int.PopCount, uint.LeadingZeroCount, uint.IsPow2, uint.RotateLeft and friends come from the generic-math interfaces (.NET 7+) and work in generic algorithms. BitOperations remains the simplest call for concrete types.
  • Clear the lowest bit. x & (x - 1) removes the lowest set bit, and x & -x isolates it. Combined with TrailingZeroCount this iterates the set bits of a mask in time proportional to their count.
  • Hash tables and buffers. RoundUpToPowerOf2 and IsPow2 are the usual tools for sizing power-of-two tables so that hash & (size - 1) can replace %.
  • Hardware hints. On x86 and ARM the JIT uses popcnt, lzcnt, tzcnt or 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.BitArray or a Span<ulong> and apply these helpers per word.