JuniorBit Manipulation
Binary Gap
Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0.
Two 1's are adjacent if there are only 0's separating them, possibly no 0's. The distance between two 1's is the absolute difference between their bit positions.
Example 1
Input
n = 22Output
222 in binary is "10110", and the largest distance among adjacent pairs of 1's is 2.
Example 2
Input
n = 8Output
08 in binary is "1000", so there are no adjacent pairs of 1's and the answer is 0.
Constraints
- 1 <= n <= 10^9