我想将unsigned long
的32位重新解释为signed long
。完全相同的位,仅被视为2的补码整数,而不是无符号整数。我不认为简单地将它投射到long
会做到这一点。我错了吗?
或许还有更好的方法。我使用unsigned long作为计时器。偶尔我会读取它的当前值并将其与之前的读数(两个unsigned long
s)进行比较,以查看已经过了多长时间。我需要处理一个可能的溢出,这会导致当前值比前一个值少。将这两个值解释为有符号长数并减去似乎给出正确答案。
我试过这个:
return reinterpret_cast<long>(time4) - reinterpret_cast<long>(currTimeLo); // treat unsigned as 2's complement
但只是有一个编译器错误:
Arduino: 1.6.7 (Mac OS X), Board: "Arduino Nano, ATmega328"
invalid cast from type 'long unsigned int' to type 'long int'