[solved]Is it a bug ? int32_t pixels = 320*240 VS. pixels = 76800

I wrote a faster lib for ST7783 TFT controller. Here are how I clear the screen 320*240 pixels;

DJ

 // this is OK:
int32_t pixels = 76800; // 320*240 = 76800
while (pixels--) {
  writefastDATA(0,0);
}
// but this not:
int32_t pixels = 320*240; // 320*240 = 76800
while (pixels--) {
  writefastDATA(0,0);
}

Integer constants are treated as int unless specified otherwise. the highest number int can hold is 32767, then it overflows to -32,768. 78600 is greater than 32767 so it overflows through all the possible negative values of int and back up to 11264. If you don't want this to happen you will need to specify which type you want your constants to be, in this case unsigned long would be appropriate so you add UL to one of the numbers and this will automatically promote the type of the other numbers in the operation:

int32_t pixels = 320UL*240; // 320*240 = 76800

See this page for more information: https://www.arduino.cc/en/Reference/IntegerConstants By playing around with a test sketch that prints the values of various maths you can get a better feel for how this works.

That makes sense thank you pert.

DJ