Last Updated: 23/07/2021
Lesson 6 - boolean, byte, int, long, unsigned int, unsigned long
Lessons >> Basics >> Lesson 6 - boolean, byte, int, long, unsigned int, unsigned long
Lesson Contents
Overview explaining the basic numeric variable types.
Boolean
bytes
int
long
unsigned int
unsigned
Effects of exceeding the limits of a variable type
How to select the correct variable type
Impact of selecting to large a variable type "
Example 1: NumericDataTypev1.ino
Click to Download code:NumericDataTypev1.ino
A short program to show the effects of exceeding the limits of numeric data types and how the binary values are clipped.;
/* NumericDataTypev1
bits
bytes,
integers,unsigned int
long, unsigned long
*/
//boolean is 1 bit, value of Zero or 1
boolean myBoolean;
//Byte value of 0 - 255 CANNOT BE NEGATIVE
byte myByte;
//int (short for integer) -32,768 to 32,767
int myInt;
//unsigned int 0 to 65,535 CANNOT BE NEGATIVE
unsigned int myUnsignedInt;
// long -2,147,483,648 to 2,147,483,647
long myLong;
// unsigned long 0 to 4,294,967,295 CANNOT BE NEGATIVE
unsigned long myUnsignedLong;
int q;
long w;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("NumericDataTypev1...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
Serial.println("Byte overflows at 256");
for (q = 0; q < 300; q++) {
Serial.print("q: ");
Serial.print(q);
Serial.print(" myByte: ");
Serial.print(myByte);
Serial.print(" BIN q: ");
Serial.println(myByte, BIN);
myByte++;
}
delay(3000);
//reset myByte ready for the next loop
myByte = 0;
Serial.println("unsigned int overflows at 255");
myUnsignedInt = 65449;
for (w = 65450; w < 65600; w++) {
myUnsignedInt++;
Serial.print("w: ");
Serial.print(w);
Serial.print(" myUnsignedInt: ");
Serial.print(myUnsignedInt);
Serial.print(" BIN w: ");
//prints the BINARY representation of myUnsignedInt
Serial.println(myUnsignedInt, BIN);
}
delay(3000);
Serial.println("");//blank line
Serial.println("what happens when an int overruns in a negative direction?");
//what happens when an int overruns in a negative direction?
Serial.println(" myInt = -32768 ");
myInt = -32768;
Serial.println(" myInt = myInt - 1; ");
myInt = myInt - 1; // x now contains 32,767 - rolls over in neg. direction
Serial.print(" myInt now equals: ");
Serial.println(myInt);
Serial.println("");//blank line
Serial.println("what happens when an int overruns in positive direction?");
//what happens when an int overruns in positive direction?
Serial.println(" myInt = 32767 ");
myInt = 32767;
Serial.println(" myInt = myInt + 1; ");
myInt = myInt + 1; // x now contains -32,768 - rolls over
Serial.print(" myInt now equals: ");
Serial.println(myInt);
delay(3000);
Serial.println("");//blank line
}
/* Number size calculations
* VERY important when dealing with time
*
How many seconds in a minute = 60 (byte)
How many seconds in an hours = 60 x 60 = 3600 = (Integer)
How many seconds in a day = 60 * 60 * 24 = 86400 (long)
How many seconds in a year = 60 * 60 * 24 * 365 = 31,536,000 (long)
Even for an Arduino a second is a long time, we tend to use milliseconds
How many milliseconds in a second = 1000 = integer
How many milliseconds in a minute = 60 * 1000 = 60000 = long
How many milliseconds in an hours = 60 x 60 * 1000 = 3,600,000 = long
How many milliseconds in a day = 60 * 60 * 24 * 1000 = 86,400,000 (long)
How many milliseconds in a year = 60 * 60 * 24 * 365 * 1000 = 31,536,000,000 , BIGGER THAN A LONG
Millis reaches maximum in about 50 days using an unsigned long
microseconds() 1,000,000 per second will run out of space in 70 minutes using unsigned long
*
*/
Additional Resource Links
Don't forget to use the Reference in you Arduino IDE and look at Variables for more help on this lesson.Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Lesson 6 - boolean, byte, int, long, unsigned int, unsigned long as a reference.