unsigned类型最大值:(your type)~0
int类型最大值: (unsigned int)~0 为 unsigned int类型最大值(二进制位全为1),右移1位即为int类型最大值
int类型最小值:等于int类型最大值 + 1
static const int MAX_INT = (int) ((unsigned int)~0 >> 1);
static const int MIN_INT = (int) ((unsigned int)~0 >> 1) + 1;
printf("max %d min %d \n", MAX_INT, MIN_INT);
运算结果:max 2147483647 min -2147483648
本文探讨了C++中unsigned类型和int类型的数值范围,通过位操作展示如何获取它们的最大值和最小值。文章通过示例代码解释了(int)(unsigned int)~0右移一位得到int类型最大值,以及加1得到最小值的过程,并用printf函数打印出这些值,分别是2147483647和-2147483648。
1万+

被折叠的 条评论
为什么被折叠?



