挖一挖unsigned int和补码
文章要讨论的是两部分:
1. 原码,反码和补码。
2. short, unsigned short, int, unsigned int, long, unsigned long的表示及转换
1. 原码,反码和补码
原码是最直观的表示方式:最高位表示符号(0表示正,1表示负),其余位表示大小。假设占位为1字节的数,原码表示的范围就是[-127 ~ 127]一共255个数字。理论上8个bit可以表示256个数,我们只能表示255个,是原码的设计让10000000和00000000都可以表示0。[1]
计算机中使用的不是原码,而是补码。这样做的原因在于:为了简化计算,计算机把1-1当作1+(-1)来做,从而只需要设计加法的实现。然而原码的表示无法让1-1和1+(-1)结果相等。反码虽然可以,但是最后的是1-1=+0,1+(-1)=-0,导致了0有两种表示方法。只有补码的设计,让1+(-1)和1-1得到了满意的一致结果(所有位数都为零,我们用它来表示补码的0)。
反码的定义:正数的反码就是其本身;负数的反码表示,是将其除了最高位,其余全部取反。因此,我们依然可以从最高位看出其正负。
补码的定义:正数的补码就是其本身;负数的补码是在其原码的基础上, 符号位不变, 其余各位取反, 最后+1(即在反码的基础上+1),0的补码表示是唯一的,就是所有位全零。
例子:
[+1] = [00000001]原 = [00000001]反 = [00000001]补
[-1] = [10000001]原 = [11111110]反 = [11111111]补
因为机器使用补码, 所以对于编程中常用到的32位int类型, 可以表示范围是: [-231, 231-1] 因为第一位表示的是符号位。而使用补码表示时又可以多保存一个最小值.
归纳起来,有几个注意点:
(1) 相同位数下,原码和反码可以表示的下限相同,补码可以表示的最小值则比他们还要小1。
以8位为例,原码和反码的下限都是-(27-1),原码的表示是11111111,反码的表示是10000000,补码的-(27-1)表示方式是10000001(反码+1),但是补码还可以用10000000表示-27。上限是正数,大家表示方法相同,因此一致。
(2) 补码表示方法中,最小值的表示方法是最高位是1,其余全为0。
2. short, unsigned short, int, unsigned int, long, unsigned long的表示和混用的结果
cpu, OS, complier都可以32位和64位之分。但是决定一种类型占的字节数的,最直接的是complier的位数。(Ultimately the compiler does, but in order for compiled code to play nicely with system libraries, most compilers match the behavior of the compiler[s] used to build the target system.[2])
常用数据类型对应字节数[3]
32位编译器:
char :1个字节
char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也就是4个字节。同理64位编译器)
short int : 2个字节
int: 4个字节
unsigned int : 4个字节
float: 4个字节
double: 8个字节
long: 4个字节
long long: 8个字节
unsigned long: 4个字节
64位编译器:
char :1个字节
char*(即指针变量): 8个字节
short int : 2个字节
int: 4个字节
unsigned int : 4个字节
float: 4个字节
double: 8个字节
long: 8个字节
long long: 8个字节
unsigned long: 8个字节
跨平台时为了避免问题,往往使用__int8, __int16,__int32,__int64。
混用的结果
比如出现:unsigned int a = 3; return a * -1; 结果会如何呢?
首先,不同类型的数在一起运算,必然会让编译器将它们划为同一类型再进行计算。这种类型间的自动转化标准,被称作Usual arithmetic conversions。下面是摘自MSDN上关于它的说明[4]:
If either operand is of type long double, the other operand is converted to type long double.
If the above condition is not met and either operand is of type double, the other operand is converted to type double.
If the above two conditions are not met and either operand is of type float, the other operand is converted to type float.
If the above three conditions are not met (none of the operands are of floating types), then integral conversions are performed on the operands as follows:
If either operand is of type unsigned long, the other operand is converted to type unsigned long.
If the above condition is not met and either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long.
If the above two conditions are not met, and either operand is of type long, the other operand is converted to type long.
If the above three conditions are not met, and either operand is of type unsigned int, the other operand is converted to type unsigned int.
If none of the above conditions are met, both operands are converted to type int.
这样,这个问题就好回答了,-1会被默认为int型,但是int和unsigned int做运算,int会被自动转化为unsigned int。
那么-1转换为unsigned int会是什么?
有了第一节中的讨论,下面的推论就非常明显:计算机中的表示方法是补码,int的字节数是4字节,因此-1在机器中是:0xFFFFFFFF。
这个时候我们将它当作unsigned int识别出来,unsigned int的特点是:最高位不作为符号位,所有位都表示值。
因此32位编译器上,unsigned int的范围是[0, 232-1],int的范围是[-231, 231-1](补码可以多表示一个最小值)
当0xFFFFFFFF的所有位都作为数值位时,其十进制表示就成了232-1,再乘以3,毫无疑问超过了32位而出现溢出,unsigned int取前32位,结果就是0xfffffffd,一个接近unsigned int上限的正整数。
这道例题来自http://blog.sina.com.cn/s/blog_4c7fa77b01000a3m.html,据说是微软面试题 :)
在上题的分析中,我们也可以发现一点:
机器中的补码总是不会变的,当我们把它们定义为不同的类型(int, unsigned)编译器将他们解读出来的值就会不同。
举个例子,例子来自[5]的节选:
unsigned b = -10;
if (b) printf("yes\n"); else printf("no\n");
int c = b;
printf("%d\n", c);
unsigned a = 10;
int d = -20;
int e = a + d;
printf("%d\n", e);
答案是:
yes
-10
-10
原因正如上面所说,传值传的是机器中的补码,总不会变(溢出除外),unsinged int和int只是定义了编译器解读它们的方式。
[5] 中还有一道题也非常有意思,这里就不转过来了,各位看官有兴趣可以移步去看看 :)
参考文章:
http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html (这篇文章写的是真好,深入浅出级别。第一部分基本上来自这篇博文)
http://stackoverflow.com/questions/13764892/what-determines-the-size-of-integer-in-c
http://www.cnblogs.com/augellis/archive/2009/09/29/1576501.html
http://msdn.microsoft.com/en-us/library/3t4w2bkb.aspx
http://www.cnblogs.com/krythur/archive/2012/10/29/2744398.html
挖一挖unsigned int和补码的更多相关文章
- 深度解析C语言int与unsigned int
就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...
- 关于unsigned int和int的加法
补码(two's complement) 在计算机系统中,数值一律用补码来表示和存储.原因在于,使用补码,可以将符号位和数值域统一处理:同时,加法和减法也可以统一处理.此外,补码与原码相互转换,其运算 ...
- 深入解剖unsigned int 和 int
就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种: 1.signed----有符号,可修饰char.int.Int ...
- 挖一挖@Bean这个东西
有Bean得治 任何一个正常程序的访问都会在内存中创建非常多的对象,对象与对象之间还会出现很多依赖关系(一个处理业务逻辑的类中几乎都会使用到别的类的实例),一般的做法都是使用new关键字来创建对象,对 ...
- unsigned int与int相加的问题-----C/C++小知识 区别
http://blog.csdn.net/thefutureisour/article/details/8147277 #include "stdafx.h" int _tmain ...
- -1>1?! unsigned int的世界不简单
编程语言提供了很多的基本数据类型,比如char,int,float,double等等.在C和C++的世界中,还有一种类型,叫做无符号数据,修饰符位unsigned,比如今天要说的unsigned in ...
- unsigned int 和 int
就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...
- 坑!坑!坑!防不胜防的unsigned int的运算
我很早之前就知道,unsigned int与int运算的时候,int会被转化为unsigned int来进行运算.一直觉得定这条规则的人是极度反人类的,虽说unsigned int可以表示更大的正值, ...
- 挖一挖C#中那些我们不常用的东西之系列(3)——StackTrace,Trim
时间太快了,三月又要过去了,告别一下...继续期待生死未卜的四月,今天我们继续挖一挖. 一: Environment.StackTrace 可能我们看到最多的就是catch中的e参数,里面会有一个St ...
随机推荐
- Ext JS 6学习文档-第7章-图表
Ext JS 6学习文档-第7章-图表 使用图表 本章中将探索在 ExtJS 中使用不同类型的图表并使用一个名为费用分析的示例项目结束本章所学.以下是将要所学的内容: 图表类型 条形图 和 柱形图 图 ...
- "Hello world!"团队第八次会议
Scrum会议 今天是我们"Hello world!"团队第八次召开会议,博客内容是: 1.会议时间 2.会议成员 3.会议地点 4.会议内容 5.todo list 6.会议照片 ...
- 2017秋-软件工程第四次作业(2)-结对使用TDD框架完成单元测试
第一次接触“单元测试”这个要求,我和队友学习了一些示例后开始操作.如下展示一些建立单元测试的过程.Step1:右键单击[解决方案]->左键单击[添加(D)]->[新建项目(N)]. Ste ...
- maven把项目打包成jar包后找不到velocity模板的bug
使用springmvc 开发时候要实现发送velcotiy模板邮件,在配置正常后,在本地测试正常后,使用maven打包成jar包后,报以下错误, Caused by: org.apache.veloc ...
- c# windows service 程序
service服务程序:可以长时间运行可执行应用程序.没有用户界面.可以自动启动和手动启动.适用于在服务器上或需要干扰其他工作的用户可以在同一台计算机上长时间的运行此功能. C#创建service服务 ...
- Java中I/O流之Object流
Java 中的 object 流:直接将 Object 对象写入或读出 1. serializable 接口:序列化,可以被序列化的,若确实需要将某个类的对象写在硬盘上或网络上,想把他们序列化成一个字 ...
- 原生js移动端可拖动进度条插件
该插件最初的想法来自网上的一篇文章,直达链接:https://www.cnblogs.com/libin-1/p/6220056.html 笔者因为业务需要寻找到这个插件,然后拿来用之,发现各种不方便 ...
- 关于Axure RP
Axure RP 是一款专业的原型设计工具 用于快速创建应用软件的线框图.流程图.原型和规格说明文档 贴一张图
- 2018 杭电多校2 - Naive Operations
题目链接 Problem Description In a galaxy far, far away, there are two integer sequence a and b of length ...
- BZOJ 2115 Xor(线性基)
题意:给定一个n<=50000个点m<=100000条边的无向联通图,每条边上有一个权值wi<=1e18.请你求一条从1到n的路径,使得路径上的边的异或和最大. 任意一条1到n的路径 ...