Edytuj

Udostępnij za pośrednictwem


BitHelper Class

Definition

Helpers to perform bit operations on numeric types.

public static class BitHelper
type BitHelper = class
Public Class BitHelper
Inheritance
BitHelper

Methods

ExtractRange(UInt32, Byte, Byte)

Extracts a bit field range from a given value.

ExtractRange(UInt64, Byte, Byte)

Extracts a bit field range from a given value.

HasByteEqualTo(UInt32, Byte)

Checks whether a byte in the input UInt32 value matches a target value.

HasByteEqualTo(UInt64, Byte)

Checks whether a byte in the input UInt32 value matches a target value. This method mirrors HasByteEqualTo(UInt32, Byte), but with UInt64 values.

HasFlag(UInt32, Int32)

Checks whether or not a given bit is set.

HasFlag(UInt64, Int32)

Checks whether or not a given bit is set.

HasLookupFlag(UInt32, Int32, Int32)

Checks whether or not a given bit is set in a given bitwise lookup table. This method provides a branchless, register-based (with no memory accesses) way to check whether a given value is valid, according to a precomputed lookup table. It is similar in behavior to HasFlag(UInt32, Int32), with the main difference being that this method will also validate the input x parameter, and will always return false if it falls outside of the expected interval. Additionally, this method accepts a min parameter, which is used to decrement the input parameter x to ensure that the range of accepted values fits within the available 32 bits of the lookup table in use. For more info on this optimization technique, see https://egorbo.com/llvm-range-checks.html. Here is how the code from the link above would be implemented using this method:

bool IsReservedCharacter(char c)
{
    return BitHelper.HasLookupFlag(314575237u, c, 36);
}

The resulted assembly is virtually identical, with the added optimization that the one produced by HasLookupFlag(UInt32, Int32, Int32) has no conditional branches at all.

HasLookupFlag(UInt64, Int32, Int32)

Checks whether or not a given bit is set in a given bitwise lookup table. For more info, check the XML docs of the HasLookupFlag(UInt32, Int32, Int32) overload.

HasZeroByte(UInt32)

Checks whether the given value has any bytes that are set to 0. That is, given a UInt32 value, which has a total of 4 bytes, it checks whether any of those have all the bits set to 0.

HasZeroByte(UInt64)

Checks whether the given value has any bytes that are set to 0. This method mirrors HasZeroByte(UInt32), but with UInt64 values.

SetFlag(UInt32, Int32, Boolean)

Sets a bit to a specified value.

SetFlag(UInt32, Int32, Boolean)

Sets a bit to a specified value.

SetFlag(UInt64, Int32, Boolean)

Sets a bit to a specified value.

SetFlag(UInt64, Int32, Boolean)

Sets a bit to a specified value.

SetRange(UInt32, Byte, Byte, UInt32)

Sets a bit field range within a target value.

SetRange(UInt32, Byte, Byte, UInt32)

Sets a bit field range within a target value.

SetRange(UInt64, Byte, Byte, UInt64)

Sets a bit field range within a target value.

SetRange(UInt64, Byte, Byte, UInt64)

Sets a bit field range within a target value.

Applies to