isBitSet

Easy · Bit Manipulation · 3 test cases

Check if the nth bit of a number is set (i.e., is 1). The index n is 0-based from the right.

Examples

isBitSet(10, 0) → false
isBitSet(10, 3) → true

Starter code

function isBitSet(num, n) {

}

Complexity of the optimal solution

Time O(1), space O(1). A mask is created by left-shifting 1 by n bits. The bitwise AND with the original number will be non-zero only if the nth bit was set. This is a constant time operation.

Solve isBitSet in the browser