answersLogoWhite

0


Best Answer

Algorithm bit_count is:

Input: an integer n, such that n >= 0

Output: the minimum number of binary digits (bits) required to represent n

let count := 0;

repeat {

n := n / 2; // integer division: ignore any remainder

count := count + 1;

} until n = 0;

return count;

This works because each binary digit represents an increasing power of 2, starting from 2^0. Thus if we repeatedly divide a value by 2 until the result is 0, the total number of divisions tells us the minimum number of bits required to represent the value.

Take the value 42 as an example:

42 / 2 = 21

21 / 2 = 10 (r 1)

10 / 2 = 5

5 / 2 = 2 (r 1)

2 / 2 = 1

1 / 2 = 0 (r 1)

That's 6 divisions in total so we need at least 6 binary digits to represent 42 in binary. Given that 42 is 101010 in binary, this is correct.

Let's try 31:

31 / 2 = 15 (r 1)

15 / 2 = 7 (r 1)

7 / 2 = 3 (r 1)

3 / 2 = 1 (r 1)

1 / 2 = 0 (r 1)

5 divisions so 5 bits. 31 in binary is 11111, so that's also correct. 11111 is also the maximum value we can represent with just 5 bits so it follows that 32 would need 6 bits:

32 / 2 = 16

16 / 2 = 8

8 / 2 = 4

4 / 2 = 2

2 / 2 = 1

1 / 2 = 0 (r 1)

QED

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the algorithm for finding the number of binary digits required to represent a positive decimal integer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How many binary bits are required to represent the decimal number 643?

Count them: 643(10)=1010000011(2)


What is the minimum number of bits required to represent the following decimal number 101000?

17 bits would allow a value up to 131071.


How many bits are required in decimal numbers in range 0 to 999 using Straight binary code and BCD code?

10 bits would be required. 10 bits long (10 digits long) can represent up to 1024.


If a decimal number has 25 digits then how many bits are required for binary representation?

If this is a homework assignment, please consider trying to answer it yourself first, otherwise the value of the reinforcement of the lesson offered by the assignment will be lost on you.The largest decimal number with 25 digits is 9,999,999,999,999,999,999,999,999.The smallest decimal number in the form 2n-1 which is greater than or equal to that is 19,342,813,113,834,066,795,298,815. That corresponds to 284-1.So, the minimum number of binary bits required to represent the decimal number 25 nines in a row is 84. This is 84 ones in a row. If you want to support negative as well as positive numbers, you will need 85.Since the largest integer in most compilers is 64 bits, this will require a special library supporting 128 bits, or an arbitrary length decimal library, if you want to manipulate such large numbers in a computer and still retain the precision of an integer.


How does one write an algorithm that rounds off a decimal point to the nearest integer?

double round (double x) return (int) x + 0.5;