Function updateBit

  • Updates the bit at position k in the given number n to the specified value.

    The original number is not mutated; instead, a new number is returned with the bit updated.

    Returns

    A new number equal to n with bit k updated.

    Example

    updateBit(0b1010, 1, true);   // → 0b1011 (set bit 1)
    updateBit(0b1011, 3, false); // → 0b0011 (clear bit 3)

    Remarks

    • Values other than 0 or false result in bit being set to 1.
    • Works with any 32-bit integer representable in JavaScript.

    Parameters

    • n: number

      The original number whose bit is being modified.

    • k: number

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

    • value: number | boolean

      Boolean or numeric value indicating whether the bit should be set (true / 1) or cleared (false / 0).

    Returns number

Generated using TypeDoc