Report
1 month ago.

LPC1768: how to get a true 16-bit signed value?

Hey everyone, how do you get a real 16-bit signed value on this platform?

This code snippet:

int16_t test = -1;
if (test == 0xFFFF) {
    printf("Success!\n");
} else {
    printf("No match.\n");
}

prints out No match, and only prints success if test == 0xFFFFFFFF.

How can this be changed for test == 0XFFFF to be true?

Thank you!

Comment on this question

2 Answers

1 month ago.

Hello Andrique,
That's strange. However the code below works as expected:

int16_t test = -1;
int16_t ffff = 0xFFFF;

printf("Size of test = %d\r\n", sizeof(test));
printf("Size of ffff = %d\r\n", sizeof(ffff));

if (test == ffff) {
    printf("Success!\r\n");
} else {
    printf("No match.\r\n");
}

Thanks! Ran this and it does work. As Erik pointed out, int16_t ffff = 0xFFFF seems to describe a 32-bit number.

posted by Andrique Liu 22 Apr 2017
1 month ago.

I assume 0xFFFF is a 32-bit integer. Explicitly cast it to int16_t, or probably even uint16_t should do it. So if (test == (int16_t)0xFFFF).

Thanks Erik! Casting worked, as did defining a 16-bit variable to be 0xFFFF.

posted by Andrique Liu 22 Apr 2017

To post an answer, please log in.