Jcd.BitManipulation

A bit manipulation readability enhancement library.

Jcd.BitManipulation

Jcd.BitManipulation.Algorithms.UInt16AlgorithmsExtensions

UInt16AlgorithmsExtensions.AreOnlyFirstAndLastBitsSet(this ushort) Method

Determines if only the lowest bit and one other higher bit are set.

public static bool AreOnlyFirstAndLastBitsSet(this ushort number);

Parameters

number System.UInt16

The number to evaluate.

Returns

System.Boolean true if the bitwise representation follows this pattern: 0b10..01. with any number of intervening zeros.

Example

ushort v = 0b0001;
Console.WriteLine($"{v.AreOnlyFirstAndLastBitsSet()}"); // outputs: True

v = 0b0011;
Console.WriteLine($"{v.AreOnlyFirstAndLastBitsSet()}"); // outputs: True

v = 0b0101;
Console.WriteLine($"{v.AreOnlyFirstAndLastBitsSet()}"); // outputs: True

v = 0b0111;
Console.WriteLine($"{v.AreOnlyFirstAndLastBitsSet()}"); // outputs: False