in reply to Re: a newbie's output reversed
in thread a newbie's output reversed
And since you are doing a programming assignment (the other posters did not seem to realise that), the path you are heading for asks for copy-pasting and thus is inefficient. What you instead should do is compare the value in a loop and divide by two (rounding down) after each loop iteration. This shall give you the binary form, and you don't have to code 256, 128, 64 etc in your code.
Of course, that may not be the best approach. I think the book asked for how to use the & (binary AND) operator; let's see how it behaves:
$input = 5; $input & 4 == 1; $input & 2 == 0; $input & 1 == 1;
That looks like the binary form of "5" to me.
|
|---|