1

If this question is completely garbage I apologize because I am new to CS and haven't understood at how programming languages (Python specifically) work.

I was reading about how in a standard two-byte representation of a number, if one was to increment 65,536 times, the integer would wrap back to one. Would this case occur in Python, and if so at what number would this occur?

Edit: I read a little bit further into this and it seems that after an increment, programming languages seem to redefine the value. Would this prevent this from occur

1 Answers1

0

Your question is about python, but let me make it more general. Integers are represented in three major ways in modern computers:

  1. Unsigned integers of a fixed number of bits.
  2. Signed integers of a fixed number of bits.
  3. Bignums, also known by various other names such as multiple precision integers.

The first two exhibit the wrap-around behavior. The third doesn't.

Python uses three different representations for integers:

  1. Small integers are stored as "constants".
  2. Integers which fit into a signed machine word (plain integers) are stored as such.
  3. Larger integers (long integers) are stored as bignums.

This is described in the python documentation.

Plain integers are automatically promoted to long integers if need be. Therefore the wrapping around behavior does not happen.

On a 64-bit system, $2^{63}-1$ is the largest plain integer:

>>> (2**62-1)*2+1
9223372036854775807
>>> 2**63
9223372036854775808L

Note the suffix L, which indicates a long integer.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
  • 1
    This answer is correct for Python 2, but not for more recent versions, which have eliminated the distinction between int and long on the surface. – Draconis Dec 16 '18 at 06:23