Get a Specified Bit

Given separate non-negative integers n and bitIndex, return the bit at zero-based position bitIndex in the binary representation of n. Position 0 is the least…

Given separate non-negative integers n and bitIndex, return the bit at zero-based position bitIndex in the binary representation of n. Position 0 is the least significant bit. A position beyond the written digits contains 0.

0 <= n <= 2,147,483,647 and 0 <= bitIndex <= 30.

Example 1: input n = 13, bitIndex = 0; output 1. 13 is 1101 in binary, so its least significant bit is 1.

Example 2: input n = 13, bitIndex = 1; output 0. The bit at position 1 is 0 when positions are counted from the right starting at 0.

Example 3: input n = 5, bitIndex = 4; output 0. 5 is 101 in binary and has no written bit at position 4.

Category: Math & Numbers. Topic: math. Difficulty: Easy. Patterns: bit-manipulation.