Main Content

Integers

Integer Classes

MATLAB® has four signed and four unsigned integer classes. Signed types enable you to work with negative integers as well as positive, but cannot represent as wide a range of numbers as the unsigned types because one bit is used to designate a positive or negative sign for the number. Unsigned types give you a wider range of numbers, but these numbers can only be zero or positive.

MATLAB supports 1-, 2-, 4-, and 8-byte storage for integer data. You can save memory and execution time for your programs if you use the smallest integer type that accommodates your data. For example, you do not need a 32-bit integer to store the value 100.

Here are the eight integer classes, the range of values you can store with each type, and the MATLAB conversion function required to create that type.

Class

Range of Values

Conversion Function

Signed 8-bit integer

–27 to 27–1

int8

Signed 16-bit integer

–215 to 215–1

int16

Signed 32-bit integer

–231 to 231–1

int32

Signed 64-bit integer

–263 to 263–1

int64

Unsigned 8-bit integer

0 to 28–1

uint8

Unsigned 16-bit integer

0 to 216–1

uint16

Unsigned 32-bit integer

0 to 232–1

uint32

Unsigned 64-bit integer

0 to 264–1

uint64

Creating Integer Data

MATLAB stores numeric data as double-precision floating point (double) by default. To store data as an integer, you need to convert from double to the desired integer type. Use one of the conversion functions shown in the table above.

For example, to store 325 as a 16-bit signed integer assigned to variable x, type

x = int16(325);

If the number being converted to an integer has a fractional part, MATLAB rounds to the nearest integer. If the fractional part is exactly 0.5, then MATLAB chooses the nearest integer whose absolute value is larger in magnitude:

x = 325.499;
int16(x)
ans =
	
  int16

   325
x = x + .001;
int16(x)
ans =

  int16

   326

If you need to round a number using a rounding scheme other than the default, MATLAB provides four rounding functions: round, fix, floor, and ceil. The fix function enables you to override the default and round towards zero when there is a nonzero fractional part:

x = 325.9;
int16(fix(x))
ans =

  int16

   325

Arithmetic operations that involve both integers and floating-point numbers always result in an integer data type. MATLAB rounds the result, when necessary, according to the default rounding algorithm. The example below yields an exact answer of 1426.75 which MATLAB then rounds to the next highest integer:

int16(325)*4.39
ans =

  int16

   1427

The integer conversion functions are also useful when converting other classes, such as character vectors, to integers:

chr = 'Hello World';
int8(chr)
ans =

  1×11 int8 row vector

    72   101   108   108   111    32    87   111   114   108   100

If you convert a NaN value to an integer class, the result is a value of 0 in that integer class. For example:

int32(NaN)
ans =

  int32

   0

Arithmetic Operations on Integer Classes

MATLAB can perform integer arithmetic on the following types of data:

  • Integers or integer arrays of the same integer data type. Arithmetic operations yield a result that has the same data type as the operands:

    x = uint32([132 347 528]) .* uint32(75);
    class(x)
    ans =
       'uint32'
  • Integers or integer arrays and scalar double-precision floating-point numbers. Arithmetic operations yield a result that has the same data type as the integer operands:

    x = uint32([132 347 528]) .* 75.49;
    class(x)
    ans =
       'uint32'

For all binary operations in which one operand is an array of integer data type (except 64-bit integers) and the other is a scalar double, MATLAB computes the operation using element-wise double-precision arithmetic, and then converts the result back to the original integer data type. For binary operations involving a 64-bit integer array and a scalar double, MATLAB computes the operation as if 80-bit extended-precision arithmetic were used, to prevent loss of precision.

Operations involving complex numbers with integer types are not supported.

Largest and Smallest Values for Integer Classes

For each integer data type, there is a largest and smallest number that you can represent with that type. The table shown under Integer Classes lists the largest and smallest values for each integer data type in the “Range of Values” column.

You can also obtain these values with the intmax and intmin functions:

intmax("int8")
ans =

  int8

   127
intmin("int8")
ans =

  int8

   -128

If you convert a number that is larger than the maximum value of an integer data type to that type, MATLAB sets it to the maximum value. Similarly, if you convert a number that is smaller than the minimum value of the integer data type, MATLAB sets it to the minimum value. For example:

x = int8(300)
x =

  int8

   127
x = int8(-300)
x =

  int8

   -128

Also, when the result of an arithmetic operation involving integers exceeds the maximum (or minimum) value of the data type, MATLAB sets it to the maximum (or minimum) value:

x = int8(100)*3
x =

  int8

   127
x = int8(-100)*3
x =

  int8

   -128

Loss of Precision Due to Conversion

When you create a numeric array of large integers (larger than flintmax), MATLAB initially represents the input as double precision by default. Precision can be lost when you convert this input to the int64 or uint64 data type. To maintain precision, call int64 or uint64 with each scalar element of the array instead.

For example, convert a numeric array of large integers to a 64-bit signed integer array by using int64. The output array loses precision.

Y_inaccurate = int64([-72057594035891654 81997179153022975])
Y_inaccurate = 1×2 int64 row vector

   -72057594035891656    81997179153022976

Instead, call int64 with each scalar element to return an accurate array.

Y_accurate = [int64(-72057594035891654) int64(81997179153022975)]
Y_accurate = 1×2 int64 row vector

   -72057594035891654    81997179153022975

You can also create the integer array without loss of precision by using the hexadecimal or binary values of the integers.

Y_accurate = [0xFF000000001F123As64 0x1234FFFFFFFFFFFs64]
Y_accurate = 1×2 int64 row vector

   -72057594035891654    81997179153022975