Programming Challenge: Implementing bitwise operators.
Background: Bitwise operators are operators that perform conditional operations at the binary level. These operators are bitwise AND &
, bitwise OR |
, bitwise XOR ^
, and bitwise NOT ~
, not to be confused with their logical operator counterparts, i.e. &&
, ||
, !=
, and !
respectively.
Specifically, these operations take the binary values of the left- and right-hand terms and perform the conditional operation on each matching bit position between both values.
For instance, 3 | 4
takes the binary value 010
from 2 and 100
from 4. From left to right, we compare 0 || 1
, 1 || 0
, and 0 || 0
to get 110
as the resulting binary. This produces the integer value 6
.
Goal: Your challenge is to implement one or more of these bitwise operators without using the native operators provided to you by your language of choice. Logical operators are allowed, however. These operators should work on integer values. These operators will likely take on the form of a function or method that accepts two arguments.
Bonus challenges:
- Implement all of the operators.
- Don't use any native binary conversion utilities.
- Whether or not you implement all operators, write your code in such a way that allows for good code reusability.
- For statically typed languages, handle integers of different types (e.g. int vs. uint).
Edit: Minor correction for the sake of accuracy, courtesy of @teaearlgraycold.
It's usually represented by
!=
.That's... correct? Shit, I didn't even think of that. I stand corrected. I'll edit accordingly. Thanks!
Ha ha, yeah. I don't think they ever mentioned it in school. I was looking for the XOR logical operator one day and after far too long I realized
!=
was it.It's a sneaky one for sure. The only time a one will be marked, and is guaranteed to be marked, anywhere in the binary is when the values aren't equal. I can't believe I never realized that before.
~~ In Python I believe it's
^
~~that's the bitwise operator.
Of course. Thanks.
Once you have one, you can get the rest pretty easily:
Very concise! I'll have to take a second look at this when I've gotten some sleep and thus have the patience to really understand it. It clearly makes use of some well-defined properties that take a little more thought to wrap your head around :)
for
~
we do-(x++)
Oh hey, I actually did this at work last week. We had two one line, comma-separated strings we wanted to compare and find which words were unique. So I wrote up a quick python script to xor them.
Can you explain XOR to me?
You how when you have two light switches hooked up to the same light you can flip either one and the light will either turn on or off, even though you might be flipping the switch down to turn it on and up to turn it off? That's XOR. You have two inputs. When they're equal the output is off and when they're not equal the output is on. This means that any change to either input will result in a change to the output.
To put it another way, XOR defies the meme on reddit (and elsewhere) where someone asks if something is one way or another and the top response is just "yes". "Yes" only works for a question in the form "Is X a Y or a Z?" with an inclusive OR. Exclusive OR (XOR) does not allow both Y and Z to be true.
So inclusive (OR):
Is it a [Y or Z]?
Exclusive (XOR):
Is it a [Y] or is it a [Z]?
Correct.
Another way to think of XOR is that it's the "unrequited love" operator. That is, when one term "loves" the other but isn't loved back, the result is true.
Was taught that analogy in a class back in college :)
I actually really love the idea of posting challenges like this for programming. It's a really neat way to have fun with everything. I'd love to see more of these in the future.
I'm going to give this one a shot in the morning. We'll see how it goes!
This is the third one that I'm aware of that was posted :)
How did I miss all these? I need to go track them down.
Pro tip: click on the "challenge" tag ;)
Here's my go at it.
Edit: My solution took up a lot of screen real-estate, so I thought I would relocate it to a separate service.
The topic is very old but I think my entry is useful, its not a perfect answer because I cheated a bit because of shift operator but the main idea I want to show is that we can just use lookup tables to solve this problem, I tried the best to make it appears as a truth table in the source.
Also I just tested the input of the OP, here is the code: https://paste.debian.net/1287398/