int16_t strange behaviour?

Post here first, or if you can't find a relevant section!
Post Reply
jenspogo
Posts: 29
Joined: Mon Feb 24, 2020 10:42 am

int16_t strange behaviour?

Post 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?
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: int16_t strange behaviour?

Post by stas2z »

Seems like println implicitely casting it to int32_t
0x8000 is -32767 and the same value will be 0xffff8000 for int32_t
Last edited by stas2z on Thu May 14, 2020 7:49 am, edited 1 time in total.
stevestrong
Posts: 430
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: int16_t strange behaviour?

Post 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
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: int16_t strange behaviour?

Post by stas2z »

Selection_359.jpg
Selection_359.jpg (12.21 KiB) Viewed 1456 times
here is for HAL based core
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: int16_t strange behaviour?

Post 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
User avatar
fpiSTM
Posts: 912
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 36
Location: Le Mans
Contact:

Re: int16_t strange behaviour?

Post by fpiSTM »

There is a discussion about this here:

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

Return to “General discussion”