Data::Integer(3pm)
β¨ NAME β¨
okay bestie, let's get into it. this is Data::Integer β aka Data::Integer(3pm) if you're checking the manpage header. it's literally all about the native integer data type, the whole vibe of how Perl stores integers natively. π
β¨ SYNOPSIS β¨
here's the quick teaser, the vibe check of what you can do. don't worry, we'll break it down. π
use Data::Integer qw(natint_bits);
$n = natint_bits;
# and other constants; see text
use Data::Integer qw(nint sint uint nint_is_sint nint_is_uint);
$ni = nint($ni);
$si = sint($si);
$ui = uint($ui);
if(nint_is_sint($ni)) { ...
if(nint_is_uint($ni)) { ...
use Data::Integer qw(
nint_sgn sint_sgn uint_sgn
nint_abs sint_abs uint_abs
nint_cmp sint_cmp uint_cmp
nint_min sint_min uint_min
nint_max sint_max uint_max
nint_neg sint_neg uint_neg
nint_add sint_add uint_add
nint_sub sint_sub uint_sub);
$sn = nint_sgn($ni);
$sn = sint_sgn($si);
$sn = uint_sgn($ui);
$ni = nint_abs($ni);
$si = sint_abs($si);
$ui = uint_abs($ui);
@sorted_nints = sort { nint_cmp($a, $b) } @nints;
@sorted_sints = sort { sint_cmp($a, $b) } @sints;
@sorted_uints = sort { uint_cmp($a, $b) } @uints;
$ni = nint_min($na, $nb);
$si = sint_min($sa, $sb);
$ui = uint_min($ua, $ub);
$ni = nint_max($na, $nb);
$si = sint_max($sa, $sb);
$ui = uint_max($ua, $ub);
$ni = nint_neg($ni);
$si = sint_neg($si);
$ui = uint_neg($ui);
$ni = nint_add($na, $nb);
$si = sint_add($sa, $sb);
$ui = uint_add($ua, $ub);
$ni = nint_sub($na, $nb);
$si = sint_sub($sa, $sb);
$ui = uint_sub($ua, $ub);
use Data::Integer qw(
sint_shl uint_shl
sint_shr uint_shr
sint_rol uint_rol
sint_ror uint_ror);
$si = sint_shl($si, $dist);
$ui = uint_shl($ui, $dist);
$si = sint_shr($si, $dist);
$ui = uint_shr($ui, $dist);
$si = sint_rol($si, $dist);
$ui = uint_rol($ui, $dist);
$si = sint_ror($si, $dist);
$ui = uint_ror($ui, $dist);
use Data::Integer qw(
nint_bits_as_sint nint_bits_as_uint
sint_bits_as_uint uint_bits_as_sint);
$si = nint_bits_as_sint($ni);
$ui = nint_bits_as_uint($ni);
$ui = sint_bits_as_uint($si);
$si = uint_bits_as_sint($ui);
use Data::Integer qw(
sint_not uint_not
sint_and uint_and
sint_nand uint_nand
sint_andn uint_andn
sint_or uint_or
sint_nor uint_nor
sint_orn uint_orn
sint_xor uint_xor
sint_nxor uint_nxor
sint_mux uint_mux);
$si = sint_not($si);
$ui = uint_not($ui);
$si = sint_and($sa, $sb);
$ui = uint_and($ua, $ub);
$si = sint_nand($sa, $sb);
$ui = uint_nand($ua, $ub);
$si = sint_andn($sa, $sb);
$ui = uint_andn($ua, $ub);
$si = sint_or($sa, $sb);
$ui = uint_or($ua, $ub);
$si = sint_nor($sa, $sb);
$ui = uint_nor($ua, $ub);
$si = sint_orn($sa, $sb);
$ui = uint_orn($ua, $ub);
$si = sint_xor($sa, $sb);
$ui = uint_xor($ua, $ub);
$si = sint_nxor($sa, $sb);
$ui = uint_nxor($ua, $ub);
$si = sint_mux($sa, $sb, $sc);
$ui = uint_mux($ua, $ub, $uc);
use Data::Integer qw(
sint_madd uint_madd
sint_msub uint_msub
sint_cadd uint_cadd
sint_csub uint_csub
sint_sadd uint_sadd
sint_ssub uint_ssub);
$si = sint_madd($sa, $sb);
$ui = uint_madd($ua, $ub);
$si = sint_msub($sa, $sb);
$ui = uint_msub($ua, $ub);
($carry, $si) = sint_cadd($sa, $sb, $carry);
($carry, $ui) = uint_cadd($ua, $ub, $carry);
($carry, $si) = sint_csub($sa, $sb, $carry);
($carry, $ui) = uint_csub($ua, $ub, $carry);
$si = sint_sadd($sa, $sb);
$ui = uint_sadd($ua, $ub);
$si = sint_ssub($sa, $sb);
$ui = uint_ssub($ua, $ub);
use Data::Integer qw(natint_hex hex_natint);
print natint_hex($value);
$value = hex_natint($string);
β¨ DESCRIPTION β¨
okay so this module is literally about the native integer numerical data type. a native integer is one of the types of datum that can show up in the numeric part of a Perl scalar. this module hands you constants that describe the native integer type. π
and bestie, there are actually two native integer representations: signed and unsigned. both are handled by this module, no cap. π
β¨ NATIVE INTEGERS β¨
each native integer format represents a value using binary place value, with some fixed number of bits. and the number of bits is the same for both signed and unsigned. in each case, the least-significant bit has the value 1, the next 2, the next 4, and so on. π’
in the unsigned representation, this pattern continues up to and including the most-significant bit, which for a 32-bit machine therefore has the value 2^31 (2147483648). the unsigned format cannot represent any negative numbers. periodt. π«
in the signed format, the most-significant bit is exceptional, having the negation of the value that it does in the unsigned format. thus on a 32-bit machine this has the value -2^31 (-2147483648). values with this bit set are negative, and those with it clear are non-negative; this bit is also known as the "sign bit". π
it's usual in machine arithmetic to use one of these formats at a time, for example to add two signed numbers yielding a signed result. however, Perl has a trick: a scalar with a native integer value contains an additional flag bit which indicates whether the signed or unsigned format is being used. it is therefore possible to mix signed and unsigned numbers in arithmetic, at some extra expense. slay. β¨
β¨ CONSTANTS β¨
each of the extreme-value constants has two names, a short one and a long one. the short names are more convenient to use, but the long names are clearer in a context where other similar constants exist. iykyk. π
due to the risks of Perl changing the behaviour of a native integer value that has been involved in floating point arithmetic (see "BUGS"), the extreme-value constants are actually non-constant functions that always return a fresh copy of the appropriate value. the returned value is always a pure native integer value, unsullied by floating point or string operations. no cap, it's giving fresh vibes only. π§Ό
natint_bitsβ the width, in bits, of the native integer data types. πmin_nint,min_natintβ the minimum representable value in either representation. this is-2^(natint_bits - 1). πmax_nint,max_natintβ the maximum representable value in either representation. this is2^natint_bits - 1. πmin_sint,min_signed_natintβ the minimum representable value in the signed representation. this is-2^(natint_bits - 1). π»max_sint,max_signed_natintβ the maximum representable value in the signed representation. this is2^(natint_bits - 1) - 1. πΊmin_uint,min_unsigned_natintβ the minimum representable value in the unsigned representation. this iszero. 0οΈβ£max_uint,max_unsigned_natintβ the maximum representable value in the unsigned representation. this is2^natint_bits - 1. π―
β¨ FUNCTIONS β¨
each nint_, sint_, or uint_ function operates on one of the three integer formats. nint_ functions operate on Perl's union of signed and unsigned; sint_ functions operate on signed integers; and uint_ functions operate on unsigned integers. except where indicated otherwise, the function returns a value of its primary type. π«Ά
parameters A, B, and C, where present, must be numbers of the appropriate type: specifically, with a numerical value that can be represented in that type. if there are multiple flavours of zero, due to floating point funkiness, all zeroes are treated the same. parameters with other names have other requirements, explained with each function. π
the functions attempt to detect unsuitable arguments, and die if an invalid argument is detected, but they can't notice some kinds of incorrect argument. generally, it is the caller's responsibility to provide a sane numerical argument, and supplying an invalid argument will cause mayhem. only the numeric value of plain scalar arguments is used; the string value is completely ignored, so dualvars are not a problem. π
β¨ Canonicalisation and classification β¨
these are basic glue functions. π§΄
nint(A),sint(A),uint(A)β these functions each take an argument in a specific integer format and return its numerical value. this is the argument canonicalisation that is performed by all of the functions in this module, presented in isolation. πnint_is_sint(A)β takes a native integer of either type. returns a truth value indicating whether this value can be exactly represented as a signed native integer. βnint_is_uint(A)β takes a native integer of either type. returns a truth value indicating whether this value can be exactly represented as an unsigned native integer. β
β¨ Arithmetic β¨
these functions operate on numerical values rather than just bit patterns. they will all die if the true numerical result doesn't fit into the result format, rather than give a wrong answer. so dramatic, but necessary. π€
nint_sgn(A),sint_sgn(A),uint_sgn(A)β returns+1if the argument is positive,0if the argument is zero, or-1if the argument is negative. ββnint_abs(A),sint_abs(A),uint_abs(A)β absolute value (magnitude, discarding sign). β¨nint_cmp(A, B),sint_cmp(A, B),uint_cmp(A, B)β arithmetic comparison. returns-1,0, or+1, indicating whetherAis less than, equal to, or greater thanB. βοΈnint_min(A, B),sint_min(A, B),uint_min(A, B)β arithmetic minimum. returns the arithmetically lesser of the two arguments. β¬οΈnint_max(A, B),sint_max(A, B),uint_max(A, B)β arithmetic maximum. returns the arithmetically greater of the two arguments. β¬οΈnint_neg(A),sint_neg(A),uint_neg(A)β negation: returns-A. πnint_add(A, B),sint_add(A, B),uint_add(A, B)β addition: returnsA + B. βnint_sub(A, B),sint_sub(A, B),uint_sub(A, B)β subtraction: returnsA - B. β
β¨ Bit shifting β¨
these functions all operate on the bit patterns representing integers, mostly ignoring the numerical values represented. in most cases the results for particular numerical arguments are influenced by the word size, because that determines where a bit being left-shifted will drop off the end of the word and where a bit will be shifted in during a rightward shift. π
With the exception of rightward shifts (see below), each pair of functions performs exactly the same operations on the bit sequences. And there inevitably can't be any functions here that operate on Perl's union of signed and unsigned β nope, that's not a thing π you gotta choose, by which function you call, which type the result is to be tagged as. Periodt.
sint_shl(A, DIST),uint_shl(A, DIST)β bitwise left shift (towards the more-significant bits, aka the fancy end π).DISTis the distance to shift, in bits, and it must be an integer in the range[0, natint_bits). Zeroes get shifted in from the right.sint_shr(A, DIST),uint_shr(A, DIST)β bitwise right shift (towards the less-significant bits).DISTis the distance to shift, in bits, and it must be an integer in the range[0, natint_bits).
ok bestie this one is giving plot twist π«’ when you do an unsigned right shift, zeroes get shifted in from the left. A signed right shift is different: the sign bit gets duplicated, so right-shifting a negative number always gives you a negative result. no cap.
sint_rol(A, DIST),uint_rol(A, DIST)β bitwise left rotation (towards more-significant bits, with the most-significant bit wrapping round to the least-significant bit π).DISTis the distance to rotate, in bits, and must be an integer in the range[0, natint_bits).sint_ror(A, DIST),uint_ror(A, DIST)β bitwise right rotation (towards less-significant bits, with the least-significant bit wrapping round to the most-significant bit π).DISTis the distance to rotate, in bits, and must be an integer in the range[0, natint_bits).
β¨ Format conversion β¨
These functions convert between the various native integer formats by reinterpreting the bit patterns used to represent the integers. The bit pattern stays exactly the same β it's the meaning that changes, and so the numerical value changes too. And here's the thing: Perl scalars preserve the numerical value, rather than just the bit pattern, so from the Perl point of view these are literally functions that change numbers into other numbers. it's giving glow-up but make it bits π«
nint_bits_as_sint(A)β converts a native integer of either type to a signed integer, by reinterpreting the bits. The most-significant bit (whether it was a sign bit or not) becomes a sign bit.nint_bits_as_uint(A)β converts a native integer of either type to an unsigned integer, by reinterpreting the bits. The most-significant bit (whether it was a sign bit or not) becomes an ordinary most-significant bit.sint_bits_as_uint(A)β converts a signed integer to an unsigned integer, by reinterpreting the bits. The sign bit becomes an ordinary most-significant bit.uint_bits_as_sint(A)β converts an unsigned integer to a signed integer, by reinterpreting the bits. The most-significant bit becomes a sign bit.
π Bitwise operations π
These functions all operate on the bit patterns representing integers, completely ignoring the numerical values represented. They are mostly not influenced by the word size β in the sense that they'll produce the same numerical result for the same numerical arguments regardless of word size. However, a few are affected by the word size: those on unsigned operands that return a non-zero result if given zero arguments. π
Each pair of functions performs exactly the same operations on the bit sequences. And again, there inevitably can't be any functions here that operate on Perl's union of signed and unsigned β you must choose, by which function you call, which type the result is to be tagged as. no shortcuts bestie π
sint_not(A),uint_not(A)β bitwise complement (NOT).sint_and(A, B),uint_and(A, B)β bitwise conjunction (AND).sint_nand(A, B),uint_nand(A, B)β bitwise inverted conjunction (NAND).sint_andn(A, B),uint_andn(A, B)β bitwise conjunction with inverted argument (A AND (NOT B)).sint_or(A, B),uint_or(A, B)β bitwise disjunction (OR).sint_nor(A, B),uint_nor(A, B)β bitwise inverted disjunction (NOR).sint_orn(A, B),uint_orn(A, B)β bitwise disjunction with inverted argument (A OR (NOT B)).sint_xor(A, B),uint_xor(A, B)β bitwise symmetric difference (XOR).sint_nxor(A, B),uint_nxor(A, B)β bitwise symmetric similarity (NXOR).sint_mux(A, B, C),uint_mux(A, B, C)β bitwise multiplex. The output has a bit fromBwhereverAhas a1bit, and a bit fromCwhereverAhas a0bit. That is, the result is(A AND B) OR ((NOT A) AND C). slay, it's a little switchboard π
π Machine arithmetic π
These functions perform arithmetic operations that are inherently influenced by the word size. They always produce a well-defined output if given valid inputs (which, love that for us). And yes, once again: there inevitably can't be any functions here that operate on Perl's union of signed and unsigned β you must choose, by which function you call, which type the result is to be tagged as. π€
sint_madd(A, B),uint_madd(A, B)β modular addition. The result for unsigned addition is(A + B) mod 2^natint_bits. The signed version behaves similarly, but with a different result range.sint_msub(A, B),uint_msub(A, B)β modular subtraction. The result for unsigned subtraction is(A - B) mod 2^natint_bits. The signed version behaves similarly, but with a different result range.sint_cadd(A, B, CARRY_IN),uint_cadd(A, B, CARRY_IN)β addition with carry. Two word arguments (AandB) and an input carry bit (CARRY_IN, which must have the value0or1) are all added together. Returns a list of two items: an output carry and an output word (of the same signedness as the inputs). Precisely, the output list(CARRY_OUT, R)is such thatCARRY_OUT*2^natint_bits + R = A + B + CARRY_IN. math girlies rise π§sint_csub(A, B, CARRY_IN),uint_csub(A, B, CARRY_IN)β subtraction with carry (borrow). The second word argument (B) and an input carry bit (CARRY_IN, which must have the value0or1) are subtracted from the first word argument (A). Returns a list of two items: an output carry and an output word (of the same signedness as the inputs). Precisely, the output list(CARRY_OUT, R)is such thatR - CARRY_OUT*2^natint_bits = A - B - CARRY_IN.sint_sadd(A, B),uint_sadd(A, B)β saturating addition. The result isA + Bif that will fit into the result format, otherwise the minimum or maximum value of the result format is returned, depending on the direction in which the addition overflowed. it said "I'm at capacity bestie" πsint_ssub(A, B),uint_ssub(A, B)β saturating subtraction. The result isA - Bif that will fit into the result format, otherwise the minimum or maximum value of the result format is returned, depending on the direction in which the subtraction overflowed.
π¦ String conversion π¦
natint_hex(VALUE)βVALUEmust be a native integer value. The function encodesVALUEin hexadecimal, returning that representation as a string. Specifically, the output is of the form"s0xdddd", where"s"is the sign and"dddd"is a sequence of hexadecimal digits.hex_natint(STRING)β generates and returns a native integer value from a string encoding it in hexadecimal. Specifically, the input format is"[s][0x]dddd", where"s"is the sign and"dddd"is a sequence of one or more hexadecimal digits. The input is interpreted case insensitively. If the value given in the string cannot be exactly represented in the native integer type, the function"die"s. π
The core Perl function "hex" (see "hex" in perlfunc) does a similar job to this function, but differs in several ways. Principally, "hex" doesn't handle negative values, and it gives the wrong answer for values that don't fit into the native integer type. In Perl 5.6 it also gives the wrong answer for values that don't fit into the native floating point type. It also doesn't enforce strict syntax on the input string. so like... hex_natint is the upgraded version, iykyk π
π BUGS π
In Perl 5.6, when a native integer scalar is used in any arithmetic other than specifically integer arithmetic, it gets partially transformed into a floating point scalar. Even if its numerical value can be represented exactly in floating point β so that floating point arithmetic uses the correct numerical value β some operations are affected by the floatness. In particular, the stringification of the scalar doesn't necessarily represent its exact value if it is tagged as floating point. not the vibe at all π
Because of this transforming behaviour, if you need to stringify a native integer it is best to ensure that it doesn't get used in any non-integer arithmetic first. If an integer scalar must be used in standard Perl arithmetic, it may be copied first and the copy operated upon, to avoid causing side effects on the original. If an integer scalar might have already been transformed, it can be cleaned by passing it through the canonicalisation function "nint". The functions in this module all avoid modifying their arguments, and always return pristine integers. β¨ (respectful, iconic behaviour honestly)
Perl 5.8+ still internally modifies integer scalars in the same circumstances, but seems to have corrected all the misbehaviour that resulted from it. π
Also in Perl 5.6, default Perl arithmetic doesn't necessarily work correctly on native integers. (This is part of the motivation for the myriad arithmetic functions in this module.) Default arithmetic here is strictly floating point, so if there are native integers that cannot be exactly represented in floating point, then the arithmetic will approximate the values before operating on them. Perl 5.8+ attempts to use native integer operations where possible in its default arithmetic, but as of Perl 5.8.8 it doesn't always succeed. For reliable integer arithmetic, integer operations must still be requested explicitly. fr fr, don't trust the vibes β be explicit π«Ά
π SEE ALSO π
Data::Float, Scalar::Number, perlnumber(1)
π AUTHOR π
Andrew Main (Zefram) <zefram@fysh.org>
Currently maintained by Robert Rothenberg <rrwo@cpan.org>
π COPYRIGHT π
Copyright (C) 2007, 2010, 2015, 2017, 2025 Andrew Main (Zefram) <zefram@fysh.org>
πΈ LICENSE πΈ
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie!! π this one came from perl v5.40.1, dated 2025-04-19 β go forth and integer responsibly β¨π
π¦ where this page comes from
- debian trixie: libdata-integer-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdata-integer-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdata-integer-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms