Page 1 of 1

int16_t strange behaviour?

Posted: Thu May 14, 2020 7:28 am
by jenspogo
This might be a really stupid question, but I'm a little bit confused.
I use an int16_t variable in a bluepill Programm and would like to do some bit operations - but it does not work as i expect it.
when I use Serial.print() i would expect something with 4 Hex Variables, but it returns 8:

Code: Select all

int16_t c = 0x8000;
int16_t d = 0x4000;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

  // Wait for USB Serial 
  while (!Serial) {

  }
  delay(1000);

Serial.println(c,HEX);

}

void loop() {
  // put your main code here, to run repeatedly:

}
Serial Print:

Code: Select all

FFFF8000
Is there some internal Serial.print() cast?

Re: int16_t strange behaviour?

Posted: Thu May 14, 2020 7:49 am
by stas2z
Seems like println implicitely casting it to int32_t
0x8000 is -32767 and the same value will be 0xffff8000 for int32_t

Re: int16_t strange behaviour?

Posted: Thu May 14, 2020 7:49 am
by stevestrong
Which core do you use?

I just tested with my libmaple core, it prints

Code: Select all

c = -8000
d = 4000
strange why a HEX value has a sign

Re: int16_t strange behaviour?

Posted: Thu May 14, 2020 8:08 am
by stas2z
Selection_359.jpg
Selection_359.jpg (12.21 KiB) Viewed 6110 times
here is for HAL based core

Re: int16_t strange behaviour?

Posted: Thu May 14, 2020 8:11 am
by stas2z
stevestrong wrote: Thu May 14, 2020 7:49 am Which core do you use?

I just tested with my libmaple core, it prints

Code: Select all

c = -8000
d = 4000
strange why a HEX value has a sign
cuz libmaple core always adds - to negative values, hal based core only for base = 10

Re: int16_t strange behaviour?

Posted: Thu May 14, 2020 8:13 am
by fpiSTM
There is a discussion about this here:

https://groups.google.com/a/arduino.cc/ ... Qi7-axAwAJ