Jcd.BitManipulation

A bit manipulation readability enhancement library.

Jcd.BitManipulation

Jcd.BitManipulation.Algorithms.Int32AlgorithmsExtensions

Int32AlgorithmsExtensions.CountBitsSet(this int) Method

Counts the bits that are set to 1 in a number.

public static int CountBitsSet(this int number);

Parameters

number System.Int32

The number to evaluate.

Returns

System.Int32 The count of the bits set to 1.

Example

var v = int.MaxValue;
Console.WriteLine($"{v.CountBitsSet()} bits are set."); // outputs: 31 bits are set.

Remarks

This is a reader-friendly alias for PopCount(this int).