User Tools

Site Tools


arduino:data_types:int

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

arduino:data_types:int [2024/02/16 01:04] (current)
Line 1: Line 1:
 +======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 [[http://en.wikipedia.org/wiki/2%27s_complement | 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 [[arduino:bitwise operators:Bitshift|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:
 +
 +<code arduino>   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
 +</code>
 +
 +
 +====See Also   ====
 +
 +* [[arduino:data_types:Byte]]
 +* [[arduino:data_types:unsignedint|unsigned int]]
 +* [[arduino:data_types:Long]]
 +* [[arduino:data_types:unsignedlong|unsignedlong]]
 +* [[arduino:data_types:IntegerConstants | Integer Constants]]
 +* [[arduino:variable scope and qualifiers:VariableDeclaration|Variable Declaration]]
 +Source: arduino.cc
  

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