Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm asking in the spirit of these two questions: can bitwise operations (AND, OR, XOR) be expressed in terms of other (more familiar?) functions?

I had been playing around with the bitwise operations in Mathematica a while back, and was at first struck by this identity for bitwise NOT: ~ n == -1 - n (I'll use C-ish notation/syntax for this question, but I'm using Mathematica's definitions, which assume a two's complement representation for negative integers.)

Try as I might, I have not managed to figure out alternative ways of expressing the other bitwise operations. I have also tried Using The Fabulous Search Engine to see if other people have looked into this, but I probably am not using the right keywords.

I have noticed that the implementation of the bitwise operators in Mathematica satisfies de Morgan's theorem:

i | j == -1 - ((-1 - i) & (-1 - j))

i & j == -1 - ((-1 - i) | (-1 - j))

and the following identity for bitwise XOR is satisfied as well:

i ^ j == (i | j) & (-1 - (i & j))

I suppose then that any alternate expression for the bitwise operators would have to satisfy these identities as well.

(I am not intending to replace the bitwise expressions in actual programming, of course; they are already optimized, and there is really no practical reason to replace them with "more mathematical" expressions. I am asking merely for curiosity's sake.)

References and sundry information will be appreciated.

share|improve this question
    
I don't know of any other more appropriate tags; please retag if you can think of anything more appropriate. –  J. M. Dec 22 '10 at 3:56
    
Would a polynomial interpolation, e.g. of the bitwise operations between two bytes, be pertinent? –  hardmath Dec 22 '10 at 4:19
2  
@J.M: I was also inquisitive about this specific thing,here is an nice discussion (that rested my confusion):answers.google.com/answers/threadview/id/389777.html –  Quixotic Dec 22 '10 at 6:29
1  
@Deb: The discussion there seems to be for fractions; here, I am considering bitwise operations on integers. –  J. M. Dec 22 '10 at 6:31
1  
@Debanjan: Thanks for posting that Google Answers link. It's a write up I did... so many years ago that I would not have recalled doing so! –  hardmath Dec 22 '10 at 13:32
show 5 more comments

2 Answers 2

Exclusive OR is addition in $\mathbb{Z}_2^n$. The AND operation corresponds to set intersection, OR to set union, NOT to complementation (and XOR to symmetric difference).

share|improve this answer
    
I may have not expressed myself clearly; I mentioned that bitwise NOT can be re-expressed as $-1-n$ ; are there similar "identities" for the other bitwise operations? –  J. M. Dec 22 '10 at 5:24
add comment

If you are asking for a simple mathematical identity in addition, subtraction, multiplication and division, then I am afraid that I do not know of any other than those already mentioned. Since you mentioned, however, that you were asking "merely for curiosity's sake", I will attempt to satisfy at least some of that curiosity.

Since bitwise operations are, after all, iterative, there exists for all bitwise operations a sequential sum that, for each given input, returns an identical output to that of the bitwise operation. Unfortunately, these are computationally incredibly expensive, but will serve all the same as a purely mathematical solution.

The mathematical equivalents to right- and left-shifts are already widely known, so I will not repeat them here. The simplest of the sequential sums that I could find, following these, was that of a bitwise NOT. While you have already stated that it can be expressed as $-1 - n$, this is more of a hack exploiting the fact that $-1$ (in 32-bit) is equal to 0xFFFFFFFF (hexadecimal, continuing your precedent of c-style notation). NOT, mathematically, can be expressed as follows: $$ not(a)=\sum_{n=1}^{\left \lfloor \log_{2}(a) \right \rfloor} \left [ n \left ( 1 - \left ( \left \lfloor \frac{a}{2^{n}} \right \rfloor mod 2 \right ) \right ) \right ] $$

I will now move fairly quickly through the remaining bitwise operations.

OR: $$ or(a,b)=\sum_{n=1}^{\left \lfloor \log_{2}(a) \right \rfloor} \left [ n \left \lceil \frac{ \left ( \left \lfloor \frac{a}{2^{n}} \right \rfloor mod 2 \right ) + \left ( \left \lfloor \frac{b}{2^{n}} \right \rfloor mod 2 \right ) }{2} \right \rceil \right ] + b - b mod 2^{\left \lfloor \log_{2}(a) \right \rfloor} $$

AND (using the floor of an addition): $$ and1(a,b)=\sum_{n=1}^{\left \lfloor \log_{2}(a) \right \rfloor} \left [ n \left \lfloor \frac{ \left ( \left \lfloor \frac{a}{2^{n}} \right \rfloor mod 2 \right ) + \left ( \left \lfloor \frac{b}{2^{n}} \right \rfloor mod 2 \right ) }{2} \right \rfloor \right ] $$

AND (using multiplication): $$ and2(a,b)=\sum_{n=1}^{\left \lfloor \log_{2}(a) \right \rfloor} \left [ n \left ( \left \lfloor \frac{a}{2^{n}} \right \rfloor mod 2 \right ) \left ( \left \lfloor \frac{b}{2^{n}} \right \rfloor mod 2 \right ) \right ] $$

XOR: $$ xor(a,b)=\sum_{n=1}^{\left \lfloor \log_{2}(a) \right \rfloor} \left [ n \left ( \left ( \left ( \left \lfloor \frac{a}{2^{n}} \right \rfloor mod 2 \right ) + \left ( \left \lfloor \frac{b}{2^{n}} \right \rfloor mod 2 \right ) \right ) mod 2 \right ) \right ] + b - b mod 2^{\left \lfloor \log_{2}(a) \right \rfloor} $$

I am aware of the fact that this thread has not seen any activity in a while, but I myself spent some time looking (unsuccessfully) on the internet for this before deciding to work it out myself. Since I did work this out by hand, however, there may be errors in the formulas. If anyone finds any, please let me know of them, and I will attempt to fix them.

Note that I could not find any mathematical way of performing these operations without the use of modulus, floors, ceilings and the sequential additions. If anyone finds any alternative methods of solving these, I would be interested in hearing of them (hypothetically, substitution of the equation for triangular numbers into the sequential sums may be able to simplify them).

Also note that, programatically, the floor and ceiling functions can be replaced by integer operations.

Hopefully somebody, sometime, will find a use for these equations.

In case anybody is wondering, the reason why I put these together was to discover if it is possible to apply calculus to bitwise operations. It may or may not be, depending on whether or not sequential sums, floors, ceilings and modulo operations can be used in calculus.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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