Principle:ClickHouse ClickHouse Wide Integer Arithmetic
| Knowledge Sources | |
|---|---|
| Domains | Numeric_Types, Arithmetic |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Multi-precision integer arithmetic supporting integers larger than native CPU register size.
Description
Native CPU integers are typically 64-bit maximum. Wide integer arithmetic implements larger integers (128, 256, or arbitrary bits) using arrays of smaller integers. Operations are implemented with carry propagation (addition), multi-word algorithms (multiplication), and special handling for signed values. ClickHouse uses wide integers for Int128/UInt128/Int256/UInt256 required by SQL standard and cryptographic operations.
Usage
Use for cryptography, exact large number calculations, financial computations requiring > 64-bit precision, or implementing SQL DECIMAL256.
Theoretical Basis
Multi-Word Representation: Store N-bit integer as array of 64-bit words plus logic for carry/borrow.
Algorithm Complexity: Addition/subtraction O(n), multiplication O(n²) naive or O(n log n) with Karatsuba, division O(n²).
Two's Complement: Signed wide integers use two's complement, requiring careful sign extension.
Template Metaprogramming: Generic implementation works for any bit width at compile time.