Converting binary to hexadecimal and octal

335 Views Asked by At

I’ve noticed a grouping method when converting from binary to hexadecimal and binary to octal.

When converting from binary to octal, my math book says to group the binary numbers into groups of three, starting from the right. Also, it says to pad zeroes to the left of the last number, if the amount of binary numbers isn’t divisible by three.

When converting from binary to hexadecimal, the books tells you to do the same as one would for binary to octal. However, the book tells you to make groups of four, rather than three.

Can someone point me in the direction of where I might find the reason for this method?

I can, somewhat, understand groups of four going from binary to hexadecimal because four is a factor of sixteen, which, of course, are the number of digits in hexadecimal, but, that doesn’t hold for binary to octal, because three isn’t a factor of eight.

Any help would be appreciated. Thanks!

Happy (early) Pi Day!

enter image description here

enter image description here

2

There are 2 best solutions below

1
On BEST ANSWER

An analogy: Converting from base ten to base one thousand is very easy (and familiar). The place values are ones, thousands, millions, billions, etc. It works out very well because one thousand is an exact power of ten. Digits are a bit of a problem in base one thousand since we don't have a thousand digit symbols. But there's a simple solution: commas. So the base ten numeral $38219746081$ becomes the base thousand numeral $38,219,746,081$ (thirty-eight billions, two hundred nineteen millions, seven hundred forty-six thousands, eighty one units).

A very similar thing happens from binary to octal because eight is an exact power of two. So we could write the binary numeral $1101000111010$ as an octal numeral in the form $1,101,000,111,010$, where each grouping of three bits takes up one octal place. However, because we only need eight symbols for octal digits, we just use the familiar symbols and write this number as $15072_{\rm eight}$

Tldr: Base conversions are easy if one base is a power of the other base.

1
On

Basically, it's because $2^3=8$ and $2^4=16$.

If you write a number $n$ in binary and you get $n_2=a_ka_{k-1}\ldots a_1a_0$, what this means is that $n=a_k2^k+a_{k-1}2^{k-1}+\cdots+a_1\times2+a_0$. But $a_k2^k+a_{k-1}2^{k-1}+\cdots+a_3\times2^3$ is a multiple of $8$ and therefore, if you write $n$ in octal, the units digit is $a_2\times2^2+a_1\times2+a_0$. That is, you've group the las three digits from the binary expansion. In order to get the next digit of the octal expansion, you use the same method: you have$$a_k2^k+a_{k-1}2^{k-1}+\cdots+a_3\times2^3=(a_k2^{k-3}+a_{k-1}2^{k-4}+\cdots+a_3)\times8$$and $a_k2^{k-3}+a_{k-1}2^{k-4}+a_6\times2^3$ is a multiple of $8$ and therefore, when you write $N$ in octal, the digit to the left of the units digit is $a_5\times2^2+a_4\times2+a_3$. And so on.

In hexadecimal it's the same thing, but this time we must group them in groups of four.