Integer constants

From Microduino Wiki
Revision as of 03:41, 18 August 2016 by Fengfeng (talk)
Jump to: navigation, search

Integer Constant Integer constants are numbers directly used in programs, such as 123. By default, these numbers are taken as int, and you can also give more limitations with U and L(see below). Normally, int constants are decimal by default, and you can add special prefixes to transform them into other radixes.

Radix 			Example 		Format 		Remark
10(Decimal) 	         123 		        No 	
2(Binary) 	         B1111011 	        Prefix'B' 	Only apply to 8-bit value (0 to 255). Char 0-1 are valid. 
8(Octal) 	         0173 		        Prefix”0” 	Char 0-7 are valid. 
16(Hexadecimal)	 0x7B 		        Prefix”0x” 	Char 0-9, A-F, and A-F are valid 

What is a mathematical sense is that decimals are decimal numbers. If a number hasn’t a specific prefix, it is decimal by default.

Binary takes 2 as base, and only 0 and 1 are effective.

  • Example
101  //It is equal to decimal 5(1*2^2 + 0*2^1 + 1*2^0)

Binary can only be 8-bit, which is to say that it cna only represent numbers during 0-255. If inputting binary numbers is more convenient, you can take the following way:

myInt = (B11001100 * 256) + B10101010;    // B11001100 as high bit. 

Octal is based on 8, and only 0-7 are valid characters. Prefixion“0”(numerical 0)represents that the value is octal.

0101    // It is equal to the decimal number 65   ((1 * 8^2) + (0 * 8^1) + 1) 

Warning:Octal prefixion 0 is likely to produce mistakes which are difficult to find, because you may put a“0” in front of a constant by mistake, which is tragic. Hexadecimal is based on 16, and the valid characters are 0-9 and A-F. Hexadecimal numbers are represented by the prefixion “0x”. Please pay attention that A-F aren’t case-insensitive, which is to say that you can also use a-f.

  • Example
0x101   // It is equal to decimal 257   ((1 * 16^2) + (0 * 16^1) + 1)

U & L format

By default, integer constants are taken as int. If you want to transform int constants into other types, please follow the following rules:

  • u orU specify a constant as unsigned(it can only represents positive numbers and 0) , for example: 33u
  • l orL specify a constant as long int(represent a broader range of the number) , for example: 100000L
  • ul or UL , you know, is the above two types, called unsigned long int. For example:32767ul

[Return to Arduino Syntax Manual]