Kevin Olome wrote:We check always the first position if its 0. If it is then it will print 0. If its not then it is 1.
Then we shift 1 bit.
Why does this convert a number to a binary sequence?
Because it always checks
the same bit position.
Take 'C' (67), which is
01000011
so the first time you '&' it with 128, which is (as you already worked out)
10000000
you get
00000000
which equals 0, so you add '0' to the string.
Then you shift left, giving you (effectively)
10000110
and you '&'
that with 128, which gives you
10000000
which is NOT 0, so you add '1' to the string.
Then you shift left again, giving you (again effectively)
00001100
and you '&'
that with 128, which gives you
00000000
which equals 0, so you add '0' to the string.
....
usw, usw.
Each time you are only checking the state of ONE bit - the one that you've moved into the
first position.
BTW, when you're trying to figure out stuff like this, it's usually good to have LOTS of paper available
HIH
Winston