Clarphimous
2008-10-23, 15:54
Okay, I've been wanting to do bitwise operations on my graphing calculator in just a single line, but I don't know how. I know a couple are possible to do but I'm not sure of the others.
If you don't know what I'm talking about, a bitwise operation just divides up a number into binary digits and applies a certain boolean expression to each individual bit. You can try it on the Windows calc.exe program in scientific mode.
Say you want to find
31 AND 55
First you convert them into binary.
11111 AND 1101111
Then you apply the AND operation to each set of bits. Where both are 1, you get an output of 1. If either bit is zero, you get zero as the result.
011111 = 31
110111 = 55
======
010111 = 23
Now, certain ones I can do in a single mathematical expressioin, such as left shift, right shift, and not. For not, you simply take the number of bits you're doing it for, say N, then find 2^(N)-1 which will give you a string of 1's in binary. Then subtract your number from that and you've applied not to it.
Say we want to find not(312) with 10 bits
2^(10)-1 = 1023 = 1111111111
312 = 100111000
1023
-312
====
711
or, in binary
not(0100111000) = 1011000111 = 711
For shifting bits to the left or the right, you just multiply or divide by 2 and knock off the remainder if shifting to the right.
But for stuff like AND, OR, and XOR I haven't found an easy way to do it yet. Is there such a way?
If you don't know what I'm talking about, a bitwise operation just divides up a number into binary digits and applies a certain boolean expression to each individual bit. You can try it on the Windows calc.exe program in scientific mode.
Say you want to find
31 AND 55
First you convert them into binary.
11111 AND 1101111
Then you apply the AND operation to each set of bits. Where both are 1, you get an output of 1. If either bit is zero, you get zero as the result.
011111 = 31
110111 = 55
======
010111 = 23
Now, certain ones I can do in a single mathematical expressioin, such as left shift, right shift, and not. For not, you simply take the number of bits you're doing it for, say N, then find 2^(N)-1 which will give you a string of 1's in binary. Then subtract your number from that and you've applied not to it.
Say we want to find not(312) with 10 bits
2^(10)-1 = 1023 = 1111111111
312 = 100111000
1023
-312
====
711
or, in binary
not(0100111000) = 1011000111 = 711
For shifting bits to the left or the right, you just multiply or divide by 2 and knock off the remainder if shifting to the right.
But for stuff like AND, OR, and XOR I haven't found an easy way to do it yet. Is there such a way?