Re: built in function for binary division
by Kenosis (Priest) on Jan 11, 2013 at 05:31 UTC
|
use strict;
use warnings;
my $quotient = 0b10010110 / 0b1111; # 150 / 15
print sprintf '%b', $quotient; # prints 1010 (decimal 10)
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
You can work with binary numbers as shown, viz., by preceding the number with 0b, performing the operation, and then formatting the result using sprintf.
Were you thinking of some other binary math function?
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
Re: built in function for binary division
by moritz (Cardinal) on Jan 11, 2013 at 06:34 UTC
|
All arithmetics in Perl is performed in binary. Though that's not really relevant, because the outcome would be the same if it were performed in any other base.
$ perl -wE 'say 4 + 5' # uses binary addition under the hood
9
It's just the input and output of numbers that decimal by default; but as others have shown you, there are ways to achieve binary input too.
So I guess the real question is: what do you want to achieve?
| [reply] [Watch: Dir/Any] [d/l] |
Re: built in function for binary division
by NetWallah (Canon) on Jan 11, 2013 at 05:23 UTC
|
Just about every mathematical operation performed by built-in perl (mathematical) operators and functions is performed on binary-value operands.
You can FORMAT the input and output to look like decimal or binary numbers. Most humans prefer to look at the default formatting in decimal.
"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."
-Confucius
| [reply] [Watch: Dir/Any] |
Re: built in function for binary division
by vinoth.ree (Monsignor) on Jan 11, 2013 at 05:32 UTC
|
We have Operators in perl for Bit-wise operations
The Bit-Manipulation Operators
The following bit operators are supported in Perl:
The & (bitwise AND) operator
The | (bitwise OR) operator
The ^ (bitwise XOR or "exclusive or") operator
The ~ (bitwise NOT) operator
The << (left shift) and >> (right shift) operators
| [reply] [Watch: Dir/Any] |
Re: built in function for binary division
by AnomalousMonk (Archbishop) on Jan 11, 2013 at 09:12 UTC
|
ghosh123:
In addition the the reply of Kenosis, note also the integer pragma. Perl represents numbers internally as doubles. Although the fractional portion of a number is ignored when printed as binary, it still exists and can produce perhaps puzzling results in subsequent calculations. use integer; causes the fractional portion to be majick'd away (don't ask me for details on this). This may produce results in your 'integer' operations that are more consistent with what you expect. (Note in the examples below, the divisor/multiplier is 0b1110 (14), not 0b1111.)
>perl -wMstrict -le
"my $quo = 0b10010110 / 0b1110;
my $n = $quo * 0b1110;
printf qq{%f %b; %f %b \n}, $quo, $quo, $n, $n;
;;
use integer;
$quo = 0b10010110 / 0b1110;
$n = $quo * 0b1110;
printf qq{%f %b; %f %b \n}, $quo, $quo, $n, $n;
"
10.714286 1010; 150.000000 10010110
10.000000 1010; 140.000000 10001100
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: built in function for binary division
by ww (Archbishop) on Jan 11, 2013 at 05:46 UTC
|
Google is your friend. The substantive portion is lifted nearly verbatim from onperl.net's binary arithmetic tut. NB You'll have work to do to extend this beyond 0b111.
C:\>perl -E "my $result = 0b01011/0b11; say $result;"
3.66666666666667 # output in decimal.
for binary output:
#! /usr/bin/perl
use 5.014;
my @CONVERSIONS = qw(000 001 010 011 100 101 110 111);
sub conv2bin{
my $octal = sprintf ("%o", $_[0]);
my @threeBitSeqs = map {$CONVERSIONS [$_]} (split //, $octal);
return (join "", @threeBitSeqs);
}
my $result = 0b01011/0b11;
say "decimal: $result";
my $bin= conv2bin($result);
say "binary: $bin";
=head
D:\>perl bin_div.pl
decimal: 3.66666666666667
binary: 011 # 3 decimal (or 3.66667 truncated) expressed as binar
+y.
# Now, try this dividing -- oh, say, 6dec by 2dec...
=cut
| [reply] [Watch: Dir/Any] [d/l] [select] |