0
$\begingroup$

When a value $x$ is a power of $2$, that is, $x = 2^n$ for some nonnegative integer n, we can readily write $x$ in hexadecimal form by remembering that the binary representation of $x$ is simply $1$ followed by $n$ zeros. The hexadecimal digit $0$ represents $4$ binary zeros. So, for $n$ written in the form $i + 4j$ , where $0 ≤ i ≤ 3$, we can write x with a leading hex digit of 1 (i = 0), 2 (i = 1), 4 (i = 2), or 8 $(i = 3)$, followed by $j$ hexadecimal $0$s. As an example, for $x = 2,048 = 2^{11}$, we have $n = 11= 3 + 4 . 2$, giving hexadecimal representation 0x800.

I have few questions about the above statement.

Firstly, if $x = 2^n$, then why the binary representation of $x$ is simply $1$ followed by $n$ zeros? More specifically, why $1$? We can see the representation at the end is 0x800, which is $8$ followed by $2$ zeros.

Secondly, the form $i + 4j$, since the hexadecimal digit $0$ represents $4$ binary zeros, I guess there is some relation between. But I'm still not clear why.

I'm not sure if I can totally understand it if I solved these two questions. I feel mess up. I'll appreciate it if someone can expand this statement and make a better explanation.

Thank you so much!

$\endgroup$
0

1 Answer 1

2
$\begingroup$

Firstly, if x=2n, then why the binary representation of x is simply 1 followed by n zeros?

That's how positional numeral systems work. If you are writing (natural) numbers in base $b$ then the least significant digit can represent any amount between $0$ and $b-1$, then 2nd least significant digit represents one of $0, b, 2b, \dots, (b-2)b$, or $(b-1)b$, and so on so forth. In general, if the value of $i$-th least significant digit is $x_i$, then the contribution of that digit to the represented number is $x b^{i-1}$.

If there are $n$ digits $x_n, x_{n-1}, \dots, x_1$ (from the most to the least significant) then, the represented number is: $$ \sum_{i=1}^n x_i b^{i-1} $$

As a consequence, adding a $0$ in the least significant position has the effect of multiplying the represented number by $b$. Therefore, for the special case $b=2$, we have that $2^0=1$ and $2^n$ can be written (in base $2$) by starting with $1$ and multiplying it $n$ times by $2$. This amounts to appending $n$ zeros.

Secondly, the form i+4j, since the hexadecimal digit 0 represents 4 binary zeros, I guess there is some relation between. But I'm still not clear why

Since $16 = 2^4$, you can think of each hexadecimal digit as representing 4 binary digits. For example $(A7)_{16} = (1010 \; 0111)_2$ where $(1010)_2$ is represented by $(A)_{16}$ and $(0111)_2$ is represented by $(7)_{16}$.

Then, to convert a binary number into an hexadecimal number you can just just arrange the binary digits into groups of four (with suitable padding of the leading zeros) and replace each group with the corresponding hexadecimal digit.

Since $2^n$ has exactly one $1$ when written in binary (and this is the $(n+1)\mbox{-th}$ least significant digit) we know that we can form $j=\lfloor n/4 \rfloor$ groups of $4$ digits that are $(0000)_2$ (and will be converted to $(0)_{16}$), while the remaining group will have a $1$ followed by $i = n \bmod 4$ zeros (and will be converted to one of $(1)_{16}$, $(2)_{16}$, $(4)_{16}$, or $(8)_{16}$).

Equivalently, we are writing $n$ as $i + 4j$, where $i$ must be between $0$ and $3$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.