Back

Bit Manipulation (Easy) / Check if K-th Bit is Set

00:00
1/20

Check if K-th Bit is Set

Given a number n, check if the k-th bit (0-indexed) is set (1) or not (0).

  • 1

    Create a mask by shifting 1 to the left by k positions (1 << k).

  • 2

    Perform a bitwise AND between n and the mask.

  • 3

    If the result is non-zero, the bit is set.

PYTHON PLAYGROUND
PYTHON PLAYGROUND
⏳ Loading editor…