# How Python represents integers with Bignum (2023)

## Learn how Python represents integers of any size with Bignum.

·

follow

(Video) Integers in Python are represented byt the built-in "int" class. The great part is that ints

Published in

Taking coding to the next level

·

·

June 3, 2020

(Video) Bignums

--

Programmers writing in lower-level languages ​​like C/C++ must consider the amount of memory used to represent integers. They must take into account the minimum and maximum values ​​of the variable to avoid overflow problems. These developers should consider whether the`And t`is enough or if a`largo`it is necessary.

Compared to C/C++, Python has an advantage when working with integers. There is no integer overflow problem in Python; therefore, Python programmers don't have to worry about which type of variable to use for each integer. Python allows programmers to manipulate huge numbers without fear of losing precision. The only limitation of Python's integer representation is when the machine runs out of free memory, but that's a hardware limitation.

In practice, this is useful for calculating large values ​​such as factorials. Without using external libraries, Python can calculate factorials regardless of the size of the result. Here is an example of a factorial function:

`factorial definition(n):if n == 0 or n == 1:return 1return n * factorial(n-1)`

Exercise`delete`function with input`231`returns the magnitude of the integer that Python can represent.

(Video) Coding Interview | Increment an Arbitrary-Precision Integer (Python)

`>>> silnia(231)1792233667382633521618843263044232513197622942259968207385215805123682159320161029848328112148883186161436034535802659466 20511186710961457324231695438360438946452453546775940132626488356652304356081187317999607218815529008186162801025046843041 1 8549357073966058335409210318845715212791451245810943745474124030865641181439579407277346347694391122603830173024891069327 16 07996148737294252994723840000000000000000000000000000000000000000000000000000000000000`

Note that there are more efficient algorithms for calculating factorials; This example is used to illustrate the magnitude of the output.

integer representation

Before continuing with the discussion, please note that this article is only about the CPython implementation. This implementation is the default and most widely distributed version of Python. Different implementations may represent integers differently, but this discussion will only cover the representation of integers in CPython. One of the advantages of using CPython is that all of the codebase is publicly available in the CPython repository on Github.

As of Python 3, all integer values ​​are represented in the following structure:

`structure _long object {PyObject_VAR_HEADdigit ob_digit;};`

It is possible to expand macros and represent the structure as follows:

`structure {sssize_t ob_refcnt;estructura _typeobject *ob_type;ssize_tob_size;uint32_t ob_digit;};`

The first two elements of the above structure are not relevant to this discussion. Element`ob_refcnt`is used in Python's garbage collectors, and`type_ob`is used to identify the type, which in this case is an integer.

The total value is represented by the other two variables:`ob_digit`I`size_ob`. uses of python`ob_digit`an array to store each digit of a number separately in different index locations. Also, the ob_size variable is used to store two values. Stores the length`ob_digit`a matrix and an integer sign (positive or negative).

In most systems,`ob_digit`Es`uint32_t`array, but on some older machines`ob_digit`Maybe`uint16_t`training. In this article, we will consider only the first case.`uint32_t`mesas.

This method of representing integer values ​​by sequences of digits using strings or arrays is known as Bignum arithmetic. Typically, Bignum implementations represent binary values; however, this would not be space efficient`uint32_t`mesas.

Base 2³⁰

Given the systems they use.`uint32_t`arrays to represent integers, Python cannot use all 32 bits to hold a digit. The brief explanation for this limitation is that many built-in functions in Python require a certain number of bits to represent integers for performance and practical reasons. For the more curious reader, there are comments about this limitation in the official CPython repository.

Since Python can only use 30 of the 32 bits of each element, all integers are converted to base 2³⁰. Therefore, all digits in the array have values ​​between 0 and 1073741823 (2³⁰-1). Note that the variable`size_ob`stores the length of the array in base 2³⁰, not base 10.

Also, the matrix representation is in Little-Endian order. In other words, the order is least significant first (lowest index value). For example, suppose the numbers are written in base 10, not base 2³⁰.`234`represented by a matrix will be:`<4,3,2>`.

For example, a number`234254646549834273498`in Python it will first convert to base 2³⁰. Since we do not have enough characters to represent all the digits of a base 2³⁰ number, the digits of a base 2³⁰ number will be represented in base 10 for illustrative purposes.`234254646549834273498`in base 2³⁰ is`462328538,197050268,203`, Where`462328538`represents the first digit, and so on for the other two values. This is because 462328538 × (2³⁰)⁰ + 197050268 ×(2³⁰)¹ + 203 × (2³⁰)² = 234254646549834273498.

Therefore, the number`234254646549834273498`in Python base 2³⁰ it has 3 digits:`462328538,197050268,203`and this would be represented in Python as follows:

If that number were negative, the Python representation would have the same array but`size_ob`I would be`-3`.

Optimization of common integers.

This process of converting and representing integers using Bignum Arithmetic requires a lot of time for runtime operations. For this reason, since`And t`The type is immutable in Python, Python creates representations for all values ​​in between.`-5`I`256`before running the program. During execution, Python reuses these objects when requested.

One clear disadvantage of using Bignum arithmetic is memory usage. Any integer value in Python takes up at least 28 bytes of memory, which is 7 to 14 times what C would need to create a variable of type.`And t`.

(Video) BigInt for large numbers - JavaScript Tutorial for Beginners

One of the benefits of using Bignum Arithmetic is the simplicity of performing arithmetic operations. In this article we will only discuss addition, but the rest of the operations are based on the same concept.

The idea behind Bignum addition is to do addition the same way people add base 10 numbers with pencil and paper. The process starts with the least significant digit and continues to the most significant digits. Each digit of each number is added separately and the result is shifted oneto usevalue to the next higher digit.

Bignum Arithmetic takes this approach using matrices. The process is to add each value at the same index of each separate array and move the value that exceeds the decimal to the next index. The algorithm starts at the index.`0`and iterates to the length of the smallest array, adding digits using the carry method.

The algorithm starts by creating a new empty array to hold the result. Note that the result of the sum of the two values ​​has a maximum of one digit more than the greater number of the sum. For example, the sum between`9`I`93`Es`102`. The highest value in this example is`93`and has 2 digits. The result of the addition has 3 digits, one more than`93`. In some cases, the number of digits in the result is equal to the number of digits in the larger number. In this case, the algorithm reduces the size of the matrix to fit the result without`0`'S The only case where the last cell in the array (upper index) has a value`0`used to represent a number`0`.

For illustrative purposes, here is an example of how to add two numbers represented by Bignum arithmetic. The added values ​​are`234254646549834273498`I`23425464654983`.

Number`234254646549834273498`will be represented like this:

I number`23425464654983`will be represented like this:

The algorithm starts by creating a new array of size 4 (one cell longer than the array that represents the largest value in the appendix).

The algorithm then starts the index-by-index transfer process.

After traversing all the indices down to the smallest length in the array, the algorithm calculates the following values:

Finally, the program reduces the size of the array by 1 to remove the last empty cell. Then the entire structure created from the plugin is represented as follows:

The original algorithm is written in C, but here's a Python function that simulates the process.

(Video) [JA] Unifying Fixnum and Bignum into Integer / Tanaka Akira

Function`add`takes two python lists. Each list represents one of the integers added in this function. The numbers are already converted to base 2³⁰ and each item in the list stores one digit of the value.

In short, Python uses Bignum arithmetic to represent integers. Compared to other languages ​​like Java and C/C++, Python makes working with integers a breeze. While other languages ​​require the programmer to specify a size variable to hold the number, Python bypasses this need. However, this method also has a drawback in terms of memory consumption. While languages ​​like C use 2 or 4 bytes to represent a variable of type`And t`, Python requires at least 28 bytes. For simple scripts, this additional memory usage makes no difference; however, for data-intensive programs, it may be interesting to use other languages ​​such as C.

## Videos

1. Python Tip: Constants and Reading large numbers in Python
(John Philip Jones)
2. BigNum class (Assignment 04)
(OOP at DSU)
3. Arrays in Python: Arbitrary Precision Increment
(LucidProgramming)
4. Assessing And Exploiting BigNum Vulnerabilities
(Black Hat)
5. Computing in arbitrary precision
(Sharcnet HPC)
6. Really Big Numbers in C for Cryptography
(Eric O Meehan)
Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated: 07/25/2023

Views: 6341

Rating: 4.8 / 5 (78 voted)

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.