#include <stdint.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{
uint64_t a = 10;
uint64_t b = 13;
int64_t c = a - b;
uint64_t d = a - b;
printf("c=%ld, d=%lx, c(lx)=%lx\n", c, d, c);
return 0;
}
这段代码的输出如下:
c=-3, d=fffffffffffffffd, c(lx)=fffffffffffffffd
不禁思考:unsigned、signed 对于加减法指令来说有意义吗?在寄存器面前,正负平等?
再看个例子:
#include <stdint.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{
int64_t e = 0xfffffffffffffffd;
printf("e=%lx, e(u)=%ld, e+e=%ld\n", e, e, e+e);
// test overflow
uint64_t f = 0xfffffffffffffffd;
printf("f=%lx, f(u)=%ld, f+f=%ld, f+f=%lu(lu)\n", f, f, f+f, f+f);
return 0;
}
执行结果:
e=fffffffffffffffd, e(u)=-3, e+e=-6
f=fffffffffffffffd, f(u)=-3, f+f=-6, f+f=fffffffffffffffa(lu)
可以看到,在加减乘除这个事情上,正负计算对于寄存器来说还真没有任何区别!它是不管溢出的。