Show / Hide Table of Contents

    Class BitHacks

    Inheritance
    Object
    BitHacks
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Advanced.Algorithms.Binary
    Assembly: Advanced.Algorithms.dll
    Syntax
    public class BitHacks

    Methods

    CountSetBits(Int32)

    Returns the number of set bits in given integer x

    Declaration
    public static int CountSetBits(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    CountTrailingZerosByBinarySearch(Int32)

    Counts the number of trailing zero bits in an efficient manner

    Declaration
    public static int CountTrailingZerosByBinarySearch(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    GetRightmostSubBitsStartingWithAnUnsetBit(Int32)

    Gets the first right most sub bits starting with a Unset bit, with rightmost being 0th bit eg. 1011 => 0011

    Declaration
    public static int GetRightmostSubBitsStartingWithAnUnsetBit(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    GetRightmostSubBitsStartingWithASetBit(Int32)

    Gets the first right most sub bits starting with a set bit, with rightmost being 0th bit

    Declaration
    public static int GetRightmostSubBitsStartingWithASetBit(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    HasOppositeSigns(Int32, Int32)

    Checks if given numbers are of opposite signs

    Declaration
    public static bool HasOppositeSigns(int x, int y)
    Parameters
    Type Name Description
    Int32 x
    Int32 y
    Returns
    Type Description
    Boolean

    IsEven(Int32)

    Checks if given number is even

    Declaration
    public static bool IsEven(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Boolean

    IsPowerOf2(Int32)

    Checks if given number is a power of 2

    Declaration
    public static bool IsPowerOf2(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Boolean

    IsSet(Int32, Int32)

    Checks if nth bit from right is set, with rightmost being 0th bit

    Declaration
    public static bool IsSet(int x, int n)
    Parameters
    Type Name Description
    Int32 x
    Int32 n
    Returns
    Type Description
    Boolean

    RightPropogateRightmostSetBit(Int32)

    Sets all the first right most sub bits starting with a set bit, with rightmost being 0th bit

    Declaration
    public static int RightPropogateRightmostSetBit(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    RightPropogateRightmostUnsetBit(Int32)

    UnSets all the first right most sub bits starting with a unset bit, with rightmost being 0th bit

    Declaration
    public static int RightPropogateRightmostUnsetBit(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    SetBit(Int32, Int32)

    Sets nth bit from right, with rightmost being 0th bit

    Declaration
    public static int SetBit(int x, int n)
    Parameters
    Type Name Description
    Int32 x
    Int32 n
    Returns
    Type Description
    Int32

    ToggleBit(Int32, Int32)

    Toggles nth bit from right, with rightmost being 0th bit

    Declaration
    public static int ToggleBit(int x, int n)
    Parameters
    Type Name Description
    Int32 x
    Int32 n
    Returns
    Type Description
    Int32

    TurnOffRightmostSetBit(Int32)

    Turns Off first set bit from right, with rightmost being 0th bit

    Declaration
    public static int TurnOffRightmostSetBit(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    TurnOnBitAfterRightmostSetBit(Int32)

    Turns On first Unset bit from right, with rightmost being 0th bit

    Declaration
    public static int TurnOnBitAfterRightmostSetBit(int x)
    Parameters
    Type Name Description
    Int32 x
    Returns
    Type Description
    Int32

    UnsetBit(Int32, Int32)

    Unsets nth bit from right, with rightmost being 0th bit

    Declaration
    public static int UnsetBit(int x, int n)
    Parameters
    Type Name Description
    Int32 x
    Int32 n
    Returns
    Type Description
    Int32

    UpdateBitToValue(Int32, Int32, Boolean)

    Update the nth bit from right with given boolean value, with rightmost being 0th bit

    Declaration
    public static int UpdateBitToValue(int x, int n, bool value)
    Parameters
    Type Name Description
    Int32 x
    Int32 n
    Boolean value
    Returns
    Type Description
    Int32
    Back to top Generated by DocFX