mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
11 lines
310 B
C++
11 lines
310 B
C++
class Solution{
|
|
public :
|
|
bool hasAlternatingBits(int n){
|
|
int m = n^(n>>1);
|
|
return (m&(m+1)==0);
|
|
}
|
|
}
|
|
|
|
// TC & SC : o(1)
|
|
// n>>1 inverts all bits and XOR makes m have all set bits if n had alternating bits. (m+1) makes all bits of m as zero and m&(m+1) will be zero
|