2

Subtraction has an easy method: you can literally count down until you get it.

So does addition, multiplication, division, etc.

However, does bitwise XOR have a method like this that you can do in your head (apart from converting into binary)? If so, how?

virchau13
  • 163
  • 6
  • If someone would invent an Olympics for ECEs that involved XORing two decimal numbers between 0 and 127 without paper or calculators, I would sit in the front row. –  Aug 19 '19 at 03:54

2 Answers2

3

Look to see whether $a$ and $b$ are odd or even. If the are both the same keep track of $0$. If they are different keep track of $1$.

If either $a$ or $b$ are odd subtract one; if even do nothing.

Divide $a$ and $b$ by $2$.

Repeat until done.

.....

Example $57 XOR 92$.

$57$ is odd $92$ is even: $**********1$

$(57-1)\div 2 = 28$. $92\div 2=46$. Both even: $*********01$

$14, 23$: One odd, the other even: $***********101$

$14\to 7$ and $23\to 22 \to 11$. Both odd: $*******0101$

$7\to 6\to 3$ and $11\to 10\to 5$. Both odd: $******00101$

$3\to 2\to 1$ and $5\to 4\to 2$. Different: $******100101$

$1\to 0$ and $4\to 1$. Different: $*******1100101$

$0$ adn $1\to 0$. Same and we are done:

$57 XOR 92 = 1100101$ or in decimal:

$1 + 0 + 4 +0 + 0 + 32 + 64 = 101$.

======

Ross Millikin says it might be easier to go from the higher powers of two down.

Let's see.

$57XOR 92$.

Smallest power of $2$ larger than both is $128$ and both are smaller so that's a leading $0$ (doesn't) count.

Next power is $64$ and $57 < 64 < 92$. They are on different sides. Count it. $1_2$ or $64$.

Subtract $64 $ from $92$ to get $28$.

Next power is $32$. $28 < 32 < 57$. Different sides count that. $11_2$ or $64+32 = 96$.

Subtract $32$ from $57$ to get $25$.

Next power is $16$. $16 < 25< 28$. Both on same side. Don't count it. $110_2$ or $96$.

Subtract $16$ from both to the $9$ and $12$.

Next power is $8$. $8 < 9 < 12$. Same side. Don't count. $1100_2$ or $96$.

Subtract $8$ to get $1$ and $4$

Nex power is $4$. $1< 4= 4$. Equality is the cutoff points so they are considered different sides so so Count it. $11001_2$ or $96+4 = 100$.

Subtract $4$ from $4$ to get $0$

Next power is $2$. $0<1<2$ Same. $110010_2$ or $100$.

Last power is $1$. $0< 1=1$ Different. $1100101_2$ or $100 + 1 =101$

And we are done.

Was that easier? ..... I have to admit it had its charms.

fleablood
  • 124,253
  • I find it easier to start from the top, but this does not require knowing powers of $2$ and is not limited to the powers you know. – Ross Millikan Aug 19 '19 at 03:02
  • Hmmm, you may be right. That may be easier. I've never done this so I just made this up on the spot. – fleablood Aug 19 '19 at 03:09
  • It is born of my preference for mental multiplication. I find it easier to keep track of where I am by starting at the most significant digits, and I can quit early if I have enough precision for the problem at hand. However XOR doesn't make higher bits more important than lower ones, so starting from the bottom is more reasonable. You don't need to know large powers of $2$ that way. Both approaches work. – Ross Millikan Aug 19 '19 at 03:19
0

You have to convert to binary, but it may be easier if you do it down from the top, and it will certainly be easier if you know the powers of $2$ automatically. Do you know instantly that $2^{13}=8128?$ For things up to $17$ bits you just need powers up to $2^{16}=65536$. Start from the top. Are they both or neither greater than $65536?$ If so, write $0$, else write $1$. Subtract $65536$ from whichever is greater than that and ask if either or both is greater than $32768$. Keep going.

Ross Millikan
  • 374,822