I am trying hard to understand this but can visualize it correctly. Will any one make me visualize it that if we have a byte(8 bits) why can't we convert it directly as we do in hex and octal?
Why do we can't replace binary numbers with decimal like we do in hexadecimal and octal?
633 Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 3 best solutions below
On
As long as you limit yourself to 8 bits, some kind of "direct" conversion is possible (technically, at worst via a lookup table), as in $$\begin{align}00000000&\mapsto 0\\ 00000001&\mapsto 1\\ \cdots\\ 00001001&\mapsto 9\\ 00001010&\mapsto 10\\ \cdots\\ 01011001&\mapsto 99\\ 01011010&\mapsto 100\\ \cdots\\ 11111111&\mapsto 255 \end{align} $$
However, problems come up as soon as you want to extend this in a simple way to multi-byte input: We want $$00000111\; 10100001\; 00100000\mapsto500000$$ and $$00000111\; 10100001\; 00011111\mapsto499999$$ hence before we can ouput the leading $4$ or $5$, which is mostly caused by the largeness of the number, i.e., by the most significant byte, we must consider all data down to the least significant byte!
The reason is ultimately that there is no power of $10$ that is also a power of $2$, i.e., $10^n=2^m$ has no solution except the trivial solution $n=m=0$, which translates roughly to "there is no sequence of $n$ decimal digits that corresponds uniquely to $m$ binary digits".
On the other hand, $16^1=2^4$, $8^1=2^3$, and $16^3=8^4$, so that conversion between any two of binary, octal, hexadecimal is fairly easy. In fact, as we are dealing with bytes, I should rather say that $16^2=256^1$, $8^8=256^3$, $2^8=256^1$ imply that conversion from bytes (=base $256$) to hex, octal, binary and back is fairly easy. The highest complexity involved here is that octals require you to work with gorups of three bytes, grouping startng at the LSB.
On
Here's another approach to look at this:
Background: the fact that hexadecimal is $16=2^4$ and octal $8=2^3$ allows us to easily group digits together. You might already be familiar with such a grouping of three bits:
$$(\color{blue}{110}\color{red}{001}\color{green}{101})_2=(\color{blue}{6}\color{red}{1}\color{green}{5})_8$$
or the grouping of four bits:
$$(\color{blue}{1101}\color{red}{1001}\color{green}{0101})_2=(\color{blue}{A}\color{red}{9}\color{green}{5})_{16}$$
Your question: essentially boils down to why does this grouping work? Once you know why it works, you'll automatically understand why it doesn't work in base 10.
Look at this proof for conversion to octal. Consider a arbitrary binary number $m=(b_{n-1}...b_2b_1b_0)_2$, such that there are $n$ bits and $n=3k$. We can rewrite it in decimal as:
$$m=\color{blue}{2^{n-1}b_{n-1}+2^{n-2}b_{n-2}+2^{n-3}b_{n-3}+}$$ $$\color{green}{2^{n-4}b_{n-4}+2^{n-5}b_{n-5}+2^{n-6}b_{n-6}+}$$ $$...+$$ $$\color{violet}{2^{2}b_{2}+2^{1}b_{1}+2^{0}b_{0}}$$
Notice that this can be regrouped as:
$$m=2^{n-3}\cdot(\color{blue}{2^{2}b_{n-1}+2^{1}b_{n-2}+2^0b_{n-3})+}$$ $$2^{n-6}\cdot(\color{green}{2^{2}b_{n-4}+2^{1}b_{n-5}+2^{0}b_{n-6})+}$$ $$...+$$ $$2^0\cdot(\color{violet}{2^{2}b_{2}+2^{1}b_{1}+2^{0}b_{0})}$$
putting $n=3k$, you get:
$$m=8^{k-1}\cdot(\color{blue}{2^{2}b_{n-1}+2^{1}b_{n-2}+2^0b_{n-3})+}$$ $$8^{k-2}\cdot(\color{green}{2^{2}b_{n-4}+2^{1}b_{n-5}+2^{0}b_{n-6})+}$$ $$...+$$ $$8^0\cdot(\color{violet}{2^{2}b_{2}+2^{1}b_{1}+2^{0}b_{0})}$$
Magic! Notice how:
- the powers of $8$ scale down from $k-1$ to $0$, as if there are $k$ bits in our octal number!
- notice how each colored expression ($2^2\cdot b_i+2^1\cdot b_{i-1}+2^0\cdot b_{i-2}$) is actually a single bit in octal system!
- this can be rewritten into an octal number now!
Similarly, you can proceed for hexadecimal as well. Here, the important step was taking the highest power of $2$ common from each triad we formed. The very fact that we formed groups of three, and, our final base turned out to be octal, is not a coincidence. It was only possible because $8$ is the third power of $2$.
Since 10 is not an integral power of $2$, you cannot group an integral number of bits together in that step. Hence, this grouping method of base interconversion will never work for base 10. Proved.
Hope it helps!
Converting between binary and octal: $$ 1_2\leftrightarrow 1_8\\ 10_2\leftrightarrow 2_8\\ 100_2\leftrightarrow 4_8\\ 1000_2\leftrightarrow 10_8\\ 10000_2\leftrightarrow 20_8\\ 100000_2\leftrightarrow 40_8 $$ See how a single non-zero bit corresponds to just a single non-zero octit? Now, let's do the same for decimal: $$ 1_2\leftrightarrow 1_{10}\\ 10_2\leftrightarrow 2_{10}\\ 100_2\leftrightarrow 4_{10}\\ 1000_2\leftrightarrow 8_{10}\\ 10000_2\leftrightarrow 16_{10}\\ 100000_2\leftrightarrow 32_{10} $$ See how a single bit spreads out over more than one digit?
This is what makes converting between binary, octal and hexadecimal easy, while decimal is incompatible with any of them.
Yes, between octal and hexadecimal it does spread out a bit, but groups of four octits corresponds to groups of three hexits, so one octit influences at most three hexits and one hexit influences at most four octits.
For decimal there is no such limitation. A single digit can influence any number of bits, octits or hexits, and vice versa. Thus you can't do a "part-by-part" simple conversion between decimal and any of the others.