n 位可以表示多少个值? [英] How many values can be represented with n bits?
问题描述
例如,如果n=9
,那么9个二进制数字(位)可以表示多少个不同的值?
For example, if n=9
, then how many different values can be represented in 9 binary digits (bits)?
我的想法是,如果我将这 9 位中的每一个都设置为 1,那么我将使这 9 位能够表示的最大数字成为可能.因此,最大值是 1 1111 1111
,它等于十进制的 511
.因此,我得出结论,9位二进制可以代表511个不同的值.
My thinking is that if I set each of those 9 bits to 1, I will make the highest number possible that those 9 digits are able to represent. Therefore, the highest value is 1 1111 1111
which equals 511
in decimal. I conclude that, therefore, 9 digits of binary can represent 511 different values.
我的思维过程正确吗?如果没有,有人可以解释一下我错过了什么吗?如何将其概括为 n
位?
Is my thought process correct? If not, could someone kindly explain what I'm missing? How can I generalize it to n
bits?
推荐答案
29 = 512 个值,因为你可以有多少个 0 和 1 的组合.
29 = 512 values, because that's how many combinations of zeroes and ones you can have.
然而,这些值代表什么取决于您使用的系统.如果它是一个无符号整数,您将拥有:
What those values represent however will depend on the system you are using. If it's an unsigned integer, you will have:
000000000 = 0 (min)
000000001 = 1
...
111111110 = 510
111111111 = 511 (max)
在二的补码中,通常用于表示二进制整数,你'会有:
In two's complement, which is commonly used to represent integers in binary, you'll have:
000000000 = 0
000000001 = 1
...
011111110 = 254
011111111 = 255 (max)
100000000 = -256 (min) <- yay integer overflow
100000001 = -255
...
111111110 = -2
111111111 = -1
通常,使用 k 位可以表示 2k 个值.它们的范围取决于您使用的系统:
In general, with k bits you can represent 2k values. Their range will depend on the system you are using:
无符号:0 到 2k-1
签名:-2k-1 到 2k-1-1
Unsigned: 0 to 2k-1
Signed: -2k-1 to 2k-1-1
这篇关于n 位可以表示多少个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!