Jcd.BitManipulation

A bit manipulation readability enhancement library.

Jcd.BitManipulation

Jcd.BitManipulation.Algorithms.Int16AlgorithmsExtensions

Int16AlgorithmsExtensions.GetHighestBitSet(this short) Method

Calculate the index of the highest bit that’s been set.

public static int GetHighestBitSet(this short number);

Parameters

number System.Int16

The number to evaluate.

Returns

System.Int32 The index of the highest bit that’s been set; or -1 if none were set.

Example

short v = 1;
Console.WriteLine($"{v.GetHighestBitSet()}"); // outputs: 0

v = 2;
Console.WriteLine($"{v.GetHighestBitSet()}"); // outputs: 1

v = 3;
Console.WriteLine($"{v.GetHighestBitSet()}"); // outputs: 1

v = 4;
Console.WriteLine($"{v.GetHighestBitSet()}"); // outputs: 2