ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BinaryInteger.h File Reference

The arithmetic on integers of \( n \) bits, vectors of bits from the least significant one, of section 2.6: More...

#include "ADS/Contract.h"
#include "ADS/numeric/Polynomial.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <vector>
#include "ADS/numeric/impl/BinaryInteger.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Typedefs

using ADS::Bits = std::vector< int >
 the bits, the least significant first

Functions

Bits ADS::toBits (std::uint64_t x, std::size_t n=0)
 the bits of \( x \), at least \( n \) of them
std::uint64_t ADS::fromBits (const Bits &b)
 the value \( \sum_i b_i 2^i \) of the bits, which must be at most 64
Bits ADS::carry (const std::vector< long > &c)
 the coefficients \( c_i \), possibly larger than 1 or negative, made bits of the same value \( \sum_i c_i 2^i \) by the carries
Bits ADS::binaryAdd (const Bits &X, const Bits &Y)
 the sum \( X + Y \), \( \Theta( n ) \)
std::vector< long > ADS::widen (const Bits &X)
 the bits of \( X \) as the coefficients of a polynomial in \( x = 2 \)
Bits ADS::multiplyBinarySchool (const Bits &X, const Bits &Y, long *mults=nullptr)
 the product \( X Y \) by the method of the school, \( \Theta( n^2 ) \); the products of two bits added to *mults, if given
Bits ADS::multiplyBinaryFour (const Bits &X, const Bits &Y, long *mults=nullptr)
 the product \( X Y \) by four products of half size, \( \Theta( n^2 ) \); the products of two bits added to *mults, if given
Bits ADS::multiplyBinaryKaratsuba (const Bits &X, const Bits &Y, long *mults=nullptr)
 the product \( X Y \) by the three products of Karatsuba, \( O( n^{1.59} ) \); the products of two bits added to *mults, if given
std::uint64_t ADS::ahmes (std::uint64_t a, std::uint64_t b, long *steps=nullptr)
 the product \( a b \) by the multiplication of Ahmes; the halvings added to *steps, if given

Detailed description

The arithmetic on integers of \( n \) bits, vectors of bits from the least significant one, of section 2.6:

  • binaryAdd(), the sum with the propagation of the carry, \( \Theta( n ) \), optimal since every bit must be read;
  • multiplyBinarySchool(), the product of the school, a partial product per bit, \( \Theta( n^2 ) \);
  • multiplyBinaryFour() and multiplyBinaryKaratsuba(), with \( X = a 2^{n/2} + b \) and \( Y = c 2^{n/2} + d \), by four products of half size or by the three of Karatsuba, \( \Theta( n^2 ) \) and \( \Theta( n^{\log_2 3} ) \): the products of the polynomials of the bits [see Polynomial.h] followed by the carries;
  • ahmes(), the multiplication of the papyrus of Ahmes (1650 BC), which adds \( b \) to \( p \) when \( a \) is odd, halves \( a \) and doubles \( b \), until \( a = 0 \): from \( a b = ( a / 2 ) ( 2 b ) \) for \( a \) even and \( a b = ( ( a - 1 ) / 2 ) ( 2 b ) + b \) for \( a \) odd, \( O( \log a ) \) steps, the shift and add of the arithmetic units.

The contracts [see Contract.h]: a vector of bits holds only 0 and 1, an audit on the inputs of the sums and of the products, and fromBits() needs at most 64 of them; toBits() gives at least \( n \) bits whose value is \( x \), and ahmes() gives the product modulo \( 2^{64} \). The functions are defined in BinaryInteger.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, sections 1.4 and 2.6, https://algoritmicamente.it/materiali/libro/capitoli/cap02.pdf
A. Karatsuba, Yu. Ofman, Multiplication of multidigit numbers on automata, Soviet Physics Doklady 7, 595-596, 1963
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa