Read the K-th Bit
Mediumjava
Read a non-negative integer n and a bit position k (0 = least significant). Print the value of that bit using (n >> k) & 1.
Input: two integers n and k separated by whitespace.
Output: one line — 0 or 1.
Example 1
Input
13 0
Output
1
Example 2
Input
13 1
Output
0
- 0 <= n <= 1000000000
- 0 <= k <= 30
Hint 1
Shifting n right by k moves the wanted bit into the lowest position.
Hint 2
& 1 keeps only that lowest bit.
13 is 1101 in binary. Shifting right by k lines up bit k with position 0, and & 1 masks off everything else, leaving just that bit.