Jcd.BitManipulation

A bit manipulation readability enhancement library.

Jcd.BitManipulation

Jcd.BitManipulation.Algorithms.SByteAlgorithmsExtensions

SByteAlgorithmsExtensions.RoundUpToPowerOfTwo(this sbyte) Method

For any given number return the number if it’s a power of two, or return the next higher power of two capable of fitting in the data type. For negative values or values that would result in a negative next higher power of two, zero is returned. (e.g. number > 2^(k-2)) where k is the bit size.)

public static sbyte RoundUpToPowerOfTwo(this sbyte number);

Parameters

number System.SByte

The number to evaluate.

Returns

System.SByte

Example

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

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