User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







arduino:data_types:int

int

Description

Integers are your primary data-type for number storage.

On the Arduino Uno (and other ATMega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).

On the Arduino Due, an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).

int's store negative numbers with a technique called 2's complement math. The highest bit, sometimes referred to as the “sign” bit, flags the number as a negative number. The rest of the bits are inverted and 1 is added.

The Arduino takes care of dealing with negative numbers for you, so that arithmetic operations work transparently in the expected manner. There can be an unexpected complication in dealing with the bitshift right operator (>>) however.

Example

  [=int ledPin = 13;=]

Syntax

  [=int var = val;=]

*var - your int variable name *val - the value you assign to that variable

Coding Tip

When variables are made to exceed their maximum capacity they “roll over” back to their minimum capacity, note that this happens in both directions. Example for a 16-bit int:

   int x;
   x = -32768;
   x = x - 1;       // x now contains 32,767 - rolls over in neg. direction
 
   x = 32767;
   x = x + 1;       // x now contains -32,768 - rolls over

See Also

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
arduino/data_types/int.txt · Last modified: 2024/02/16 01:04 (external edit)

Impressum Datenschutz