LogicGates.org Open the simulatorSimulator

int64: the 64-bit signed integer

int64 holds every whole number from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, which is −2⁶³ to 2⁶³ − 1. That is 2⁶⁴ = 18,446,744,073,709,551,616 values in 64 bits, stored in two’s complement.

int64 limits

Maximum
9,223,372,036,854,775,807
power2⁶³ − 1
hex0x7FFFFFFFFFFFFFFF
binary0111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111
Minimum
−9,223,372,036,854,775,808
power−2⁶³
hex0x8000000000000000
binary1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
Distinct values
2⁶⁴ = 18,446,744,073,709,551,616
Size
64 bits, 8 bytes

The hex and binary forms are the bit patterns. The top bit is the sign: 0 for the maximum, 1 for the minimum.

int64 overflow playground

Starts at the maximum, one step from overflowing. Change the operation, the value or the type, or cast to another width, and watch the bits wrap.

Decimal, 0x hex or 0b binary, with a minus sign if negative. 2^31 − 1 works too.

int64: 9,223,372,036,854,775,807 +1 −9,223,372,036,854,775,808 Overflow: wrapped past the maximum to the bottom of the range
Before 0x7FFFFFFFFFFFFFFF
Exact answer 9,223,372,036,854,775,808 written in 65 bits: the leftmost bit does not fit and is dropped
After 0x8000000000000000

The solid underlined bit is the sign bit: 1 means negative in a signed type. Struck-through bits are dropped.

  1. Exact answer: 9,223,372,036,854,775,808.
  2. That is above the maximum of int64, 9,223,372,036,854,775,807.
  3. The hardware keeps only the low 64 bits, which is the same as taking the answer modulo 2⁶⁴ = 18,446,744,073,709,551,616: 9,223,372,036,854,775,808.
  4. The top bit is 1, so read as signed it is 9,223,372,036,854,775,808 − 18,446,744,073,709,551,616 = −9,223,372,036,854,775,808.

What int64 is called in each language

Language Type Limits in code Note
C and C++ int64_t INT64_MIN, INT64_MAX long is 64 bits on 64-bit Linux and macOS but 32 bits on Windows; long long is at least 64 everywhere.
Java long Long.MIN_VALUE, Long.MAX_VALUE
C# long long.MinValue, long.MaxValue
Rust i64 i64::MIN, i64::MAX isize is the pointer width: 64 bits on 64-bit targets.
Go int64 math.MinInt64, math.MaxInt64 int is 32 or 64 bits, depending on the platform.
Swift Int64 Int64.min, Int64.max Int is the platform word size: 64 bits on 64-bit platforms.
Kotlin Long Long.MIN_VALUE, Long.MAX_VALUE
MySQL BIGINT
PostgreSQL bigint (int8) The 8 in int8 counts bytes, not bits.
SQL Server bigint
JavaScript BigInt64Array element An ordinary number is exact only up to Number.MAX_SAFE_INTEGER, 2⁵³ − 1. BigInt.asIntN(64, x) wraps a BigInt into 64 bits.
Python ctypes.c_int64

Only names with exactly 64 bits on every platform are listed as the type. Types whose width depends on the platform, such as C’s long, Go’s int and Rust’s isize, are in the notes.

What happens when int64 overflows

In 64 bits, 9,223,372,036,854,775,807 + 1 wraps to −9,223,372,036,854,775,808, and −9,223,372,036,854,775,808 − 1 wraps to 9,223,372,036,854,775,807. The processor keeps the low 64 bits of the answer, which is the same as working modulo 2⁶⁴, and the top bit then decides the sign. You can see it in JavaScript:

BigInt.asIntN(64, 9223372036854775807n + 1n) → -9223372036854775808n

Whether a program is allowed to wrap like that depends on the language:

What each language does when integer arithmetic overflows
Language By default To check or wrap on purpose
C and C++ Unsigned types wrap around modulo 2ⁿ. Signed overflow is undefined behaviour: the compiler may assume it never happens and optimise on that basis. Types narrower than int are promoted to int first, so even uint16_t × uint16_t can overflow a signed int. __builtin_add_overflow (GCC, Clang); ckd_add in C23’s <stdckdint.h>; -fsanitize=undefined to catch it
Java Wraps around silently, in two’s complement. Math.addExact, multiplyExact and friends throw ArithmeticException
C# Wraps around, unless the code is in a checked context, which throws OverflowException. A constant expression that overflows is a compile error. checked(...) blocks, or the CheckForOverflowUnderflow compiler option
Rust Panics in debug builds. Release builds wrap, unless overflow-checks is turned on for that profile. checked_add, wrapping_add, saturating_add, overflowing_add
Go Wraps around silently. Only constant expressions that overflow are compile errors. math/bits.Add64 reports the carry; Mul64 returns the high half of the product
Swift Traps: the program stops with a runtime error. &+, &-, &* wrap on purpose; addingReportingOverflow reports it
Kotlin Wraps around silently, like Java. Math.addExact on the JVM
JavaScript Numbers never wrap, but lose precision past 2⁵³. Bitwise operators and typed arrays wrap to their width. BigInt never overflows. Number.isSafeInteger; BigInt.asIntN and asUintN to wrap deliberately
Python int never overflows: it grows as needed. ctypes and NumPy fixed-width types do wrap
SQL An error: the statement fails with an out of range or arithmetic overflow message. MySQL outside strict mode clips an out-of-range value stored into a column to the limit, with a warning

Common mistakes with int64

  • Negating −9,223,372,036,854,775,808 or taking its absolute value. The exact answer, 9,223,372,036,854,775,808, is one past the maximum of 9,223,372,036,854,775,807, so once it is stored as int64 it wraps back to −9,223,372,036,854,775,808: −x and abs(x) of the minimum stay negative wherever the result wraps, and in C and C++ the overflow is undefined behaviour.
  • Finding a midpoint as (low + high) / 2. When low and high are both 4,611,686,018,427,387,904 or more, just over half the maximum, their sum overflows int64. low + (high − low) / 2 gives the same midpoint without the overflow.
  • Sending 64-bit IDs through JSON to JavaScript. JSON.parse turns every number into a double, which is exact only up to 2⁵³ − 1. An ID of 9,007,199,254,740,993 arrives as 9,007,199,254,740,992. Send large IDs as strings.

When int64 overflowed for real

64-bit Unix time

With a signed 64-bit count of seconds, Unix time lasts about 292 billion years either side of 1970, which is why moving to 64 bits settles the 2038 problem for good.

Where you meet int64

  • Unix time in modern systems
  • long in Java, C# and Kotlin
  • Database primary keys (bigint)

Need no negatives and twice the reach? The unsigned type of the same width is uint64, from 0 to 18,446,744,073,709,551,615.

Every integer type

Type Minimum Maximum
int8 −128 127
uint8 0 255
int16 −32,768 32,767
uint16 0 65,535
int32 −2,147,483,648 2,147,483,647
uint32 0 4,294,967,295
int64 this page −9,223,372,036,854,775,808 9,223,372,036,854,775,807
uint64 0 18,446,744,073,709,551,615
int128 −170,141,183,460,469,231,731,687,303,715,884,105,728 170,141,183,460,469,231,731,687,303,715,884,105,727
uint128 0 340,282,366,920,938,463,463,374,607,431,768,211,455

Questions

What is the maximum value of int64?

9,223,372,036,854,775,807, which is 2⁶³ − 1 or 0x7FFFFFFFFFFFFFFF in hex. The minimum is −9,223,372,036,854,775,808 (−2⁶³).

Why is the minimum of int64 one further from zero than the maximum?

64 bits make 2⁶⁴ patterns. Two’s complement gives the half with the top bit set to the negatives, −9,223,372,036,854,775,808 to −1, and the other half to zero and the positives, 0 to 9,223,372,036,854,775,807. Zero takes one of the non-negative patterns, so there is one more negative number than positive. That is also why negating −9,223,372,036,854,775,808 overflows back to itself.

How many values can int64 hold?

2⁶⁴ = 18,446,744,073,709,551,616 distinct values, from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

What happens when int64 overflows?

On the hardware the result keeps only its low 64 bits, so 9,223,372,036,854,775,807 + 1 becomes −9,223,372,036,854,775,808. Java, Kotlin, Go and C# (outside a checked context) do exactly that. In C and C++ signed overflow is undefined behaviour; Rust panics in debug builds and wraps in release builds; Swift stops with a runtime error.

What is int64 called in other languages?

C and C++: int64_t; Java: long; C#: long; Rust: i64; Go: int64; Swift: Int64; Kotlin: Long; Python: ctypes.c_int64. In SQL: BIGINT in MySQL, bigint (int8) in PostgreSQL, bigint in SQL Server.