PHPackages                             adept-digital/int-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. adept-digital/int-utils

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

adept-digital/int-utils
=======================

Utilities for emulating WebAssembly 32-bit and 64-bit integer operations in PHP.

0.1.0(9mo ago)11LGPL-3.0-or-laterPHP

Since Aug 13Pushed 9mo agoCompare

[ Source](https://github.com/adept-digital/int-utils)[ Packagist](https://packagist.org/packages/adept-digital/int-utils)[ RSS](/packages/adept-digital-int-utils/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Integer Utilities
=================

[](#integer-utilities)

Utilities for emulating WebAssembly 32-bit and 64-bit integer operations in PHP. These may be useful for porting an application or library from C, C++, or other languages.

Requirements
------------

[](#requirements)

This package requires a 64-bit build of PHP 8.4 or later.

There is no dependency on external Composer packages or PHP extensions.

Installation
------------

[](#installation)

Install via [Composer](https://getcomposer.org/doc/00-intro.md).

```
composer require adept-digital/int-utils
```

Usage
-----

[](#usage)

There are two classes with near-identical interfaces:

- `\AdeptDigital\IntUtils\Int32`
- `\AdeptDigital\IntUtils\Int64`

The following operations are provided:

- `add(int $a, int $b): int`: Integer addition.
- `sub(int $a, int $b): int`: Integer subtraction.
- `mul(int $a, int $b): int`: Integer multiplication.
- `div_s(int $a, int $b): int`: Signed integer division. [1](#user-content-fn-1-3f3466f1c00d66787a07c9829205ad73)
- `div_u(int $a, int $b): int`: Unsigned integer division.
- `rem_s(int $a, int $b): int`: Signed integer modulo. [1](#user-content-fn-1-3f3466f1c00d66787a07c9829205ad73)
- `rem_u(int $a, int $b): int`: Unsigned integer modulo.
- `eq(int $a, int $b): int`: Equal comparison. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `eqz(int $a): int`: Equals zero test. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `ne(int $a, int $b): int`: Not equal comparison. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `lt_s(int $a, int $b): int`: Signed less than comparison. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `lt_u(int $a, int $b): int`: Unsigned less than comparison.
- `le_s(int $a, int $b): int`: Signed less than or equal comparison. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `le_u(int $a, int $b): int`: Unsigned less than or equal comparison.
- `gt_s(int $a, int $b): int`: Signed greater than comparison. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `gt_u(int $a, int $b): int`: Unsigned greater than comparison.
- `ge_s(int $a, int $b): int`: Signed greater than or equal comparison. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `ge_u(int $a, int $b): int`: Unsigned greater than or equal comparison.
- `and(int $a, int $b): int`: Bitwise AND. [1](#user-content-fn-1-3f3466f1c00d66787a07c9829205ad73)
- `or(int $a, int $b): int`: Bitwise OR. [1](#user-content-fn-1-3f3466f1c00d66787a07c9829205ad73)
- `xor(int $a, int $b): int`: Bitwise XOR. [1](#user-content-fn-1-3f3466f1c00d66787a07c9829205ad73)
- `shl(int $a, int $b): int`: Shift left. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `shr_s(int $a, int $b): int`: Signed (arithmetic) shift right. [2](#user-content-fn-2-3f3466f1c00d66787a07c9829205ad73)
- `shr_u(int $a, int $b): int`: Unsigned (logical) shift right.
- `rotl(int $a, int $b): int`: Rotate left.
- `rotr(int $a, int $b): int`: Rotate right.
- `clz(int $a): int`: Count leading zeros.
- `ctz(int $a): int`: Count trailing zeros.
- `popcnt(int $a): int`: Population count.
- `extend8_s(int $a): int`: Extend the sign of the least significant 8 bits.
- `extend16_s(int $a): int`: Extend the sign of the least significant 16 bits.
- `extend32_s(int $a): int`: Extend the sign of the least significant 32 bits. [3](#user-content-fn-3-3f3466f1c00d66787a07c9829205ad73)

32-bit representation
---------------------

[](#32-bit-representation)

PHP has no 32-bit integer type, so 32-bit numbers are represented in 64-bit, but are limited to the range of a signed 32-bit integer (`-2,147,483,648` to `2,147,483,647`). For example, `4,294,967,295` (`2**32-1`) should be represented as `-1`. Using numbers outside this range will cause a `RangeException` to be thrown, unless assertions are disabled, in which case the result is undefined.

To correctly format an unsigned 32-bit integer, unset the most significant 32 bits:

```
use \AdeptDigital\IntUtils\Int32;

echo Int32::add(Int32::MAX, 1) & 0xFFFF_FFFF;
// output: 2147483648
```

64-bit representation
---------------------

[](#64-bit-representation)

PHP has no unsigned integer type, so numbers larger than `PHP_INT_MAX` will appear to be negative.

To correctly format an unsigned integer, use `printf()` or `sprintf()`:

```
use \AdeptDigital\IntUtils\Int64;

printf('%u', Int64::add(Int64::MAX, 1));
// output: 9223372036854775808
```

Contributing
------------

[](#contributing)

All contributions are welcome. If you would like to work on a new feature, consider creating an issue before starting.

TODO
----

[](#todo)

- Add benchmarks and benchmark on different machines.
- Add alternative implementations (eg: GMP, FFI, custom extension).
- Add feature detection for alternative implementations.

License
-------

[](#license)

Copyright 2025 David Gallagher .

The package is licensed under the GNU Lesser General Public License 3.0 or later.

Footnotes
---------

1. Method is only provided for completeness. It does nothing different when compared to the equivalent PHP operator. [↩](#user-content-fnref-1-3f3466f1c00d66787a07c9829205ad73) [↩2](#user-content-fnref-1-2-3f3466f1c00d66787a07c9829205ad73) [↩3](#user-content-fnref-1-3-3f3466f1c00d66787a07c9829205ad73) [↩4](#user-content-fnref-1-4-3f3466f1c00d66787a07c9829205ad73) [↩5](#user-content-fnref-1-5-3f3466f1c00d66787a07c9829205ad73)
2. Method only provides a trivial transformation on inputs or output. [↩](#user-content-fnref-2-3f3466f1c00d66787a07c9829205ad73) [↩2](#user-content-fnref-2-2-3f3466f1c00d66787a07c9829205ad73) [↩3](#user-content-fnref-2-3-3f3466f1c00d66787a07c9829205ad73) [↩4](#user-content-fnref-2-4-3f3466f1c00d66787a07c9829205ad73) [↩5](#user-content-fnref-2-5-3f3466f1c00d66787a07c9829205ad73) [↩6](#user-content-fnref-2-6-3f3466f1c00d66787a07c9829205ad73) [↩7](#user-content-fnref-2-7-3f3466f1c00d66787a07c9829205ad73) [↩8](#user-content-fnref-2-8-3f3466f1c00d66787a07c9829205ad73) [↩9](#user-content-fnref-2-9-3f3466f1c00d66787a07c9829205ad73)
3. Only available on the `\AdeptDigital\IntUtils\Int64` class. [↩](#user-content-fnref-3-3f3466f1c00d66787a07c9829205ad73)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance58

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

279d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/593ed74eb68e3ea204471e4fc684ae8f50a6c6dcfdf44af80687956e8a935346?d=identicon)[thegallagher](/maintainers/thegallagher)

---

Top Contributors

[![thegallagher](https://avatars.githubusercontent.com/u/486626?v=4)](https://github.com/thegallagher "thegallagher (1 commits)")

---

Tags

integerwraprotateunsignedwebassemblywasmoverflowpop counttrailing zeroleading zerological shift

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/adept-digital-int-utils/health.svg)

```
[![Health](https://phpackages.com/badges/adept-digital-int-utils/health.svg)](https://phpackages.com/packages/adept-digital-int-utils)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[skleeschulte/base32

Base32 encoding and decoding class (RFC 4648, RFC 4648 extended hex, Crockford, z-base-32/Zooko).

17314.5k9](/packages/skleeschulte-base32)[aza/math

AzaMath - Anizoptera CMF mathematic component. Arbitrary precision arithmetic (for huge integers; BCMath wrapper) and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, can use GMP and core PHP functions for speed optimization).

1921.9k1](/packages/aza-math)[phpmath/biginteger

A PHP library to work with big integers.

235.8k1](/packages/phpmath-biginteger)[danog/phpstruct

PHP implementation of python's struct module.

1110.1k](/packages/danog-phpstruct)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
