Given N bits, how many integers can be represented in binary?

。_饼干妹妹 提交于 2021-02-05 10:37:45

问题


Suppose you have 14 bits. How do you determine how many integers can be represented in binary from those 14 bits?

Is it simply just 2^n? So 2^14 = 16384?

Please note this part of the question: "how many INTEGERS can be represented in BINARY...". That's where my confusion lies in what otherwise seems like a fairly straightforward question. If the question was just asking how many different values or numbers can be represented from 14 bits, than yes, I'm certain it's just 2^n.


回答1:


The answer depends on whether you need signed or unsigned integers.

If you need unsigned integers then using 2^n you can represent integers from 0 to 2^n exclusive. e.g. n=2; 2^2=4 you can represent the integers from 0 to 4 exclusive (0 to 3 inclusive). Therefore with n bits, you can represent a maximum unsigned integer value of 2^n - 1, but a total count of 2^n different integers including 0.

If you need signed integers, then half of the values are negative and half of the values are positive and 1 bit is used to indicate whether the integer is positive or negative. You then calculate using using 2^n/2. e.g. n=2; 2^2/2=2 you can represent the integers from -2 to 2 exclusive (-2 to +1 inclusive). 0 is considered postive, so you get 2 negative values (-2, -1) and 2 positive values (0 and +1). Therefore with n bits, you can represent signed integers values between (-) 2^n/2 and (+) 2^n/n - 1, but you still have a total count of 2^n different integers as you did with the unsigned integers.




回答2:


Yes, it's that easy as 2^n.

A bit can have 2 distinct values: 0 and 1.

If you have 2 bits, than you have 4 distinct values: 00, 01, 10, 11. The list goes on.

Combinatorics has the simple counting formula

N = n_1 ⋅ n_2 ⋅ ... ⋅ n_k

Since n_1 = n_2 = n_k = 2 you can reduce the formula to

N = 2 ^ k 


来源:https://stackoverflow.com/questions/49470764/given-n-bits-how-many-integers-can-be-represented-in-binary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!