Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

decimal to binary conversion

by mp0065789 (Initiate)
on May 30, 2015 at 11:10 UTC ( [id://1128389]=perlquestion: print w/replies, xml ) Need Help??

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

Can anyone help here:

#!/usr/bin/perl -w use strict; use warnings; print "Enter decimal number less than 256:"; my $decimal; $decimal=<STDIN>; #chomp $decimal; print $decimal & 128 <=> 0; print $decimal & 64 <=> 0; print $decimal & 32 <=> 0; print $decimal & 16 <=> 0; print $decimal & 8 <=> 0; print $decimal & 4 <=> 0; print $decimal & 2 <=> 0; print $decimal & 1 <=> 0 ;
output: F:\Perl\code>chap2ex3.pl Enter decimal number less than 256:255 11111111 F:\Perl\code>chap2ex3.pl Enter decimal number less than 256:12 00000000 F:\Perl\code>chap2ex3.pl Enter decimal number less than 256:8 00000000

output is coming wrong for all except 255. Frown

Replies are listed 'Best First'.
Re: decimal to binary conversion
by AnomalousMonk (Archbishop) on May 30, 2015 at 12:07 UTC

    To see your program as Perl sees it, deparse it:

    c:\@Work\Perl\monks>perl -wMstrict -MO=Deparse,-p -e "print 'Enter decimal number less than 256: '; my $decimal; $decimal=<STDIN>; chomp $decimal; print $decimal & 128 <=> 0; print $decimal & 64 <=> 0; print $decimal & 32 <=> 0; print $decimal & 16 <=> 0; print $decimal & 8 <=> 0; print $decimal & 4 <=> 0; print $decimal & 2 <=> 0; print $decimal & 1 <=> 0; " BEGIN { $^W = 1; } use strict 'refs'; print('Enter decimal number less than 256: '); my($decimal); ($decimal = <STDIN>); chomp($decimal); print(($decimal & 1)); print(($decimal & 1)); print(($decimal & 1)); print(($decimal & 1)); print(($decimal & 1)); print(($decimal & 1)); print(($decimal & 1)); print(($decimal & 1)); -e syntax OK
    See O and B::Deparse.

    Update: Why does every expression like  128 <=> 0 become the constant 1? Because the Perl compiler, like most compilers today, is smart enough to know that the result of the comparison of a constant to a constant is just going to be yet another constant, 1 in all these cases. (Update: I believe this process is called constant folding.)


    Give a man a fish:  <%-(-(-(-<

Re: decimal to binary conversion
by toolic (Bishop) on May 30, 2015 at 12:22 UTC
    See also printf %b:
    use warnings; use strict; print "Enter decimal number less than 256:"; my $decimal; $decimal=<STDIN>; chomp $decimal; printf '%08b', $decimal; print "\n";
Re: decimal to binary conversion
by BrowserUk (Patriarch) on May 30, 2015 at 11:30 UTC

    You have a precedence problem. Modify your print lines to be: print +( $decimal & nnn ) <=> 0;


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      What is it about the value '255' that allows the program to work correctly ?
      $ perl -E 'say 254 & 2 <=> 0' 0 $ perl -E 'say 255 & 2 <=> 0' 1

              "Despite my privileged upbringing, I'm actually quite well-balanced. I have a chip on both shoulders."         - John Nash

        What is it about the value '255' that allows the program to work correctly ?

        255 has all ones. Thus if the program tests the same bit and prints 1, 8 times it just looks like it worked.

        It didn't. Any value that has the first bit set will also produce a 1:

        say 253 & 2 <=> 0;;

        Using the OPs original code, the fact that only the first bit is being tested becomes obvious when you enter a 1:

        C:\test>perl #!/usr/bin/perl -w use strict; use warnings; print "Enter decimal number less than 256:"; my $decimal; $decimal=<STDIN>; #chomp $decimal; print $decimal & 128 <=> 0; print $decimal & 64 <=> 0; print $decimal & 32 <=> 0; print $decimal & 16 <=> 0; print $decimal & 8 <=> 0; print $decimal & 4 <=> 0; print $decimal & 2 <=> 0; print $decimal & 1 <=> 0 ; ^Z Enter decimal number less than 256:1 11111111

        And the fact that 255 appeared to work, is just coincidence.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        What is it about the value '255' that allows the program to work correctly?

        Even a stopped clock is right twice a day.

        Odd, isn't it? .-)
Re: decimal to binary conversion
by Hosen1989 (Scribe) on Jun 05, 2015 at 00:43 UTC

    Hi there

    please try this version of your code, it worked for me ^_^

    #!/usr/bin/perl -w use strict; use warnings; print "Enter decimal number less than 256:"; my $decimal; $decimal=<STDIN>; chomp $decimal; # $decimal += 0; print (($decimal & 0x80) <=> 0); print (($decimal & 0x40) <=> 0); print (($decimal & 0x20) <=> 0); print (($decimal & 0x10) <=> 0); print (($decimal & 0x08) <=> 0); print (($decimal & 0x04) <=> 0); print (($decimal & 0x02) <=> 0); print (($decimal & 0x01) <=> 0);

      What is the reason for the n <=> 0 comparison?


      Give a man a fish:  <%-(-(-(-<

        The usage for <=> here in the masking expreasion ($decimal & 0x80) is to test if this 8th bit is zero or one, also you can do it like:

        #!/usr/bin/perl -w use strict; use warnings; print "Enter decimal number less than 256:"; my $decimal; $decimal=<STDIN>; chomp $decimal; # $decimal += 0; print (($decimal & 0x80)? (1) : (0) ); # to test the 8th bit print (($decimal & 0x40)? (1) : (0) ); # to test the 7th bit print (($decimal & 0x20)? (1) : (0) ); # to test the 6th bit print (($decimal & 0x10)? (1) : (0) ); # to test the 5th bit print (($decimal & 0x08)? (1) : (0) ); # to test the 4th bit print (($decimal & 0x04)? (1) : (0) ); # to test the 3th bit print (($decimal & 0x02)? (1) : (0) ); # to test the 2nd bit print (($decimal & 0x01)? (1) : (0) ); # to test the 1st bit

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-28 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found