Why does the number of bits in the binary representation of decimal number 16 == 5?
This question not probably not typical stackoverflow but am not sure where to ask this small question of mine.
Problem:
Find the number of bits in the binary representation of decimal number 16?
Now I tried to solve this one using the formula $2^n = 16 \Rightarrow n = 4$ but the correct answer as suggested by my module is 5. Could anybody explain how ?
After reading some answer,(and also I have 10 more mints before I could accept the correct answer)I think this is probably an explanation,that will be consistent to the mathematical formula,
For representing 16 we 开发者_Go百科need to represent 17 symbols (0,16), hence $2^n = 17 \Rightarrow n = 4.08746$ but as n need to be an integer then $n = 5$
Think of how binary works:
Bit 1: Add 1
Bit 2: Add 2
Bit 3: Add 4
Bit 4: Add 8
Bit 5: Add 16
Thus 16 would be: 10000
With 4 bits, you can represent numbers from 0 to 15.
So yes, you need 5 bits to represent 16.
Decimal - 16 8 4 2 1
Binary - 1 0 0 0 0
So for anything up to decimal 31 you only need 5 bits.
This is a classic fencepost error.
As you know, computers like to start counting from 0.
So to represent 16, you need bits 0, 1, 2, 3 and 4 (= floor(log2(16))).
But to actually contain bits 0 to 4, you need 5 bits.
精彩评论