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
Count them: 643(10)=1010000011(2)
17 bits would allow a value up to 131071.
10 bits would be required. 10 bits long (10 digits long) can represent up to 1024.
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.
double round (double x) return (int) x + 0.5;
9 bits
103
8
how many bits are needed to represent decimal values ranging from 0 to 12,500?
5 digits will suffice.
Count them: 643(10)=1010000011(2)
Seven will be more than enough.
10 digits.
A decimal can be a positive or negative number.
You just divide like you would divide any other decimal, and then... Positive and Positive = Positive Negative and Negative = Positive Positive and Negative = Negative I hope my answer helped! :)
A decimal is not an integer. However a number with a decimal component can be either positive or negative
17 bits would allow a value up to 131071.