整数类型 [英] integer types

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

问题描述

有人可以告诉我为什么在签名的int上范围是-32768到+32768。

为什么我们还得到一个否定的。据我所知它需要两个

字节的内存。

can someone tell me why on a signed int the range is -32768 to +32768.
why do we get one more in the negative. I understand it takes two
bytes of memory.

推荐答案

Mark R Rivet发布:
Mark R Rivet posted:
有人可以告诉我为什么在签名的int上范围是-32768到+32768。
为什么我们还得到一个否定的。我知道它需要两个字节的内存。
can someone tell me why on a signed int the range is -32768 to +32768.
why do we get one more in the negative. I understand it takes two
bytes of memory.




int在不同平台上的大小不同。

让我们说这是32位。这就是它在内存中的样子:
$ b $ 00 0000 0000 0000 0000 0000 0000 0000 0000 0000


使用unsigned int,你可以使用全部32 -Bits,离开你:


2 ^ 32

可能的组合。如果您包含数字0,那么这将给您一个

范围:


0 - > (2 ^ 32) - 1


0 - > (4,294,967,296)-1


0 - > 4,294,967,295

如果您有一个有符号的int,那么其中一个位用于表示

符号。剩下31个比特来表达价值。


有了这些31位,就有:


2 ^ 31


可能的组合。


包括数字0,这将离开你:


(2 ^ 31) - 1种可能的组合,包括0.


(2 ^ 31) - 1 = 2,147,483,647

现在,要找出范围,把它除以2.


你得到:1,073,741,823.5

有你的问题,它不是一个整数!额外的1给了

正面。


-


我会进一步详细说明,这是一个8位有符号整数:

$ b $ 00 0000 0000


它的值等于0.


现在让我们做出否定:


1000 0000


0 = -0

这里,我们有两个0的值,这是浪费!因此:


1000 0000


等于-1!


并且一致:


1000 0001

等于-2 !!


希望有所帮助。 />
-JKop



int is different sizes on different platforms.
Let''s say it''s 32-Bit. Here''s what it looks like in memory:
0000 0000 0000 0000 0000 0000 0000 0000 0000

With an unsigned int , you can use all 32-Bits, that leaves you with:

2 ^ 32

possible combinations. If you include the number, 0, then that gives you a
range of:

0 -> (2 ^ 32) - 1

0 -> (4,294,967,296) -1

0 -> 4,294,967,295

If you have a signed int , then one of the bits is used to indicate the
sign. That leaves you with 31 Bits to express the value.

With those 31-Bits, there''s:

2 ^ 31

possible combinations.

Include the number, 0, and that leaves you with:

(2 ^ 31) - 1 possible combinations, including 0.

(2 ^ 31) - 1 = 2,147,483,647
Now, to find out the range, just divide that by 2.

You get: 1,073,741,823.5
There''s your problem, it''s not an integer! The extra 1 is given to the
positive side.

--

I''ll elaborate further, here''s an 8-Bit Signed Integer:

0000 0000

The value of that is equal to 0.

Now let''s make the negative:

1000 0000

0 = -0
Here, we have two values for 0, that''s a waste! Therefore:

1000 0000

is equal to -1!

And concordantly:

1000 0001

is equal to -2!!

Hope that helps.
-JKop


Mark R Rivet< ma ******** @ verizon.net>写道:
Mark R Rivet <ma********@verizon.net> writes:
有人可以告诉我为什么在有符号的int上范围是-32768到+32768。
你的意思是-32768到32767(含),我认为。

为什么我们还得到另外一个。
别忘了0.

我明白需要两个字节的内存。
can someone tell me why on a signed int the range is -32768 to +32768. You mean -32768 to 32767 inclusive, I think.
why do we get one more in the negative. Don''t forgot 0.
I understand it takes two bytes of memory.



是的。谷歌为两个的补充或者查看
http:// www2.eng.cam.ac.uk/~tpl/E6/binary.html


JKop发布:
JKop posted:
让我们说它是32位。这就是它在内存中的样子:
$ b $ 00 0000 0000 0000 0000 0000 0000 0000 0000 0000
Let''s say it''s 32-Bit. Here''s what it looks like in memory:
0000 0000 0000 0000 0000 0000 0000 0000 0000



TYPO TYPO TYPO

0000 0000 0000 0000 0000 0000 0000 0000

-JKop


TYPO TYPO TYPO

0000 0000 0000 0000 0000 0000 0000 0000

-JKop


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

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