views:

161

answers:

7

I fear that I may be over thinking this homework question that I have for my assembly course. It reads:

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?

+7  A: 

What you're missing: Zero is a value

SamStephens
+2  A: 

Okay, since it already "leaked": You're missing zero, so the correct answer is 512 (511 is the greatest one, but it's 0 to 511, not 1 to 511).

By the way, an good followup exercise would be to generalize this:

How many different values can be represented in n binary digits (bits)?
schnaader
Thank you for explaining that I was missing the 0 value. The way you reworded the question will definitely help me in the future.
Sean
+5  A: 

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

NullUserException
You raise a very interesting point. I hadn't thought of signed and unsigned integers.
Sean
but in any case the number of *different values* is always 2^k
Nathan Reed
+1  A: 

Without wanting to give you the answer here is the logic.

You have 2 possible values in each digit. you have 9 of them.

like in base 10 where you have 10 different values by digit say you have 2 of them (which makes from 0 to 99) : 0 to 99 makes 100 numbers. if you do the calcul you have an exponential function

base^numberOfDigits:
10^2 = 100 ;
2^9 = 512
pastjean
+1  A: 

There's an easier way to think about this. Start with 1 bit. This can obviously represent 2 values (0 or 1). What happens when we add a bit? We can now represent twice as many values: the values we could represent before with a 0 appended and the values we could represent before with a 1 appended.

So the the number of values we can represent with n bits is just 2^n (2 to the power n)

dave
+1  A: 

A better way to solve it is to start small.

Let's start with 1 bit. Which can either be 1 or 0. That's 2 values, or 10 in binary.

Now 2 bits, which can either be 00, 01, 10 or 11 That's 4 values, or 100 in binary... See the pattern?

Andrew Dunn
+1  A: 

The thing you are missing is which encoding scheme is being used. There are different ways to encode binary numbers. Look into signed number representations. For 9 bits, the ranges and the amount of numbers that can be represented will differ depending on the system used.

James Kastrantas
The range of numbers that is represented will differ depending on the encoding, but the number of distinct values that can be represented stays the same no matter the encoding used
Lie Ryan
I just worked out 3-bit fixed precision on paper to check myself. The unsigned and 2's compliment representations can represent 8 values. I count 7 values for signed magnitude and 1's compliment because those systems have representations for positive and negative zero. Are positive and negative zero also counted?
James Kastrantas
@James Kastrantas: for 1's compliment, positive and negative zeros are considered as two *distinct values*. I'm not sure what you meant by "signed magnitude", can you elaborate what you meant by that?
Lie Ryan
@Lie: One could argue that `+0` and `-0` are two different _datums_ that represent the same _value_. Using that terminology, the number of datums in a sequence of k bits is always 2^k, but the number of values those datums may represent is less than or equal to 2^k.
James McNellis
@Lie Ryan: http://en.wikipedia.org/wiki/Signed_number_representations#Sign-and-magnitude_method I didn't know (or forgot...) that +0 and -0 are both counted. I'll remember that in the future. Thanks.
James Kastrantas