C# Class GSF.BitMath

Contains some random and useful functions.
Show file Open project: GridProtectionAlliance/gsf

Public Methods

Method Description
CountBitsCleared ( uint value ) : int

Counts the number of bits that are not set

CountBitsCleared ( ulong value ) : int

Counts the number of bits that are not set

CountBitsSet ( uint value ) : int

Counts the number of bits that are set

CountBitsSet ( ulong value ) : int

Counts the number of bits that are set

CountLeadingOnes ( uint value ) : int

Counts the number of consecutive 1's starting from the highest bit working down.

CountLeadingOnes ( ulong value ) : int

Counts the number of consecutive 1's starting from the highest bit working down.

CountLeadingZeros ( uint value ) : int

Counts the number of consecutive 0's starting from the highest bit working down.

Unfortunately, c# cannot call the cpu instruction clz Example from http://en.wikipedia.org/wiki/Find_first_set

CountLeadingZeros ( ulong value ) : int

Counts the number of consecutive 0's starting from the highest bit working down.

Unfortunately, c# cannot call the cpu instruction clz Example from http://en.wikipedia.org/wiki/Find_first_set

CountTrailingOnes ( uint value ) : int

Counts the number of consecutive 1's starting from the lowest bit working up.

CountTrailingOnes ( ulong value ) : int

Counts the number of consecutive 1's starting from the lowest bit working up.

CountTrailingZeros ( uint value ) : int

Counts the number of consecutive 0's starting from the lowest bit working up.

Unfortunately, c# cannot call the cpu instruction ctz Example from http://en.wikipedia.org/wiki/Find_first_set

CountTrailingZeros ( ulong value ) : int

Counts the number of consecutive 0's starting from the lowest bit working up.

Unfortunately, c# cannot call the cpu instruction ctz Example from http://en.wikipedia.org/wiki/Find_first_set

CreateBitMask ( int bitCount ) : ulong

Creates a bit mask for a number with the given number of bits.

GetClearedBitPositions ( uint value ) : IEnumerable

Returns the bit position for every bit that is cleared in the provided value. Bit positions are defined as 0-31;

GetClearedBitPositions ( ulong value ) : IEnumerable

Returns the bit position for every bit that is cleared in the provided value. Bit positions are defined as 0-63;

GetSetBitPositions ( uint value ) : IEnumerable

Returns the bit position for every bit that is set in the provided value. Bit positions are defined as 0-31;

GetSetBitPositions ( ulong value ) : IEnumerable

Returns the bit position for every bit that is set in the provided value. Bit positions are defined as 0-63;

IsPowerOfTwo ( int value ) : bool

Determines if the number is a power of 2.

IsPowerOfTwo ( long value ) : bool

Determines if the number is a power of 2.

IsPowerOfTwo ( uint value ) : bool

Determines if the number is a power of 2.

IsPowerOfTwo ( ulong value ) : bool

Determines if the number is a power of 2.

RoundDownToNearestPowerOfTwo ( uint value ) : uint

Rounds a number down to the nearest power of 2. If the value is a power of two, the same value is returned. If value is zero, 1 is returned.

RoundDownToNearestPowerOfTwo ( ulong value ) : ulong

Rounds a number down to the nearest power of 2. If the value is a power of two, the same value is returned. If value is zero, one is returned.

RoundUpToNearestPowerOfTwo ( uint value ) : uint

Rounds a number up to the nearest power of 2. If the value is a power of two, the same value is returned. If the value is larger than the largest power of 2. It is rounded down.

Method based on a method found at: http://graphics.stanford.edu/~seander/bithacks.htm Subtitle: Round up to the next highest power of 2

RoundUpToNearestPowerOfTwo ( ulong value ) : ulong

Rounds a number up to the nearest power of 2. If the value is a power of two, the same value is returned. If the value is larger than the largest power of 2. It is rounded down.

Method based on a method found at: http://graphics.stanford.edu/~seander/bithacks.htm Subtitle: Round up to the next highest power of 2

Method Details

CountBitsCleared() public static method

Counts the number of bits that are not set
public static CountBitsCleared ( uint value ) : int
value uint
return int

CountBitsCleared() public static method

Counts the number of bits that are not set
public static CountBitsCleared ( ulong value ) : int
value ulong
return int

CountBitsSet() public static method

Counts the number of bits that are set
public static CountBitsSet ( uint value ) : int
value uint
return int

CountBitsSet() public static method

Counts the number of bits that are set
public static CountBitsSet ( ulong value ) : int
value ulong
return int

CountLeadingOnes() public static method

Counts the number of consecutive 1's starting from the highest bit working down.
public static CountLeadingOnes ( uint value ) : int
value uint
return int

CountLeadingOnes() public static method

Counts the number of consecutive 1's starting from the highest bit working down.
public static CountLeadingOnes ( ulong value ) : int
value ulong
return int

CountLeadingZeros() public static method

Counts the number of consecutive 0's starting from the highest bit working down.
Unfortunately, c# cannot call the cpu instruction clz Example from http://en.wikipedia.org/wiki/Find_first_set
public static CountLeadingZeros ( uint value ) : int
value uint
return int

CountLeadingZeros() public static method

Counts the number of consecutive 0's starting from the highest bit working down.
Unfortunately, c# cannot call the cpu instruction clz Example from http://en.wikipedia.org/wiki/Find_first_set
public static CountLeadingZeros ( ulong value ) : int
value ulong
return int

CountTrailingOnes() public static method

Counts the number of consecutive 1's starting from the lowest bit working up.
public static CountTrailingOnes ( uint value ) : int
value uint
return int

CountTrailingOnes() public static method

Counts the number of consecutive 1's starting from the lowest bit working up.
public static CountTrailingOnes ( ulong value ) : int
value ulong
return int

CountTrailingZeros() public static method

Counts the number of consecutive 0's starting from the lowest bit working up.
Unfortunately, c# cannot call the cpu instruction ctz Example from http://en.wikipedia.org/wiki/Find_first_set
public static CountTrailingZeros ( uint value ) : int
value uint
return int

CountTrailingZeros() public static method

Counts the number of consecutive 0's starting from the lowest bit working up.
Unfortunately, c# cannot call the cpu instruction ctz Example from http://en.wikipedia.org/wiki/Find_first_set
public static CountTrailingZeros ( ulong value ) : int
value ulong
return int

CreateBitMask() public static method

Creates a bit mask for a number with the given number of bits.
public static CreateBitMask ( int bitCount ) : ulong
bitCount int
return ulong

GetClearedBitPositions() public static method

Returns the bit position for every bit that is cleared in the provided value. Bit positions are defined as 0-31;
public static GetClearedBitPositions ( uint value ) : IEnumerable
value uint
return IEnumerable

GetClearedBitPositions() public static method

Returns the bit position for every bit that is cleared in the provided value. Bit positions are defined as 0-63;
public static GetClearedBitPositions ( ulong value ) : IEnumerable
value ulong
return IEnumerable

GetSetBitPositions() public static method

Returns the bit position for every bit that is set in the provided value. Bit positions are defined as 0-31;
public static GetSetBitPositions ( uint value ) : IEnumerable
value uint
return IEnumerable

GetSetBitPositions() public static method

Returns the bit position for every bit that is set in the provided value. Bit positions are defined as 0-63;
public static GetSetBitPositions ( ulong value ) : IEnumerable
value ulong
return IEnumerable

IsPowerOfTwo() public static method

Determines if the number is a power of 2.
If is less than zero
public static IsPowerOfTwo ( int value ) : bool
value int The value to check power of two properties
return bool

IsPowerOfTwo() public static method

Determines if the number is a power of 2.
If is less than zero
public static IsPowerOfTwo ( long value ) : bool
value long The value to check power of two properties
return bool

IsPowerOfTwo() public static method

Determines if the number is a power of 2.
public static IsPowerOfTwo ( uint value ) : bool
value uint The value to check power of two properties
return bool

IsPowerOfTwo() public static method

Determines if the number is a power of 2.
public static IsPowerOfTwo ( ulong value ) : bool
value ulong The value to check power of two properties
return bool

RoundDownToNearestPowerOfTwo() public static method

Rounds a number down to the nearest power of 2. If the value is a power of two, the same value is returned. If value is zero, 1 is returned.
public static RoundDownToNearestPowerOfTwo ( uint value ) : uint
value uint
return uint

RoundDownToNearestPowerOfTwo() public static method

Rounds a number down to the nearest power of 2. If the value is a power of two, the same value is returned. If value is zero, one is returned.
public static RoundDownToNearestPowerOfTwo ( ulong value ) : ulong
value ulong
return ulong

RoundUpToNearestPowerOfTwo() public static method

Rounds a number up to the nearest power of 2. If the value is a power of two, the same value is returned. If the value is larger than the largest power of 2. It is rounded down.
Method based on a method found at: http://graphics.stanford.edu/~seander/bithacks.htm Subtitle: Round up to the next highest power of 2
public static RoundUpToNearestPowerOfTwo ( uint value ) : uint
value uint
return uint

RoundUpToNearestPowerOfTwo() public static method

Rounds a number up to the nearest power of 2. If the value is a power of two, the same value is returned. If the value is larger than the largest power of 2. It is rounded down.
Method based on a method found at: http://graphics.stanford.edu/~seander/bithacks.htm Subtitle: Round up to the next highest power of 2
public static RoundUpToNearestPowerOfTwo ( ulong value ) : ulong
value ulong
return ulong