[LeetCode] Exactly One Consecutive Set Bits Pair

3950. Exactly One Consecutive Set Bits Pair

You are given an integer n.

Return true if its binary representation contains exactly one adjacent pair of set bits, and false otherwise.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool consecutiveSetBits(int n) {
int ok = 0;
while(n) {
if((n & 3) == 3) ok++;
n /= 2;
}
return ok == 1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/exactly-one-consecutive-set-bits-pair/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.