Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many values can be represented with n bits?

Tags:

binary

bits

For example, if n=9, then how many different values can be represented in 9 binary digits (bits)?

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.

Is my thought process correct? If not, could someone kindly explain what I'm missing? How can I generalize it to n bits?

like image 357
Sean Avatar asked Sep 28 '10 01:09

Sean


People also ask

How many more numbers can be represented with N 1 bits?

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.


2 Answers

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 

In general, with k bits you can represent 2k values. Their range will depend on the system you are using:

Unsigned: 0 to 2k-1
Signed: -2k-1 to 2k-1-1

like image 109
NullUserException Avatar answered Sep 22 '22 10:09

NullUserException


What you're missing: Zero is a value

like image 43
SamStephens Avatar answered Sep 21 '22 10:09

SamStephens