Using arithmetic division as a substitute for bit manipulation can create unexpected results. Since what you're doing here is shifting bits, why not treat $number like the binary it is? Use the shift right operator >> instead of dividing by two. http://perldoc.perl.org/perlop.html#Shift-Operators
(edited to add) Likewise, rather than using arithmetic modulo to to the equivalent of extracting the low bit, just use the bitwise and operator & to do a real bit mask operation. $number & 1 always gives the low bit of an integer $number, $number & 2 the next least significant bit, and so forth.
If personally coding a solution for that specific chore isn't important, see the concise, ready-to-use solutions in this question and answer. http://www.perlmonks.org/?node_id=55320 I've found that nineteen times out of twenty, consulting Perl Monks Super Search and Google (which often sends me to Perl Monks) brings up a solution ready to drop in as is or use with a little adaptation.
Hope this helps!