0
$\begingroup$

I have this question or a confusion from college time. When we convert a decimal digit to binary, we divide the decimal digit with the base of binary number system. Why there is no similar method taught as hex to binary conversion. Suppose I have a number 0xAF, and its divided by 2. how can we see the remainder and quotient keeping all mathematical logic same as decimal.

$\endgroup$
  • $\begingroup$ You can do it that way, but it's not done because it's not necessary and inefficient. That's because $16=2^4$ is a power of two, so each hexadecimal digit corresponds to $4$ binary digits (whereas there's no such correspondence between decimal and binary digits). Thus you just need to know the $16$ bit patterns of the $16$ hexadecimal digits to readily convert between hexadecimal and binary. $\endgroup$ – joriki May 19 '16 at 6:24
0
$\begingroup$

First of all, you can convert from hexadecimal to binary in the exact same manner:

0xAF/2 = 0x57 , 0xAF%2 = 1
0x57/2 = 0x2B , 0x57%2 = 1
0x2B/2 = 0x15 , 0x2B%2 = 1
0x15/2 = 0x0A , 0x15%2 = 1
0x0A/2 = 0x05 , 0x0A%2 = 0
0x05/2 = 0x02 , 0x05%2 = 1
0x02/2 = 0x01 , 0x02%2 = 0
0x01/2 = 0x00 , 0x01%2 = 1

Hence $AF_{16}=10101111_{2}$.

Second, due to the fact that $16=2^4$, a much simpler conversion method is at hand.

You can simply replace each hexadecimal digit, with a sequence of $4$ binary digits.

The conversion table is given below:

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111
$\endgroup$
  • $\begingroup$ how you divided 57/2 = 2B. Using calculator I can also do the same. But I want to see how we can do it numerically. $\endgroup$ – mrigendra May 19 '16 at 8:49
  • $\begingroup$ @mrigendra: What do you mean "numerically"? You can use long division if you want, but it's not that easy when performed on a non-decimal base. You could convert first from hexadecimal to decimal, and then from decimal to binary... But again, there's a much simpler approach for this specific case. $\endgroup$ – barak manos May 19 '16 at 8:53
  • $\begingroup$ numerically means, doing with hands with explanation on paper. $\endgroup$ – mrigendra May 19 '16 at 8:55
  • $\begingroup$ @mrigendra: $AF_{16}=A\cdot16^1+F\cdot16^0=10\cdot16^1+15\cdot16^0=160+15=175$... $\lfloor175/2\rfloor=87$... $87_{10}=5\cdot16^1+7\cdot16^0=57_{16}$... $\endgroup$ – barak manos May 19 '16 at 9:03
  • $\begingroup$ I got my answer. We do not memorize hex tables as we memorize decimal tables. Thats why I got confused in hex division. $\endgroup$ – mrigendra May 19 '16 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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