Do you know where your variables are? | |
PerlMonks |
Re: binary conversionby ikegami (Patriarch) |
on Dec 12, 2011 at 18:31 UTC ( [id://943152]=note: print w/replies, xml ) | Need Help?? |
Let's deal with the more familiar first and ask ourselves how someone could convert a number to its decimal representation. Converting a number to its decimal representation is to build a string consisting of its digits. Well, we know that one can decompose a number into its digits as follows,
So a particular digit can be obtained as follows:
Bits are to number in binary as digits are to numbers in decimal, so converting a number to its binary representation is to build a string consisting of all its bits. The same approach can be taken.
The thing is, there are more efficient tools for dealing with binary.
can be written as
and
can be written as
We end up with
Now just put that in a loop, and you're done. That said, I would just use sprintf "%08b", $num. Based on the hint, I think they're expecting you to use the following, but this is Perl not C.
In Section
Seekers of Perl Wisdom
|
|