计算机如何区分一个整数是有符号的还是无符号的? [英] How computer distinguish an integer is signed or unsigned?

查看:31
本文介绍了计算机如何区分一个整数是有符号的还是无符号的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置两个补码是为了让计算机更容易计算两个数的减法.但是计算机如何区分一个整数是有符号整数还是无符号整数呢?它的内存中只有 0 和 1.

Two's complements is set to make it easier for computer to compute the substraction of two numbers. But how computer distinguish an integer is signed integer or unsigned integer? It's just 0 and 1 in its memory.

例如,计算机内存中的1111 1111可以代表数字255,也可以代表-1.

For exmaple, 1111 1111 in the computer memory may represent number 255 but also can represent -1.

推荐答案

有符号和无符号使用相同的数据,但指令不同.

Signed and unsigned use the same data, but different instructions.

计算机将有符号和无符号整数存储为相同的数据.IE.255 和 -1 是相同的位.但是,您告诉编译器变量的类型.如果它是有符号的,编译器会使用有符号的运算符来操作变量(例如 IDIV),如果是无符号的,则它会使用另一条指令(例如 DIV).所以编译器制作了一个程序,告诉 CPU 如何解释数据.

The computer stores signed and unsigned integers as the same data. I.e. 255 and -1 are the same bits. However, you tell the compiler what type the variable has. If it is signed, the compiler uses signed operators for manipulating the variables (e.g. IDIV) and when unsigned, it uses another instruction (e.g. DIV). So the compiler makes a program which tells the CPU how to interpret the data.

这篇关于计算机如何区分一个整数是有符号的还是无符号的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆