0
$\begingroup$

In logic there is so called OR operation that is quite clear to me as long as it is in the binary system. For example, if I want to OR such binary values as "101" (which corresponds to decimal "5") and "110" (which corresponds to decimal "6"), you would do it this way:

101

+

110

=

111

The logic here is quite clear: if there is at least one "1", then the result must also be "1".

However, I have no idea how this operation can be performed on hexadecimal numbers. For example, if I needed to OR such hexadecimal values as "1A" (which corresponds to decimal "26") and "1F" (which corresponds to decimal "31"), how would i do that than?

1A

+

1F

=

??

3 Answers 3

8

To expand on copper.hat’s answer just a little, observe that every hexadecimal digit corresponds to a string of four bits:

$\begin{array}{c|c} \text{Hex}&\text{Bin}\\ \hline 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 \end{array}$

Thus, any hexadecimal number converts very easily to binary: just convert the individual digits. Hex $B94A$, for instance converts to $1011\;1001\;0100\; 1010$. (That conversion is practically hard-wired, since I earned my spending money in college writing IBM 360 assembler language programs!)

  • 0
    For quick reference, you can even make a $1$6-by-$1$6 table of the results, all in hexadecimals. Well, maybe that's not quicker :P BTW, I like copper.hat's DEADBEEF!2012-08-31
4

Treat the hex number as binary. $1A_{16} = 11010_2$, $1F_{16} = 11111_2$, so the 'or' is clearly $1F_{16}$.

0

The other answers here are based on converting from hexadecimal to binary, then computing a bitwise OR. I would like to propose a different method without converting to a different base.

Where my idea comes from: (not-tested) A standard diode in electronics only allows electricity to go one way. A simple OR gate in electronics is two diodes pointing into each other. If you were to put a high voltage through one side (F) the output would be that same voltage (F). Even if you added a lower voltage (A) to the other input, the output would remain the same as the output is already at a higher voltage.

So, our current OR gates really end up being "Which Ever Is Greater" gates. That would mean your example of 1A+1F = 1F. Also, 5B+A0=AB. This is very different than a binary bitwise OR, but that is because it is a "true" hexadecimal bitwise OR.