Read One Bit
Written in binary, a number is a row of bits. Position 0 is the lowest bit, the one worth 1; position 1 is worth 2, position 2 is worth 4, and so on.
Return the bit of n at position k: 1 or 0. A position beyond the highest 1 of n holds a 0.
Example 1
n 13
k 2
returns 1
13 is 1101 in binary. Position 2, the third bit from the right, is 1.
Example 2
n 13
k 1
returns 0
13 is 1101. Position 1 is 0.
Example 3
n 5
k 10
returns 0
Position 10 lies far above the highest bit of 5.
Constraints
0 <= n <= 10**90 <= k <= 30
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Read a Binary StringBeginner
- Set Bit PositionsBeginner
- Switch Off the Lowest OneBeginner
- Add Without PlusEasy
- Alternating BitsEasy
- Binary GapEasy
- Bitwise AND of a RangeEasy
- Block of OnesEasy