divyaimca has asked for the wisdom of the Perl Monks concerning the following question:
HI, I am new to perl. I was converting the decimal to binary for numbers below 256. I got a way to do this:
I think its ok. But I am wondering if the number is greater than 256 ??#!/usr/bin/perl use warnings; use strict; print " Enter a number between 0 to 256 : "; my $dec = <STDIN>; my $a = ($dec & 128) <=> 0; my $b = ($dec & 64) <=> 0; my $c = ($dec & 32) <=> 0; my $d = ($dec & 16) <=> 0; my $e = ($dec & 8) <=> 0; my $f = ($dec & 4) <=> 0; my $g = ($dec & 2) <=> 0; my $h = ($dec & 1) <=> 0; print " The entered decimal number in binary is : ",$a.$b.$c.$d.$e.$f. +$g.$h," \n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: decimal to binary
by GrandFather (Saint) on Jan 09, 2012 at 06:57 UTC | |
by divyaimca (Initiate) on Jan 09, 2012 at 09:14 UTC | |
by GrandFather (Saint) on Jan 09, 2012 at 09:39 UTC | |
|
Re: decimal to binary
by Khen1950fx (Canon) on Jan 09, 2012 at 07:34 UTC | |
|
Re: decimal to binary
by mbethke (Hermit) on Jan 09, 2012 at 06:51 UTC | |
|
Re: decimal to binary
by Marshall (Canon) on Jan 09, 2012 at 11:06 UTC | |
|
Re: decimal to binary
by Anonymous Monk on Jan 09, 2012 at 06:46 UTC |