Two's complement and int8_t

The DS3231 RTC's Aging register is a single byte in two's complement format, so it allows positive and negative adjustments. It appears I can read and write this register using an int8_t variable without having to do a formal 2's complement conversion. Is that correct for AVR processors? Is it correct for any other processor I might ever use with the Arduino IDE?

int8_t is a twos complement format.

https://en.cppreference.com/w/cpp/types/integer

You have small endian and big endian processors. I do not know if all processors that can be programmed from arduino IDE are all same endian...

(cough) bytes can't be little or big endian...

1 Like

Not to nitpick, well maybe yes, but that is not quite true. Internal representation of signed numbers is implementation dependent. However it is 2's complement on 99.999% of processors. :slight_smile:

It is true. From the cppreference link:

signed integer type with width of exactly 8, 16, 32 and 64 bits respectively
with no padding bits and using 2's complement for negative values
(provided if and only if the implementation directly supports the type)

Whether a platform supports the type is implementation dependent, but if it exists, it is guaranteed to be two's complement.

I see. But the original question is whether is it safe to assume that it is implemented as 2's complement on all platforms.
I guess that makes it a purely hardware question...

It's the convenience of 2's complement that makes it so almost universal. They can be added and subtracted using unsigned arithmetic (at the hardware level). In those cases, the main (maybe only) difference between signed and unsigned arithmetic operations, is in how the status flags are set/cleared by the operation.

BTW, been there, done that, my DS3231 code treats the aging register value exactly the way that was described..

Thanks, @anon57585045. It looks like I'm going to be dealing with the DS3231M from now on, and I'm working on a way to calibrate them, and see how close to perfect timekeeping I can get them. I think they will be inherently less stable than the DS3231SN, and much more sensitive to temperature changes. Anyway I think I'll take my chances with int8_t.

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