Binary number with alternating bits, wrong answer

  bit-manipulation, c++, data-structures

The question is of checking whether the given number has alternating bits or not.

Link to question – https://leetcode.com/problems/binary-number-with-alternating-bits/

Even though dry run of the code shows that logic is correct but answer is wrong and I don’t know what am I missing out in the code or what I have done wrong in code that I’m not able to judge.

enter image description here

Dry run of code for 11 –

11’s binary representation – 1011

cur = 1011 & 0001 --> cur = 0001
n = 1011>>1 --> n = 0101 
if(n&1 == cur) --> if(0101&0001 == 0001)
return false;

Source: Windows Questions C++

LEAVE A COMMENT