Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

binary conversion

by saintj0n (Initiate)
on Dec 12, 2011 at 18:13 UTC ( [id://943147]=perlquestion: print w/replies, xml ) Need Help??

saintj0n has asked for the wisdom of the Perl Monks concerning the following question:

I am a newbie that is reading a Perl tutorial and doing the exercises at the end of each chapter. I am stumped by one particular exercise. It says to ask the user to input an integer less than 256 ad then use perl to convert it to a binary numbers. The hint says to use the "and" operator 8 times in the conversion process. Anyone know how this would work?

Replies are listed 'Best First'.
Re: binary conversion
by ikegami (Patriarch) on Dec 12, 2011 at 18:31 UTC

    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,

    846 = 8*(10**2) + 4*(10**1) + 6*(10**0)

    So a particular digit can be obtained as follows:

    units: ( int($num / (10**0)) ) % 10 tens: ( int($num / (10**1)) ) % 10 hundreds: ( int($num / (10**2)) ) % 10

    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.

    23 = 1*(2**4) + 0*(2**3) + 1*(2**2) + 1*(2**1) + 1*(2**0)
    bit 0: ( int($num / (2**0)) ) % 2 bit 1: ( int($num / (2**1)) ) % 2 bit 2: ( int($num / (2**2)) ) % 2 ... bit 7: ( int($num / (2**3)) ) % 2

    The thing is, there are more efficient tools for dealing with binary.

    int($i / (2**$j))

    can be written as

    $i >> $j

    and

    $k % 2

    can be written as

    $k & 1

    We end up with

    bit 0: ( $num >> 0 ) & 1 bit 1: ( $num >> 1 ) & 1 bit 2: ( $num >> 2 ) & 1 ... bit 7: ( $num >> 7 ) & 1

    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.

    bit 0: ( $num & (1 << 0) ) >> 0 = ( $num & (1 << 0) ) ? '1' : '0' bit 1: ( $num & (1 << 1) ) >> 1 = ( $num & (1 << 1) ) ? '1' : '0' bit 2: ( $num & (1 << 2) ) >> 2 = ( $num & (1 << 2) ) ? '1' : '0' ... bit 7: ( $num & (1 << 7) ) >> 7 = ( $num & (1 << 7) ) ? '1' : '0'
Re: binary conversion
by fullermd (Priest) on Dec 12, 2011 at 18:25 UTC

    Sure.

    Oh, wait, you wanted more than an answer to that last sentence? Well, if I just give you the answer, you won't know it; you'll just have been told it.

    Instead, answer these questions yourself and see if they help you figure out where to attack the problem.

    1. What does it mean to have a number in binary?
    2. Why does it specify 256? What property does that number have in connection with numbers in binary?

    If you get the right answer to those two, you should be well on your way to coming up with the solution. Or it not, at least know just where you lose the story and need a nudge back on track.

Re: binary conversion
by TJPride (Pilgrim) on Dec 12, 2011 at 21:59 UTC
    Probably not what they want, but the best I could come up with on short notice without cheating and just telling Perl to convert directly.

    use strict; use warnings; my $nd = 186; my $nb = ''; for (reverse (0..7)) { if ($nd >= 2**$_) { $nb .= '1'; $nd -= 2**$_; } else { $nb .= '0'; } } print $nb;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://943147]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found