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 | |
|
Re: decimal to binary conversion
by toolic (Bishop) on May 30, 2015 at 12:22 UTC | |
|
Re: decimal to binary conversion
by BrowserUk (Patriarch) on May 30, 2015 at 11:30 UTC | |
by NetWallah (Canon) on May 31, 2015 at 01:06 UTC | |
by BrowserUk (Patriarch) on May 31, 2015 at 08:43 UTC | |
by hippo (Archbishop) on May 31, 2015 at 12:47 UTC | |
by soonix (Chancellor) on May 31, 2015 at 06:53 UTC | |
|
Re: decimal to binary conversion
by Hosen1989 (Scribe) on Jun 05, 2015 at 00:43 UTC | |
by AnomalousMonk (Archbishop) on Jun 05, 2015 at 01:14 UTC | |
by Hosen1989 (Scribe) on Jun 05, 2015 at 08:02 UTC | |
by AnomalousMonk (Archbishop) on Jun 05, 2015 at 15:24 UTC | |
by aaron_baugher (Curate) on Jun 05, 2015 at 16:12 UTC |