如何被存储在存储器的整数 [英] How is an integer stored in memory?

查看:125
本文介绍了如何被存储在存储器的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是最有可能最愚蠢的问题,有人会问,但无论我希望我会找到这个明确的答案。

This is most probably the dumbest question anyone would ask, but regardless I hope I will find a clear answer for this.

我的问题是 - 如何存储在计算机内存中的一个整数

My question is - How is an integer stored in computer memory?

在C#中的整数大小为32位。 MSDN说,我们可以从-2,147,483,648到2,147,483,647存储整数变量中的数字。

In c# an integer is of size 32 bit. MSDN says we can store numbers from -2,147,483,648 to 2,147,483,647 inside an integer variable.

按我的理解有点只能存储2个值即0安培; 1.如果我只能0或1存储在一个位,我将如何能够存储2号码9位里面呢?

As per my understanding a bit can store only 2 values i.e 0 & 1. If I can store only 0 or 1 in a bit, how will I be able to store numbers 2 to 9 inside a bit?

更多precisely,说我有这个code INT X = 5 ;在内存或者换句话说psented这将如何重新$ P $如何5转化为0和1,什么是它背后的约定?

More precisely, say I have this code int x = 5; How will this be represented in memory or in other words how is 5 converted into 0's and 1's, and what is the convention behind it?

推荐答案

这是二进制(基数为2)psented重新$ P $。 了解更多关于数字基地。在基地2你只需要2个不同的符号来重新present一个数字。我们通常使用的符号 0 1 。在我们平时的基础,我们使用 10 不同的符号来重新present所有的数字, 0 1 2 ... 8 9

It's represented in binary (base 2). Read more about number bases. In base 2 you only need 2 different symbols to represent a number. We usually use the symbols 0 and 1. In our usual base we use 10 different symbols to represent all the numbers, 0, 1, 2, ... 8, and 9.

为了比较,想想一个数字,不适合在我们平时的系统。像14.我们没有一个符号1​​4,所以如何我们重新present呢?很简单,我们只是结合我们的两个符号 1 4 14 10为基手段 1 * 10 ^ 1 + 4 * 10 ^ 0

For comparison, think about a number that doesn't fit in our usual system. Like 14. We don't have a symbol for 14, so how to we represent it? Easy, we just combine two of our symbols 1 and 4. 14 in base 10 means 1*10^1 + 4*10^0.

1110 在基地2(二进制)手段 1 * 2 ^ 3 + 1 * 2 ^ 2 + 1 * 2 ^ 1 + 0 * 2 ^ 0 = 8 + 4 + 2 + 0 = 14 。因此,尽管在任一基地重新present没有足够的符号 14 用一个符号,我们仍然可以重新present它在这两个基地。

1110 in base 2 (binary) means 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0 = 8 + 4 + 2 + 0 = 14. So despite not having enough symbols in either base to represent 14 with a single symbol, we can still represent it in both bases.

在另一种常用的碱,基部16,其也被称为十六进制,我们有足够的符号,以重新present的 14 仅使用其中之一。你通常会看到 14 使用十六进制符号电子写的。

In another commonly used base, base 16, which is also known as hexadecimal, we have enough symbols to represent 14 using only one of them. You'll usually see 14 written using the symbol e in hexadecimal.

有关负整数,我们使用一个方便的重新presentation称为二进制补码这是补(均 1 取值翻转 0 和所有 0 取值翻转 1 S)与一个添加到它。

For negative integers we use a convenient representation called twos-complement which is the complement (all 1s flipped to 0 and all 0s flipped to 1s) with one added to it.

有两个主要的原因,这是太方便了:

There are two main reasons this is so convenient:


  • 我们立刻知道,如果一个数字是正负面通过观察一个位,最显著位出的 32 我们使用。

这是数学上正确在 X - Y = X + -y 使用常规此外,您在小学学过的方法相同。这意味着,处理器不需要做什么特别的实施减法,如果他们已经有了另外。他们只需找到的二进制补码(回想一下,翻转位和添加一个),然后添加 X 使用他们已有的加法电路,而不是一个特殊的电路减法。

It's mathematically correct in that x - y = x + -y using regular addition the same way you learnt in grade school. This means that processors don't need to do anything special to implement subtraction if they already have addition. They can simply find the twos-complement of y (recall, flip the bits and add one) and then add x and y using the addition circuit they already have, rather than having a special circuit for subtraction.

这篇关于如何被存储在存储器的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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