对于一个字节来说:

unsigned char :     0  ~  255              0000 0000  ~ 1111 1111

char :-128  ~  127              -128  ~  -1     1000 0000  ~ 1111 1111                     0  ~  127     0000 0000  ~   0111 1111

(-128 的补码是1000 0000,它没有对应的原码、反码,其推导是根据 -128 + 127 = -1,即1000 0000 + 0111 1111 = 1111 1111)

计算机CPU只有加法逻辑运算器,对于减法运算,是使用两个数的补码进行加法运算。

 #include "stdafx.h"
#include <stdio.h> void print_2(unsigned char val2)
{
unsigned char *p = (unsigned char*)&val2; //从低位到高位,低端字节计算机 for (int i = ; i >= ; i--)
{
if(val2 & ( << i))
printf("");
else
printf("");
}
printf(" "); } int main(int argc, char **argv)
{
unsigned char a = ; //补码:0000 0001
unsigned char b = ; //补码:0000 0010
unsigned char c = a - b; //补码:0000 0001 + (-2)1111 1110 = (-1)1111 1111 printf("%02x,%02x,%02x\n", a, b, c);
printf("%d,%d,%d\n", a, b, c);
print_2(a);
print_2(b);
print_2(c); char d = a - b;
printf("\n%d\n", d); getchar();
}

内存中的数据结果是 1111 1111 ,如果按照unsigned char 解释即为255,按照char 解释则为 -1。

计算机补码概念剖析

unsigned char 无符号整形 减法运算的更多相关文章

  1. iOS NSArray 的count方法返回的是无符号整形!

    ){ return cell; } 这样写是错误的!!!当数组为空时,由于count方法返回的是无符号整形,没有负数,self.requests.count -1是一个非常大的正数! 正确写法: &g ...

  2. 利用Java位运算符,完成Unsigned转换(无符号)

    方案二:利用Java位运算符,完成Unsigned转换. 正常情况下,Java提供的数据类型是有符号signed类型的,可以通过位运算的方式得到它们相对应的无符号值,参见几个方法中的代码: publi ...

  3. c#中将IP地址转换成无符号整形数的方法与逆变换方法

    我们知道 IP地址就是给每个连接在Internet上的主机分配的一个32bit地址. 按照TCP/IP协议规定,IP地址用二进制来表示,每个IP地址长32bit,比特换算成字节,就是4个字节.而c#中 ...

  4. Integral Promotions整数提升和符号扩展(char,unsigned char,signed char)

    以下来自msdn: Objects of an integral type can be converted to another wider integral type (that is, a ty ...

  5. MySQL学习----unsigned 无符号的总结

    unsigned    为“无符号”的意思,          unsigned,zerofill    既为非负数,用此类型可以增加数据长度,      例如如果    int最大是65535,那  ...

  6. char类型到底是有符号还是无符号

    根据c标准,char类型到底是有符号整数类型还是无符号整数类型,这取决于c实现,也就是c编译器的作者的想法:( 那么,如何快速的编写一个检测程序,查看当前编译器如何对char进行定义? #includ ...

  7. C++ char, unsigned char, signed char

    C语言中的 char, unsigned char, signed char 一.他们是什么? signed char是有符号的,但是unsigned char没有符号,两者在存储上没有任何区别都是8 ...

  8. c语言中 char* 和 unsigned char* 的区别浅析(转)

    原文:https://blog.csdn.net/guotianqing/article/details/77341657 背景最近在项目中遇到了一个编译警告,是因为定义的变量为char[],而在使用 ...

  9. char 与 unsigned char的本质区别

    在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...

随机推荐

  1. SDUT 2603:Rescue The Princess

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  2. YTU 2925: 文件操作--文本文件读入

    2925: 文件操作--文本文件读入 时间限制: 1 Sec  内存限制: 128 MB 提交: 38  解决: 16 题目描述 现有100名学生的姓名(name).学号(num).英语(Englis ...

  3. 【转】Linus:利用二级指针删除单向链表

    原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...

  4. HDU(1572),最短路,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...

  5. IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法

    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法 IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrl ...

  6. swift + jj实践

    1,UIButton里面的字体和图片左对齐 button.imageEdgeInsets = UIEdgeInsetsMake(0,180/2,0.0,320/2) let btnRect = but ...

  7. jquery post 报404错误

    如果确定页面存在,那很可能是程序执行时报错,可改成get方式来确认一下.

  8. nl2br

    PHP中   在字符串所有新行之前插入 HTML 换行标记 说明 string nl2br ( string $string [, bool $is_xhtml = true ] ) 在字符串 str ...

  9. oracle分组统计某列逗号隔开数据

    所有版本的oracle都可以使用select wm_concat(name) as name from user; 但如果是oracle11g,使用select listagg(name, ',') ...

  10. SD卡驱动分析(二)

    三.下面分析一下高通的android2.3的代码中SD卡驱动的流程. 在kernel中,SD卡是作为平台设备加入到内核中去的,在/kernel/arch/arm/mach-msm/devices-ms ...