This really has me puzzled. Easiest to show with code and output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
uint8_t numberOfBits = 8;
Serial.print("pow(2,numberOfBits-1) = "); Serial.println (pow(2,numberOfBits-1));
Serial.print("uint8_t(pow(2,numberOfBits-1)) = "); Serial.println (uint8_t(pow(2,numberOfBits-1)));
Serial.print("uint16_t(pow(2,numberOfBits-1)) = "); Serial.println (uint16_t(pow(2,numberOfBits-1)));
Serial.print("uint32_t(pow(2,numberOfBits-1)) = "); Serial.println (uint32_t(pow(2,numberOfBits-1)));
Serial.println();
for(int i=0 ; i<3 ; i++)
{
Serial.print("i = ");Serial.print(i);Serial.print(" pow(2,numberOfBits-1-i) = "); Serial.println (pow(2,numberOfBits-1-i));
Serial.print("i = ");Serial.print(i);Serial.print(" uint8_t(pow(2,numberOfBits-1-i)) = "); Serial.println (uint8_t(pow(2,numberOfBits-1-i)));
Serial.print("i = ");Serial.print(i);Serial.print(" uint16_t(pow(2,numberOfBits-1-i)) = "); Serial.println (uint16_t(pow(2,numberOfBits-1-i)));
Serial.print("i = ");Serial.print(i);Serial.print(" uint32_t(pow(2,numberOfBits-1-i)) = "); Serial.println (uint32_t(pow(2,numberOfBits-1-i)));
Serial.println();
}
|
and the output
pow(2,numberOfBits-1) = 128.00
uint8_t(pow(2,numberOfBits-1)) = 128 // Good... casting 128.00 to different integers = 128
uint16_t(pow(2,numberOfBits-1)) = 128
uint32_t(pow(2,numberOfBits-1)) = 128
i = 0 pow(2,numberOfBits-1-i) = 128.00
i = 0 uint8_t(pow(2,numberOfBits-1-i)) = 127 //BAD... casting 128.00 to different integers = 127???
i = 0 uint16_t(pow(2,numberOfBits-1-i)) = 127
i = 0 uint32_t(pow(2,numberOfBits-1-i)) = 127
i = 1 pow(2,numberOfBits-1-i) = 64.00
i = 1 uint8_t(pow(2,numberOfBits-1-i)) = 63 // //BAD... casting 64.00 to different integers = 63???
i = 1 uint16_t(pow(2,numberOfBits-1-i)) = 63
i = 1 uint32_t(pow(2,numberOfBits-1-i)) = 63
i = 2 pow(2,numberOfBits-1-i) = 32.00
i = 2 uint8_t(pow(2,numberOfBits-1-i)) = 31 ////BAD... casting 32.00 to different integers = 31???
i = 2 uint16_t(pow(2,numberOfBits-1-i)) = 31
i = 2 uint32_t(pow(2,numberOfBits-1-i)) = 31
This is running on an Arduino Mega 2650.
Any ideas?