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

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.

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.

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

Related

How many unique conditions can be defined using only two on and off mode switches? [duplicate]

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?
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
What you're missing: Zero is a value
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?
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)?
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
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)
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.

What's the range of numbers we can represent with N bits?

For unsigned integers we can represent numbers ranging from
0 to 2N - 1
And for signed integers like two's complement the range is
-2(N-1) to 2(N-1) - 1
How is the signed range calculated?
For unsigned integers :
we use all N bits to represent numbers ranging from 0 to 2^N - 1 ,its because if we use all N bit positions and calculate different configuration by placing either 0 or 1 in each position, we can get at max integer 2^N - 1 (which is 11...upto N times) and lowest integer as 0 ( which is 00..upto N times). And, hence all values from 0 to 2^N - 1 can be represented.
For Signed integers :
Here we basically use N-1 bits to represent integer numbers and 1 bit is exclusively reserved for determining sign of that integer. So, we can represent numbers ranging from -2^(N-1) to 2^(N-1) - 1. The most significant bit as 1 represent the negative integers whereas most significant bit as 0 represent non negative integer values. Here, we can represent negative from -1 to -2^(N-1) from the fact that we may utilize number with all N bits as 1. Basically compilers use two's complement representation to represent integers.
Here's the deal. Let's do it for 2 bits, so N=2.
We get Range=-2 to 1, ie it can represent -2,-1,0,1.
Now if N=4, we have Range=-8 to +7.
I can't understand where are you stuck.

Binary and Bits

Here's a question I've come across:
Assume each X represents one bit, either 0 or 1. Consider the 8-bit unsigned binary numbers A = 1XXX XXXX and B = 0XXX XXXX. Which of the following are true (you may tick more than one answer):
A B > A
B A > 127
C Can't tell which one A or B is larger
D B < 127
E A > B
Explanations needed (0 understanding on this). Thanks!
The key to the answer is in the word unsigned. This means that the MSB (left most bit) is not being used to indicate the results sign. Processors perform mathematical operations such as add, subtract and comparison on numbers using twos compliment, this means that to know what the numeric value of a binary word is we must know if it is signed (can contain negative values) or unsigned (positive numbers only).
So in the above case the values are unsigned, which means A is always greater than B and that A has the MSB of an 8 bit value set to 1 so must be at least 128.
In the same way that we count in units of 10s binary works in units of two:
Binary
128 64 32 16 8 4 2 1
Decimal
1000 100 10 1
However if the binary value were signed the left most bit would be used to express positve (0) or negative (1) and when negative we need to invert the value and add one to get back to the (Negative) result.

Calculating the total number of possibilities in binary?

How would you calculate the total number of possibilities that binary can have in one byte?
00000000 through 11111111 = num_of_possibilities
The total number is 2 to the power of the number of bits. So, eight bits has 28 possible values.
If you really mean "how to compute it", consider that each bit has two possible values.
So one bit implies 2 values.
Two bits has one set of two values of each possible value of the other bit, so
00
01
10
11
which means a total of 4 (= 2×2) values.
Three bits gives four values twice, or 8 (=4×2) values. Four bits, 8×2; five bits, 16×2, and so on.
So eight bits is 2×2×2×2×2×2×2×2 or 256.
It is a simple question: The number of possibilities is 2n where n is the number of bits.
So for 1 byte, which is 8 bits, there are 28 possibilites, 256.
There are several methods:
2^n where n is the number of bits (2^8) Each bit has 2 possibilities.
Unsigned value of all 1's + 1 (255 + 1) Count up from 0 to max value (all ones) + zero.
Build a tree where each leaf is the sum of the values to right and left of the new value from the row above. Possibilities is sum of row having n+1 entries. (2 ( 1 + 8 + 28 + 56 ) + 70) Each value is probability of that number of bits from 0 to n.

How many values can be represented with n 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?
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
What you're missing: Zero is a value
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?
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)?
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
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)
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.