简体   繁体   English

8 位二进制补码范围

[英]Two's Complement Binary Range for 8-Bit

For an 8-bit cell that uses two's complement binary representation for storing SIGNED integers, what is the range of integers for this cell as written in binary?对于使用二进制补码表示来存储有符号整数的 8 位单元,该单元以二进制形式写入的整数范围是多少?

I had 00000000 to 11111111 because I thought the decimal range was 0 to -1, but I don't think I'm approaching the problem the right way.我有 00000000 到 11111111 因为我认为十进制范围是 0 到 -1,但我认为我没有以正确的方式解决问题。

8 bits can store up to 2 8 numbers. 8 位最多可存储 2 个8数字。

It depends on the system and language, but on most systems, signed integers of maximum 8 bits would store {-128,127} range, that is -128...0...127 , comprising all 256 (2 8 ) numbers.这取决于系统和语言,但在大多数系统上,最大 8 位的有符号整数将存储{-128,127}范围,即-128...0...127 ,包括所有 256 (2 8 ) 个数字。

The standard notation would have the sign bit on the left, so 10000000 would be the most negative number, and 01111111 would be the most positive number.标准符号的符号位在左侧,因此 10000000 将是最负数,而 01111111 将是最正数。

If that seems weird, try the following thought exercise, which works for any number of bits.如果这看起来很奇怪,请尝试以下思维练习,它适用于任意位数。 I'm going to use just 3 bits, to keep things brief.我将只使用 3 位,以保持简短。

            -4  -3  -2  -1   0   1   2   3   4   5   6   7 .
unsigned                    000 001 010 011 100 101 110 111
 signed     100 101 110 111 000 001 010 011

Note that (1) the eight 3-bit codes appear in the same order, it's just the "wraparound point" that differs, and (2) the positive numbers have the same bits in either representation.请注意,(1)八个 3 位代码以相同的顺序出现,只是“环绕点”不同,(2)正数在任一表示中具有相同的位。

The range for a signed 8-bit cell in decimal integers is -128 to 127.十进制整数中带符号的 8 位单元的范围是 -128 到 127。

Converting those to 2's complement binary:将它们转换为 2 的补码二进制:

 127 = 01111111
-128 = 10000000

Here is some information on 2's complement .这是有关2 的补码的一些信息。

The range for an unsigned 8-bit cell would be 00000000 to 11111111 which is 0 to 255 in decimal.无符号8 位单元的范围是 00000000 到 11111111,即十进制的 0 到 255。

For a type of width n bits and using two's complement encoding.对于宽度为n位的类型并使用二进制补码编码。

  • the largest value is 2 n-1 -1最大值为 2 n-1 -1
  • the smallest value is 2 n-1最小值为 2 n-1

So for n = 8.所以对于 n = 8。

  • 2 n-1 -1 = 128-1 = 127 largest signed value for a byte 2 n-1 -1 = 128-1 = 127 一个字节的最大有符号值
  • 2 n-1 = 128 = -128 smallest signed value for a byte 2 n-1 = 128 = -128 一个字节的最小有符号值

or using bit shifting或使用位移

1<<7 - 1 = 10000000 = 128 - 1 = 01111111  = 127
1<<7     = 10000000 = 128  =    10000000  = -128

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM