为什么符号字节的范围是从-128到127(2的补数),而不是从-127到127? [英] Why is the range of signed byte is from -128 to 127 (2's complement) and not from -127 to 127?

查看:363
本文介绍了为什么符号字节的范围是从-128到127(2的补数),而不是从-127到127?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读
为什么字节-128到127的范围内在Java中?
它说:


  

128 10000000倒,它是01111111,并添加一个得到
  千万再次


所以它的结论是-128千万

所以+128不能重新在8比特2的补psented $ P $,但是这意味着我们可以在9位重新present,所以128是0.1亿,因此利用其2的补-128是1.1亿,

所以重新-128 10000000或其1.1亿presentation?
是重新presentaion位依赖?

为什么不干脆让较低的范围内,而不是写为-128 -127千万FOT 8位?


解决方案

  

为什么无符号字节的范围是从-128到127?


这不是。 (假设8位)的无符号字节是从0到255。

签署使用2的补字节的范围是从-128到127,从直接的 2的补

  01111111 = +127
01111110 = +126
01111101 = + 125
...
00000001 = +1
00000000 = 0
11111111 = -1
...
10000010 = -126
10000001 = -127
千万= -128


  -128 10000000或其1.1亿

所以重新presentation?


在8位,这是千万,在一个假设的9位再presentation它的 1.1亿


  

为什么不干脆让较低的范围为-127 8位?


人为限制范围-127起不到很大;你会不允许一个完全有效的值,一般使code更复杂的(否则你会做什么用的位模式千万?)。

I read Why is the range of bytes -128 to 127 in Java? it says

128 is 10000000. Inverted, it's 01111111, and adding one gets 10000000 again

so it concludes -128 is 10000000

so +128 cannot be represented in 2's complement in 8 bits, but that means we can represent it in 9 bits, so 128 is 010000000 and so taking its 2's complement -128 is 110000000,

so is representation of -128 10000000 or 110000000 ? Is the representaion bit dependent ?

Why not simply make the lower range -127 fot 8 bits instead of writing -128 as 10000000 ?

解决方案

Why is the range of unsigned byte is from -128 to 127?

It's not. An unsigned byte (assuming 8-bit) is from 0 to 255.

The range of a signed byte using 2's complement is from -128 to 127, directly from the definition of 2's complement:

01111111 = +127
01111110 = +126
01111101 = +125
...
00000001 = +1
00000000 =  0
11111111 = -1
...
10000010 = -126
10000001 = -127
10000000 = -128

so is representation of -128 10000000 or 110000000 ?

In 8-bit, it's 10000000, in a hypothetical 9-bit representation it's 110000000.

Why not simply make the lower range -127 for 8 bits?

Artificially restricting the range to -127 wouldn't achieve very much; you'd be disallowing a perfectly valid value, and generally making code more complex (what else would you do with the bit pattern 10000000?).

这篇关于为什么符号字节的范围是从-128到127(2的补数),而不是从-127到127?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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