UNIQUE NUMBER 2

Instead of checking for the first unset bit, you need to check for first set bit as that will be bit which differs for both the unique numbers.

while((temp&1)!=1)
{
pos++;
temp=temp>>1;
}
I am checking for first set bit only through this loop and pos will be the position of first set bit as explained in tutorial .

Edit at line 27:
if(((a[i])&(mask))>0)

Always cover the whole expression on each side with parenthesis while working with bitwise operators.

1 Like