文章要讨论的是两部分:

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]:

  1. If either operand is of type long double, the other operand is converted to type long double.

  2. If the above condition is not met and either operand is of type double, the other operand is converted to type double.

  3. If the above two conditions are not met and either operand is of type float, the other operand is converted to type float.

  4. 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和补码的更多相关文章

  1. 深度解析C语言int与unsigned int

    就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...

  2. 关于unsigned int和int的加法

    补码(two's complement) 在计算机系统中,数值一律用补码来表示和存储.原因在于,使用补码,可以将符号位和数值域统一处理:同时,加法和减法也可以统一处理.此外,补码与原码相互转换,其运算 ...

  3. 深入解剖unsigned int 和 int

    就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种: 1.signed----有符号,可修饰char.int.Int ...

  4. 挖一挖@Bean这个东西

    有Bean得治 任何一个正常程序的访问都会在内存中创建非常多的对象,对象与对象之间还会出现很多依赖关系(一个处理业务逻辑的类中几乎都会使用到别的类的实例),一般的做法都是使用new关键字来创建对象,对 ...

  5. unsigned int与int相加的问题-----C/C++小知识 区别

    http://blog.csdn.net/thefutureisour/article/details/8147277 #include "stdafx.h" int _tmain ...

  6. -1>1?! unsigned int的世界不简单

    编程语言提供了很多的基本数据类型,比如char,int,float,double等等.在C和C++的世界中,还有一种类型,叫做无符号数据,修饰符位unsigned,比如今天要说的unsigned in ...

  7. unsigned int 和 int

    就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...

  8. 坑!坑!坑!防不胜防的unsigned int的运算

    我很早之前就知道,unsigned int与int运算的时候,int会被转化为unsigned int来进行运算.一直觉得定这条规则的人是极度反人类的,虽说unsigned int可以表示更大的正值, ...

  9. 挖一挖C#中那些我们不常用的东西之系列(3)——StackTrace,Trim

    时间太快了,三月又要过去了,告别一下...继续期待生死未卜的四月,今天我们继续挖一挖. 一: Environment.StackTrace 可能我们看到最多的就是catch中的e参数,里面会有一个St ...

随机推荐

  1. UVa 1583 - Digit Generator 解题报告 - C语言

    1.题目大意 如果a加上a的各个数字之和得到b,则说a是b的生成元.给出n其中$1\le n\le 100000$,求其最小生成元,若没有解则输出0. 2.思路 使用打表的方法打出各个数字a对应的b, ...

  2. leetcode个人题解——#15 3sums

    class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { sor ...

  3. JQuery文本框验证

    <" CODEPAGE="936"%><!--#include file="conncon.asp"--><!--#in ...

  4. android课程第一节(TextView控件使用)

    TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...

  5. SGU 194 Reactor Cooling(无源无汇上下界可行流)

    Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidin ...

  6. 简单DP

      1.一只小蜜蜂   有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input输入数据的第一行是一个整数N,表 ...

  7. python异步初步窥探

    1.异步之难:因为其执行吮吸不可预料,当下正要发生什么事件不可预料.        程序下一步行为往往依赖上一步值执行结果,如何知晓上次异步调用已完成并获取结果,        回调成了必然选择,那又 ...

  8. Mininet实验 多个数据中心的拓扑网络实现

    实验目的 掌握多数据中心网络拓扑的构建 掌握多数据中心数据交换过程 实验原理 主机间发送消息上报给交换机,交换机对收到的报文信息进行分析判断,如果交换机中存在此消息相对应的流表,则交换机直接下发流表, ...

  9. 数字证书认证这点事, SSL/TLS,OpenSSL

    1.概念 数字证书 HTTPS请求时,Server发给浏览器的认证数据,用私钥签名,并且告诉浏览器公钥,利用公钥解密签名,确认Server身份. 证书还会指明相应的CA,CA能确认证书是否真的是CA颁 ...

  10. dedecms添加新模型

    dedecms虽然预设了一些常见网页的功能模型,但是如果需要新的功能则需要自己创建,dedecms也提供了创建新模型的功能,如下: 1.打开后台首页=>核心=>内容模型管理 2.添加新模型 ...