Minimum number of bits required to represent decimal numbers

10

I'm making a list of computer architecture and one of the first exercises is pretty basic:

  

What is the minimum amount of bits needed to represent each of the unsigned decimal numbers in binary?

     

a) 4095
  b) 65534
  c) 42319

I thought of turning the numbers into binary (dividing by two several times) to see their representation in binary and thus find the answer, but I believe there is another way of answering this without converting, but I do not remember the teacher having taught it in class.

Is there really any other way? I do not need the answer of the exercise, just the way to solve it.

    
asked by anonymous 24.04.2017 / 22:33

2 answers

12

I will teach you and not solve, as you asked.

How do you find the number of digits needed to store a decimal number?

In the decimal numerical representation we obviously have 10 different digits. So this is our basis.

The number of digits required is the exponent. So if you have a number 7 you need the first number that is a power of 10. So 10 high to 1 already gives 10, and 7 to 10. Then 1 digit is enough to represent that number.

To make it easier and not to stay guessing I can go dividing by 10 until I get to a number less than 1. Amount of divisions made that you have made is the number of digits required.

If you have 56, you need 10 to 2 to give 100 that would fit 56. Then you need two digits.

383 fits 1000, so 10 gets 3, so it needs 3 digits and so it goes.

How many different digits do you have in the binary representation? 2, right? So this will be the basis. To find out how many binary digits (bits) are needed you need to test the first exponent that matches the number you want.

Let's say you have the number 200. You making 2 to 8 gives 256, so you need 8 bits.

How do I easily discover this? I'm dividing the number by 2 successively until it reaches a smaller number than 1. So 200 needs 8 divisions (it would result in 0.78125 that does not interest us).

Let's say the number is 256. If you make 8 divisions the result gives 1. But you should only stop when you give less than 1. Then you need to make one more division. In fact the number 256 needs 9 bits to be represented binary.

If this is difficult to understand, understand the decimal. How many decimals can be represented with just one digit? 10, obvious, right? But can 10 be represented by a digit? Of course not, you need 2. Why? Because it starts from 0. then the tenth number is 9. It happens the same with binary.

If the number was 256 it would need 9 bits, since 8 bits could represent 256 numbers, from 0 to 255.

Basically that's it, now just do the math for these exercise cases.

If you have a calculator with this capability you can use logarithm with base 2. But you have a catch that if you give an exact number, you have to add 1 bit. Since the number starts at 0, if the number gives exactly the calculated power needs to add a digit. Another way is to add 1 to the number you want to calculate and then apply the logarithm. So you do not have to make an exception.

    
24.04.2017 / 22:43
10

To calculate the number of bits, you can use Log 2 (X), rounded up:

  • Log 2 (4095): 11.9996477365, that is, 12 ;
  • Log 2 (65534): 15.9999559718, that is, 16 ;
  • Log 2 (42319): 15.3690179165, that is, 16 .
24.04.2017 / 22:43