NAME

Math::Int32 - Manipulate 32 bits integers in Perl

SYNOPSIS

use Math::Int32 qw(int32);

my $i = int32(1);
my $j = $i << 40;
my $k = int32("12345678901234567890");
print($i + $j * 1000000);

DESCRIPTION

This module adds support for 32 bit integers, signed and unsigned, to Perl.

EXPORTABLE FUNCTIONS

int32()
int32($value)

Creates a new int32 value and initializes it to $value, where $value can be a Perl number or a string containing a number.

For instance:

$i = int32(34);
$j = int32("-123454321234543212345");

$k = int32(1234567698478483938988988); # wrong!!!
                                       #  the unquoted number would
                                       #  be converted first to a
                                       #  real number causing it to
                                       #  loose some precision.

Once the int32 number is created it can be manipulated as any other Perl value supporting all the standard operations (addition, negation, multiplication, postincrement, etc.).

net_to_int32($str)

Converts an 8 bytes string containing an int32 in network order to the internal representation used by this module.

int32_to_net($int32)

Returns an 8 bytes string with the representation of the int32 value in network order.

native_to_int32($str)
int32_to_native($int32)

similar to net_to_int32 and int32_to_net, but using the native CPU order.

int32_to_number($int32)

returns the optimum representation of the int32 value using Perl internal types (IV, UV or NV). Precision could be lost.

For instance:

for my $l (10, 20, 30, 40, 50, 60) {
  my $i = int32(1) << $l;
  my $n = int32_to_number($i);
  print "int32:$i => perl:$n\n";
}
uint32
uint32_to_number
net_to_uint32
uint32_to_net
native_to_uint32
uint32_to_native

These functions are similar to their int32 counterparts, but manipulate 32 bit unsigned integers.

BUGS AND SUPPORT

At this moment, this module requires int32 support from the C compiler. Also, it doesn't take any advantage of perls with 32 bit IVs.

For bug reports, feature requests or just help using this module, use the RT system at http://rt.cpan.org or send my and email or both!

SEE ALSO

Other modules that allow Perl to support larger integers or numbers are Math::BigInt, Math::BigRat and Math::Big, Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.

AUTHOR

Salvador Fandiño, <sfandino@yahoo.com>

COPYRIGHT AND LICENSE

Copyright (C) 2007 by Salvador Fandiño

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.