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: <%-(-(-(-<
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: decimal to binary conversion
by toolic (Bishop) on May 30, 2015 at 12:22 UTC
|
use warnings;
use strict;
print "Enter decimal number less than 256:";
my $decimal;
$decimal=<STDIN>;
chomp $decimal;
printf '%08b', $decimal;
print "\n";
| [reply] [Watch: Dir/Any] [d/l] |
Re: decimal to binary conversion
by BrowserUk (Patriarch) on May 30, 2015 at 11:30 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
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);
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
#!/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
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|