This is a homework question that I am stuck with.
Consider unsigned integer representation. How many bits will be required to store a decimal number
For a binary number of n digits the maximum decimal value it can hold will be
2^n - 1, and 2^n is the total permutations that can be generated using these many digits.
Taking a case where you only want three digits, ie your case 1. We see that the requirements is,
2^n - 1 >= 999
Applying log to both sides,
log(2^n - 1) >= log(999)
log(2^n) - log(1) >= log(999)
n = 9.964 (approx).
Taking the ceil value of n since 9.964 can't be a valid number of digits, we get n = 10.
This one works!
floor(loge(n) / loge(2)) + 1
To include negative numbers, you can add an extra bit to specify the sign.
floor(loge(abs(n)) / loge(2)) + 2
The simplest answer would be to convert the required values to binary, and see how many bits are required for that value. However, the question asks how many bits for a decimal number of X digits. In this case, it seems like you have to choose the highest value with X digits, and then convert that number to binary.
As a basic example, Let's assume we wanted to store a 1 digit base ten number, and wanted to know how many bits that would require. The largest 1 digit base ten number is 9, so we need to convert it to binary. This yields 1001, which has a total of 4 bits. This same example can be applied to a two digit number (with the max value being 99, which converts to 1100011). To solve for n digits, you probably need to solve the others and search for a pattern.
To convert values to binary, you repeatedly divide by two until you get a quotient of 0 (and all of your remainders will be 0 or 1). You then reverse the orders of your remainders to get the number in binary.
Exampe: 13 to binary.
Hope this helps out.
Keep dividing the number by 2 until you get a quotient of 0.
Assuming that the question is asking what's the minimum bits required for you to store
My approach to this question would be:
This problem can be solved this way by dividing 999 by 2 recursively. However, it's simpler to use the power of maths to help us. Essentially, we're solving n for the equation below:
2^n = 999
nlog2 = log999
n ~ 10
You'll need 10 bits to store 3 digit number.
Use similar approach to solve the other subquestions!
Hope this helps!
The largest number that can be represented by an n digit number in base b is bn - 1. Hence, the largest number that can be represented in N binary digits is 2N - 1. We need the smallest integer N such that:
2N - 1 ≥ bn - 1
⇒ 2N ≥ bn
Taking the base 2 logarithm of both sides of the last expression gives:
log2 2N ≥ log2 bn
⇒ N ≥ log2 bn
⇒ N ≥ log bn / log 2
Since we want the smallest integer N that satisfies the last relation, to find N, find log bn / log 2 and take the ceiling.
In the last expression, any base is fine for the logarithms, so long as both bases are the same. It is convenient here, since we are interested in the case where b = 10, to use base 10 logarithms taking advantage of log1010n == n.
For n = 3:
N = ⌈3 / log10 2⌉ = 10
For n = 4:
N = ⌈4 / log10 2⌉ = 14
For n = 6:
N = ⌈6 / log10 2⌉ = 20
And in general, for n decimal digits:
N = ⌈n / log10 2⌉