zaro

What are the sizes of integers in Julia?

Published in Julia Integer Sizes 3 mins read

Julia provides a comprehensive set of integer types, allowing for precise control over memory usage and value ranges. These types are primarily distinguished by their bit size, determining the maximum and minimum values they can hold, and whether they are signed (can represent negative numbers) or unsigned (only non-negative numbers).

Standard Integer Types

Julia includes signed and unsigned integer types for 8, 16, 32, 64, and 128 bits. Each type explicitly states its bit size, ensuring predictable behavior across different systems.

Here's a breakdown of the standard integer types in Julia, their bit sizes, and their corresponding value ranges:

Type Bits Signed? Minimum Value Maximum Value
Int8 8 -128 (-2^7) 127 (2^7 - 1)
UInt8 8 0 255 (2^8 - 1)
Int16 16 -32,768 (-2^15) 32,767 (2^15 - 1)
UInt16 16 0 65,535 (2^16 - 1)
Int32 32 -2,147,483,648 (-2^31) 2,147,483,647 (2^31 - 1)
UInt32 32 0 4,294,967,295 (2^32 - 1)
Int64 64 -9,223,372,036,854,775,808 (-2^63) 9,223,372,036,854,775,807 (2^63 - 1)
UInt64 64 0 18,446,744,073,709,551,615 (2^64 - 1)
Int128 128 -2^127 2^127 - 1
UInt128 128 0 2^128 - 1

Note: The values highlighted in bold are specifically mentioned in the reference as examples of integer types and their largest values.

Default Integer Types (Int and UInt)

Julia also defines default integer types, Int and UInt, which are aliases for the native integer size of your system. This makes code more portable while leveraging optimal performance.

  • On 64-bit systems (the most common today), Int is an alias for Int64, and UInt is an alias for UInt64.
  • On 32-bit systems, Int would be Int32, and UInt would be UInt32.

You can check the default integer size on your system by running typemin(Int) or typemax(Int) in the Julia REPL.

Practical Insights and Examples

Understanding integer sizes is crucial for avoiding overflow errors, which occur when a calculation results in a number larger or smaller than the type can hold.

  • Checking Type and Range:

    # Check the type of an integer literal
    typeof(1)  # Output: Int64 (on a 64-bit system)
    
    # Check the default unsigned integer type
    typeof(0x1) # Output: UInt64 (on a 64-bit system)
    
    # Get the minimum and maximum values for a specific type
    typemin(Int32)  # Output: -2147483648
    typemax(UInt16) # Output: 65535
    
    # Check the bit size of a type
    sizeof(Int16)   # Output: 2 bytes (16 bits)
  • Explicit Type Declaration:
    You can explicitly specify the type of an integer literal by preceding the number with the type name, or by appending a hexadecimal literal with U for unsigned types.

    x = Int8(100)       # x is an Int8
    y = UInt16(50000)   # y is a UInt16
    z = 0xFF            # z is a UInt8 by default for hex literals fitting in 8 bits
    w = 0xFFFFu         # w is an unsigned integer, typically UInt16 or larger

For more in-depth information, refer to the official Julia documentation on integers and floating-point numbers: Integers and Floating-Point Numbers