Function setBit

  • Sets (changes to 1) the bit at the specified position in a number.

    The original number is not mutated; a new number is returned with the bit set.

    Returns

    A new number equal to n with bit k set.

    Example

    setBit(0b1001, 1); // → 0b1011 (sets bit 1)
    setBit(0b0111, 3); // → 0b1111 (sets bit 3)

    Remarks

    • Only the bit at position k is affected; all other bits remain unchanged.
    • Works with any 32-bit integer representable in JavaScript.

    Parameters

    • n: number

      The original number whose bit is to be set.

    • k: number

      Zero-based bit position to set (0 = least-significant bit).

    Returns number

Generated using TypeDoc