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

built in function for binary division

by ghosh123 (Monk)
on Jan 11, 2013 at 04:51 UTC ( #1012790=perlquestion: print w/replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk

I wanted to know is there any built-in function in Perl for binary division ?
Or any other built-in function for binary operation ?

Replies are listed 'Best First'.
Re: built in function for binary division
by Kenosis (Priest) on Jan 11, 2013 at 05:31 UTC

    Do you mean something like the following?

    use strict; use warnings; my $quotient = 0b10010110 / 0b1111; # 150 / 15 print sprintf '%b', $quotient; # prints 1010 (decimal 10)

      Hi Kenosis,

      Yes, this is what I meant. Is it possible to do by any built-in perl function ? Else any hint to achieve that ?

        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?

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?

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

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

Re: built in function for binary division
by AnomalousMonk (Chancellor) on Jan 11, 2013 at 09:12 UTC

    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
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'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 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012790]
Approved by vinoth.ree
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-06-20 21:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.