int32存储在内存中 [英] int32 storage in memory

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

问题描述

我有一个关于int32 storage (c#)的问题.

32位表示int的最大数字是2 ^ 32.

32 bits means that the biggest number for int is 2^32.

2^32 = 4294967296,如果将其除以2,则会得到int32的最大值:

2^32 = 4294967296, if you divide it by 2 you get the maximum value for an int32 :

4294967296 / 2 = -2147483648 to 2147483648

因此,我认为一半的数字用于负数,另一半用于正数. 但这不能成立,因为2^16 = 65536.

So I thought half of the bits are for negative figures and the other for positive. But that cant be true because 2^16 = 65536.

现在我的问题:

这实际上是如何在内存中设置的?

How is this actually set up in memory?

我真的很好奇你的答案.

Im really curious about your answers.

推荐答案

仅一位用于符号(负或正)

Only one bit is used for sign (negative or positive)

Int32值用31位表示,其中30秒位表示 用作标志位.正值用表示 符号和幅度表示.负值是二 补码表示法, MSDN

Int32 values are represented in 31 bits, with the thirty-second bit used as a sign bit. Positive values are represented by using sign-and-magnitude representation. Negative values are in two's complement representation, MSDN

Int32.MaxValue =  2^31 - 1 = 01111111111111111111111111111111                
Int32.MinValue = -2^31     = 10000000000000000000000000000000

我在此处.

从Two的补码转换

以数字0xFFFFFFFF为例.用二进制表示,即:

Use the number 0xFFFFFFFF as an example. In binary, that is:

1111 1111 1111 1111 1111 1111 1111 1111

关于这个数字我们能说些什么?它的第一个(最左边)位为1,表示这表示一个负数.这就是事物取二的补码的方式:前导1表示数字为负,前导0表示数字为0或正.

What can we say about this number? It's first (leftmost) bit is 1, which means that this represents a number that is negative. That's just the way that things are in two's complement: a leading 1 means the number is negative, a leading 0 means the number is 0 or positive.

要查看此数字是否为负,请反转该数字的符号.但是该怎么做呢?要反转符号,您只需将位反转(0变为1,1变为0),然后将结果加1.

To see what this number is a negative of, we reverse the sign of this number. But how to do that? To reverse the sign you simply invert the bits (0 goes to 1, and 1 to 0) and add one to the resulting number.

该二进制数的求反显然是:

The inversion of that binary number is, obviously:

0000 0000 0000 0000 0000 0000 0000 0000

然后我们添加一个.

0000 0000 0000 0000 0000 0000 0000 0001

所以0xFFFFFFFF的负数是0x00000001,通常称为1.所以0xFFFFFFFF-1.

So the negative of 0xFFFFFFFF is 0x00000001, more commonly known as 1. So 0xFFFFFFFF is -1.

转换为二进制补码

请注意,这两种方法均有效.如果您有-30,并且想要用2的补码表示它,则可以采用30的二进制表示形式:

Note that this works both ways. If you have -30, and want to represent it in 2's complement, you take the binary representation of 30:

0000 0000 0000 0000 0000 0000 0001 1110

反转数字.

1111 1111 1111 1111 1111 1111 1110 0001

并添加一个.

1111 1111 1111 1111 1111 1111 1110 0010

转换回十六进制,这是0xFFFFFFE2

Converted back into hex, this is 0xFFFFFFE2

编辑,CPU如何使用二进制补码进行减法

CPU使用负数的二进制补码加法执行减法.让我们以8位数字为例.我们要从7中减去4.

The CPU performs the subtraction using addition on two's complement of negative number. Lets take an example of 8 bit numbers. We want to subtract 4 from seven.

7 = 00000111
4 = 00000100

2的4的补码

步骤1通过将0转换为1并将1转换为0取00000100的倒数

Two' complement of 4

Step 1 take inverse of 00000100 by converting 0 to 1 and 1 to 0

00000100 -> 11111011

第2步将一加到逆数

11111011
00000001
========
11111100

7- 4 = 7 +(4的二进制补码)

00000111 (binary representation of 7)
11111100 (binary representation after Two's complement of 4)
========
00000011  (binary representation of 3)    

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

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