Second code works because you prints '0'
when a
is == 0
else '1'
. Accordingly in first code piece, if(a==1)
should be if(a)
that means print 1
if a
is not 0
(Rremember every non-zero value is true in C).
The thing is a = (userInput & (1<<i));
is not always 1
but a
can be a number that is either zero or a number in which only one bit is one (e.g. ...00010000)