无符号整型与有符号整型相运算规则

有符号数和无符号数运算的时候,有符号数会自动向无符号数转换

 1 #include<iostream>
 2 #include<ctime>
 3 #include <stdio.h>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include <map>
 7 #include <string>
 8 using namespace std;
 9 
10 #if TEST
11 int main(){
12     int a = -3;
13     unsigned int b = 2;
14     int c = a+b;
15     printf("c:%u
", c);
16     printf("c:%d
", c);
17     int x = -3;
18     unsigned int y = 2;
19     unsigned int z = x+y;
20     printf("z:%u
", z);
21     printf("z:%d
", z);
22 }
23 #endif
View Code

u c:4294967295
d c:-1
z:4294967295
z:-1

原文地址:https://www.cnblogs.com/guxuanqing/p/5851573.html