This repository has been archived on 2019-12-23. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
<p>Integer constants are numbers used directly in a sketch, like <code>123</code>. By default, these numbers are treated as <aclass='wikilink'href='Int.html'>int</a>'s but you can change this with the U and L modifiers (see below).
</p>
<pclass='vspace'></p><p>Normally, integer constants are treated as base 10 (decimal) integers, but special notation (formatters) may be used to enter numbers in other bases.
</p>
<pclass='vspace'></p><pre>
Base Example Formatter Comment
10 (decimal) 123 none
2 (binary) B1111011 leading 'B' only works with 8 bit values (0 to 255)
characters 0-1 valid
8 (octal) 0173 leading "0" characters 0-7 valid
16 (hexadecimal) 0x7B leading "0x" characters 0-9, A-F, a-f valid
</pre>
<pclass='vspace'></p><p><strong>Decimal</strong> is base 10. This is the common-sense math with which you are acquainted. Constants without other prefixes are assumed to be in decimal format.
</p>
<pclass='vspace'></p>Example:<pre>
101 // same as 101 decimal ((1 * 10^2) + (0 * 10^1) + 1)
</pre>
<p><br/>
<strong>Binary</strong> is base two. Only characters 0 and 1 are valid.
</p>
<pclass='vspace'></p>Example:<pre>
B101 // same as 5 decimal ((1 * 2^2) + (0 * 2^1) + 1)
</pre>
<p>The binary formatter only works on bytes (8 bits) between 0 (B0) and 255 (B11111111). If it is convenient to input an int (16 bits) in binary form you can do it a two-step procedure such as:
</p>
<pclass='vspace'></p><pre>
myInt = (B11001100 * 256) + B10101010; // B11001100 is the high byte
</pre>
<pclass='vspace'></p><p><strong>Octal</strong> is base eight. Only characters 0 through 7 are valid. Octal values are indicated by the prefix "0"
</p>
<pclass='vspace'></p><p>Example:
</p><pre>
0101 // same as 65 decimal ((1 * 8^2) + (0 * 8^1) + 1)
</pre><divclass='indent'>Warning
</div><divclass='indent'>It is possible to generate a hard-to-find bug by (unintentionally) including a leading zero before a constant and having the compiler unintentionally interpret your constant as octal.
</div><p><br/>
<strong>Hexadecimal (or hex)</strong> is base sixteen. Valid characters are 0 through 9 and letters A through F; A has the value 10, B is 11, up to F, which is 15. Hex values are indicated by the prefix "0x". Note that A-F may be syted in upper or lower case (a-f).
</p>
<pclass='vspace'></p><p>Example:
</p><pre>
0x101 // same as 257 decimal ((1 * 16^2) + (0 * 16^1) + 1)
</pre>
<pclass='vspace'></p><h4>U & L formatters</h4>
<p>By default, an integer constant is treated as an <aclass='wikilink'href='Int.html'>int</a> with the attendant limitations in values. To specify an integer constant with another data type, follow it with:
</p>
<pclass='vspace'></p><ul><li>a 'u' or 'U' to force the constant into an unsigned data format. Example: <code>33u</code>
</li><li>a 'l' or 'L' to force the constant into a long data format. Example: <code>100000L</code>
</li><li>a 'ul' or 'UL' to force the constant into an unsigned long constant. Example: <code>32767ul</code>
<pclass='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <aclass='urllink'href='http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs'rel='nofollow'>Forum</a>.</em>
</p>
<pclass='vspace'></p><p>The text of the Arduino reference is licensed under a
<aclass='urllink'href='http://creativecommons.org/licenses/by-sa/3.0/'rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>. Code samples in the reference are released into the public domain.