C-like Unsigned Integers

0.2.2 · abandoned · verified Sun Apr 19

The `cuint` library provides functionality for handling C-like unsigned 32-bit and 64-bit integers in JavaScript, a capability not natively supported by the language. It represents unsigned integers as objects, splitting them into 16-bit segments (e.g., two for UINT32, four for UINT64) to emulate fixed-width arithmetic. Designed for performance, it aims to provide C-like behavior, including truncation on overflow. The current and only stable version is 0.2.2, last updated approximately 9 years ago (as of 2026). Due to its age and lack of updates, it is considered an abandoned project, meaning it likely does not receive bug fixes, security patches, or compatibility updates for newer JavaScript environments or Node.js versions. Its primary differentiator was bringing explicit unsigned integer arithmetic to JavaScript before `BigInt` was standardized.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing `UINT32` and `UINT64` in Node.js, instantiating unsigned integer objects, performing in-place addition, and showcasing the `clone()` method for immutable operations, highlighting the library's C-like behavior with fixed-width arithmetic.

// In Node.js environment
const { UINT32, UINT64 } = require('cuint');

// Instantiate a 32-bit unsigned integer
const thirtyTwoBitValue = UINT32('4294967290'); // Close to max 32-bit unsigned value
console.log(`Initial UINT32 value: ${thirtyTwoBitValue.toString()}`);

// Instantiate another 32-bit value
const smallThirtyTwoBitValue = UINT32(10);

// Perform an addition (modifies thirtyTwoBitValue in place)
thirtyTwoBitValue.add(smallThirtyTwoBitValue);
console.log(`UINT32 after adding 10: ${thirtyTwoBitValue.toString()}`); // Will demonstrate truncation if it overflows

// Instantiate a 64-bit unsigned integer
const sixtyFourBitValue = UINT64('18446744073709551600'); // Close to max 64-bit unsigned value
console.log(`Initial UINT64 value: ${sixtyFourBitValue.toString()}`);

// Instantiate another 64-bit value
const smallSixtyFourBitValue = UINT64(15);

// Perform an addition (modifies sixtyFourBitValue in place)
sixtyFourBitValue.add(smallSixtyFourBitValue);
console.log(`UINT64 after adding 15: ${sixtyFourBitValue.toString()}`);

// Demonstrate cloning for immutable operations
const originalValue = UINT32(100);
const addedValue = UINT32(50);
const resultValue = originalValue.clone().add(addedValue);
console.log(`Original value (immutable check): ${originalValue.toString()}`); // Should still be 100
console.log(`Result of clone().add(): ${resultValue.toString()}`); // Should be 150

view raw JSON →