负数作为2的补码存储在内存中,CPU如何知道它是负数还是正数? [英] Negative numbers are stored as 2's complement in memory, how does the CPU know if it's negative or positive?

查看:20
本文介绍了负数作为2的补码存储在内存中,CPU如何知道它是负数还是正数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-1 可以用 4 位二进制表示为(2 的补码)1111

-1 can be represented in 4 bit binary as (2's complement) 1111

15 也表示为 1111.

15 is also represented as 1111.

那么,当 CPU 从内存中获取值时,它是如何区分 15 和 -1 的呢?

So, how does CPU differentiate between 15 and -1 when it gets values from memory?

推荐答案

当一个字节从一个地方移动到另一个地方时,CPU 并不关心它是保持 -1 还是 15.没有签名移动"之类的东西(到相同大小的位置 - 更大或更小的目的地都有签名移动).

The CPU doesn't care whether a byte holds -1 or 15 when it moves it from one place to another. There's no such thing as a "signed move" (to a location of the same size - there is a signed move for larger or smaller destinations).

CPU 仅在对字节进行算术运算时才关心表示.CPU 根据您(或代表您的编译器)选择的操作码知道是进行有符号还是无符号运算.

The CPU only cares about the representation when it does arithmetic on the byte. The CPU knows whether to do signed or unsigned arithmetic according to the op-code that you (or the compiler on your behalf) chose.

这篇关于负数作为2的补码存储在内存中,CPU如何知道它是负数还是正数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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