Byte vs Int

Hi, I was wondering when to use Byte and when to use Int in Arduino Programming. What are the pros and cons for them? Thank you.

‘byte’ needs one byte of memory.

‘int’ needs two bytes of memory.


Number 'type's.

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0 to 255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0 to 65535
  • unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0 to 4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
    float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. We'll touch on this later. Sparkfun.

You select the 'type' best suited for your variables.

ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select 'int'. int temperature;
  • if your variable needs to be within -64 to +64 a 'char' will do nicely. char joystick;
  • if your variable is used for ASCII then you need type 'char', char myText = {"Raspberry Pie Smells"};
  • if your variable enables some code then boolean can be used. boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, unsigned long currentTime = millis();
    etc.

And since int on a lot of platforms is 32-bit, but is usually only 16-bit on most Arduino, you might want to consider using the stdint types for clarity/consistency...

int8_t
int16_t
int32_t
uint8_t
uint16_t
uint32_t

Also a lot less verbose than typing “unsigned” everywhere.

1. byte is a keyword that is used to tell the compiler to reserve 1-memory location of 8-bit size when the number that you want to store in the said memory location has the range: 0 to 255 (0x00 - 0xFF). For example:

byte x = 23;

2. int is a keyword that is used to tell the compiler to reserve 2-memory locations of 16-bit size when the number that you want to store in the said memory locations has the range: -32768 to 32767 to 255 (0x8000 - 0x7FFF). For example:

int x = -15347;

Keep in mind that "byte" is an Arduino specific type, the generic C++ type is "unsigned char". If you want to be really explicit about the number of bits in the variable, use "uint8_t".

"int" also has different bit widths on different processor architectures (i.e. different compilers). That can bite you since Arduino uses different compilers for some boards. It's just that "int" is usually 16 bits for AVR GCC. The safe way to be portable is "uint16_t", but only if the exact number of bits matters. "int" is intended for general use, so it will handle most jobs.

Also not so much a problem, as something to note carefully, "int" type is signed, and "byte" type is unsigned. That can make a difference in some expressions.

If you want a "signed byte", use a "char". :slight_smile:

I just ran this on a Seeed Studio Xiao in the Arduino IDE:

#define DEBUG

void setup() {
#ifdef DEBUG
Serial.begin(9600);
while (!Serial) // Waste time until Serial is created
 ;
#endif

  byte myByte;
  int myInt;
  long myLong;
  float myFloat;
  double myDouble;
  
  Serial.print("  byte length = ");
  Serial.println(sizeof(myByte));
  Serial.print("   int length = ");
  Serial.println(sizeof(myInt));
  Serial.print("  long length = ");
  Serial.println(sizeof(myLong));
  Serial.print(" float length = ");
  Serial.println(sizeof(myFloat));
  Serial.print("double length = ");
  Serial.println(sizeof(myDouble));
  
}

void loop() {

}

and got the following output (length in bytes):

byte length = 1
int length = 4
long length = 4
float length = 4
double length = 8

This shows what pcbbc is talking about, since the Xaio is a 32-bit processor. Also, many processors do not support an 8-byte double.