SOLVED: SEEED XAIO not handling HEX 2's complement

When using a SEEED XAIO SAMD21, the "2's complement" of a 16-bit integer (i.e., negative number) is not working correctly. The sample code below works for a 32-bit long, but only works properly for a 16-bit integer on a NANO, but not the XAIO. I suspect the XIAO is not handling integers correctly. Ultimately I need to send negative numbers over I2C thus the desire to use 2's complement.

long longData = 0xFFFFFFFF; // intention is this means -1
Serial.println(longData);   // prints -1 on XIAO and NANO; Good!

int intData = 0xFFFF;       // intention is this means -1
Serial.println(intData);    // prints -1 on NANO, but 65535 on XAIO; Bad!

The fact that the XAIO prints 65535 for the integer example above is problematic since integers are defined as -32768 to 32767. I also suspect (maybe?) I should explicitly include a library that makes the XAIO handle integers like the NANO?

Check the size of int on the XIAO.

Indeed, why are you assuming an int on the XIAO is 2 bytes ????

Try this;

int intData = 0x10000;     // this is 65536
Serial.println(intData);    //  prints ?????

If you want to be sure what sizes of variables you are dealing with do this;


int16_t intData = 0xFFFF;   // intention is this means -1
Serial.println(intData);    // prints -1 on NANO, and -1 on XAIO; Good!

Yes, on XIAO int is 4 bytes, which explains its behavior. Searching now for XAIO unsigned 2 byte datatype.

See above, add a u.

SOLVED. Learn something new everyday; declare 16-bit per above.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.