Expressing bitwise operations in terms of other functions
$begingroup$
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.
functions
$endgroup$
|
show 5 more comments
$begingroup$
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.
functions
$endgroup$
2
$begingroup$
@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
$endgroup$
– Quixotic
Dec 22 '10 at 6:29
2
$begingroup$
@Deb: The discussion there seems to be for fractions; here, I am considering bitwise operations on integers.
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 6:31
1
$begingroup$
@J.M.: Note that the formula -1-n for bitwise NOT(n) involves an identity of machine representations rather than of integers, inasmuch as the machine representation of "-1" in twos-complement is the integer $2^w-1$ for suitable word size w. Hence that formula relies on a fixed word size and limited range of values.
$endgroup$
– hardmath
Dec 22 '10 at 12:15
1
$begingroup$
@hardmath: I'm aware that such limitations exist in C and other such languages; I got curious on how Mathematica did it with this snippet: "Bitwise operations in Mathematica in effect allow integers to have an unlimited number of digits. When an integer is negative, it is taken to be represented in two's complement form, with an infinite sequence of ones on the left. This allowsBitNot[n]to be equivalent simply to $-1-n$"
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 12:21
1
$begingroup$
@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!
$endgroup$
– hardmath
Dec 22 '10 at 13:32
|
show 5 more comments
$begingroup$
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.
functions
$endgroup$
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.
functions
functions
edited Apr 13 '17 at 12:20
Community♦
1
1
asked Dec 22 '10 at 3:55
J. M. is not a mathematicianJ. M. is not a mathematician
61.3k5152290
61.3k5152290
2
$begingroup$
@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
$endgroup$
– Quixotic
Dec 22 '10 at 6:29
2
$begingroup$
@Deb: The discussion there seems to be for fractions; here, I am considering bitwise operations on integers.
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 6:31
1
$begingroup$
@J.M.: Note that the formula -1-n for bitwise NOT(n) involves an identity of machine representations rather than of integers, inasmuch as the machine representation of "-1" in twos-complement is the integer $2^w-1$ for suitable word size w. Hence that formula relies on a fixed word size and limited range of values.
$endgroup$
– hardmath
Dec 22 '10 at 12:15
1
$begingroup$
@hardmath: I'm aware that such limitations exist in C and other such languages; I got curious on how Mathematica did it with this snippet: "Bitwise operations in Mathematica in effect allow integers to have an unlimited number of digits. When an integer is negative, it is taken to be represented in two's complement form, with an infinite sequence of ones on the left. This allowsBitNot[n]to be equivalent simply to $-1-n$"
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 12:21
1
$begingroup$
@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!
$endgroup$
– hardmath
Dec 22 '10 at 13:32
|
show 5 more comments
2
$begingroup$
@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
$endgroup$
– Quixotic
Dec 22 '10 at 6:29
2
$begingroup$
@Deb: The discussion there seems to be for fractions; here, I am considering bitwise operations on integers.
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 6:31
1
$begingroup$
@J.M.: Note that the formula -1-n for bitwise NOT(n) involves an identity of machine representations rather than of integers, inasmuch as the machine representation of "-1" in twos-complement is the integer $2^w-1$ for suitable word size w. Hence that formula relies on a fixed word size and limited range of values.
$endgroup$
– hardmath
Dec 22 '10 at 12:15
1
$begingroup$
@hardmath: I'm aware that such limitations exist in C and other such languages; I got curious on how Mathematica did it with this snippet: "Bitwise operations in Mathematica in effect allow integers to have an unlimited number of digits. When an integer is negative, it is taken to be represented in two's complement form, with an infinite sequence of ones on the left. This allowsBitNot[n]to be equivalent simply to $-1-n$"
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 12:21
1
$begingroup$
@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!
$endgroup$
– hardmath
Dec 22 '10 at 13:32
2
2
$begingroup$
@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
$endgroup$
– Quixotic
Dec 22 '10 at 6:29
$begingroup$
@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
$endgroup$
– Quixotic
Dec 22 '10 at 6:29
2
2
$begingroup$
@Deb: The discussion there seems to be for fractions; here, I am considering bitwise operations on integers.
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 6:31
$begingroup$
@Deb: The discussion there seems to be for fractions; here, I am considering bitwise operations on integers.
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 6:31
1
1
$begingroup$
@J.M.: Note that the formula -1-n for bitwise NOT(n) involves an identity of machine representations rather than of integers, inasmuch as the machine representation of "-1" in twos-complement is the integer $2^w-1$ for suitable word size w. Hence that formula relies on a fixed word size and limited range of values.
$endgroup$
– hardmath
Dec 22 '10 at 12:15
$begingroup$
@J.M.: Note that the formula -1-n for bitwise NOT(n) involves an identity of machine representations rather than of integers, inasmuch as the machine representation of "-1" in twos-complement is the integer $2^w-1$ for suitable word size w. Hence that formula relies on a fixed word size and limited range of values.
$endgroup$
– hardmath
Dec 22 '10 at 12:15
1
1
$begingroup$
@hardmath: I'm aware that such limitations exist in C and other such languages; I got curious on how Mathematica did it with this snippet: "Bitwise operations in Mathematica in effect allow integers to have an unlimited number of digits. When an integer is negative, it is taken to be represented in two's complement form, with an infinite sequence of ones on the left. This allows
BitNot[n] to be equivalent simply to $-1-n$"$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 12:21
$begingroup$
@hardmath: I'm aware that such limitations exist in C and other such languages; I got curious on how Mathematica did it with this snippet: "Bitwise operations in Mathematica in effect allow integers to have an unlimited number of digits. When an integer is negative, it is taken to be represented in two's complement form, with an infinite sequence of ones on the left. This allows
BitNot[n] to be equivalent simply to $-1-n$"$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 12:21
1
1
$begingroup$
@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!
$endgroup$
– hardmath
Dec 22 '10 at 13:32
$begingroup$
@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!
$endgroup$
– hardmath
Dec 22 '10 at 13:32
|
show 5 more comments
3 Answers
3
active
oldest
votes
$begingroup$
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:
$$
operatorname{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 bmod 2 right ) right ) right ]
$$
I will now move fairly quickly through the remaining bitwise operations.
OR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rceil right ] + b - b bmod 2^{left lfloor log_{2}(a) right rfloor}
$$
AND (using the floor of an addition):
$$
operatorname{and}_1(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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rfloor right ]
$$
AND (using multiplication):
$$
operatorname{and}_2(a,b)=sum_{n=1}^{left lfloor log_{2}(a) right rfloor} left [ n left ( left lfloor frac{a}{2^{n}} right rfloor bmod 2 right ) left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ]
$$
XOR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ) bmod 2 right ) right ] + b - b bmod 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.
$endgroup$
add a comment |
$begingroup$
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).
$endgroup$
$begingroup$
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?
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 5:24
add a comment |
$begingroup$
I have no references; I've just puzzled a bit on this nice question. I'm not sure the result resembles what you were looking for, but one simple way is to use induction to define bitwise and.
$
newcommand{bnot}{mathop{boxed{lnot}}}
newcommand{band}{mathbin{boxed{land}}}
newcommand{bor}{mathbin{boxed{lor}}}
newcommand{bxor}{mathbin{boxed{notequiv}}}
newcommand{beq}{mathbin{boxed{equiv}}}
newcommand{divtwo}[1]{text{div2}(#1)}
newcommand{modtwo}[1]{text{mod2}(#1)}
$In this answer $;a,b;$ range over $;mathbb Z;$, and for consistency I will write $;bnot a;$ for bitwise negation, and similarly $;a band b;$, $;a bor b;$, $;a bxor b;$, and $;a beq b;$ for bitwise and, or, xor, and equivalence, all of which are again values in $;mathbb Z;$.
Of course we have the standard 2-complements negation
$$
bnot a ;=; -a-1
$$
And assuming we have $;band;$, then $;bor;$ can be derived by the nice
$$
a + b ;=; (a band b) + (a bor b)
$$
(This should be equivalent to DeMorgan's laws, using the above definition of $;bnot;$.)
And for equivalence and xor we have the standard
begin{align}
a beq b & ;=; (a band b) bor bnot(a bor b) \
a bxor b & ;=; bnot (a beq b) \
end{align}
Finally, to define $;band;$ inductively, we note that every $;a;$ can uniquely be split into its "leading part" and its "least significant bit":
$$
a ;=; divtwo{a} times 2 + modtwo{a}
$$
where
$$
divtwo{a} = lfloor a/2 rfloor
$$
As far as I can see, $;band;$ is now fully defined by
begin{align}
divtwo{a band b} & ;=; divtwo{a} band divtwo{b} \
modtwo{a band b} & ;=; modtwo{a} times modtwo{b} \
end{align}
I haven't done any proofs yet, but I think the above equations fully define the bitwise operators on $;mathbb Z;$. I may be missing some details, though.
So in summary, this means that the bitwise operators can be inductively defined in terms of addition, subtraction, multiplication, and 'floored division' by 2.
In principle, this is no different than the infinite sums of the first answer
$endgroup$
$begingroup$
See repl.it/HQio/11 (or gist.github.com/marnix/219cdb40a907fe0a88d556d7c722d034) for a Haskell implementation of most of the above.
$endgroup$
– Marnix Klooster
Apr 20 '17 at 18:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f15141%2fexpressing-bitwise-operations-in-terms-of-other-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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:
$$
operatorname{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 bmod 2 right ) right ) right ]
$$
I will now move fairly quickly through the remaining bitwise operations.
OR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rceil right ] + b - b bmod 2^{left lfloor log_{2}(a) right rfloor}
$$
AND (using the floor of an addition):
$$
operatorname{and}_1(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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rfloor right ]
$$
AND (using multiplication):
$$
operatorname{and}_2(a,b)=sum_{n=1}^{left lfloor log_{2}(a) right rfloor} left [ n left ( left lfloor frac{a}{2^{n}} right rfloor bmod 2 right ) left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ]
$$
XOR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ) bmod 2 right ) right ] + b - b bmod 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.
$endgroup$
add a comment |
$begingroup$
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:
$$
operatorname{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 bmod 2 right ) right ) right ]
$$
I will now move fairly quickly through the remaining bitwise operations.
OR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rceil right ] + b - b bmod 2^{left lfloor log_{2}(a) right rfloor}
$$
AND (using the floor of an addition):
$$
operatorname{and}_1(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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rfloor right ]
$$
AND (using multiplication):
$$
operatorname{and}_2(a,b)=sum_{n=1}^{left lfloor log_{2}(a) right rfloor} left [ n left ( left lfloor frac{a}{2^{n}} right rfloor bmod 2 right ) left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ]
$$
XOR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ) bmod 2 right ) right ] + b - b bmod 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.
$endgroup$
add a comment |
$begingroup$
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:
$$
operatorname{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 bmod 2 right ) right ) right ]
$$
I will now move fairly quickly through the remaining bitwise operations.
OR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rceil right ] + b - b bmod 2^{left lfloor log_{2}(a) right rfloor}
$$
AND (using the floor of an addition):
$$
operatorname{and}_1(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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rfloor right ]
$$
AND (using multiplication):
$$
operatorname{and}_2(a,b)=sum_{n=1}^{left lfloor log_{2}(a) right rfloor} left [ n left ( left lfloor frac{a}{2^{n}} right rfloor bmod 2 right ) left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ]
$$
XOR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ) bmod 2 right ) right ] + b - b bmod 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.
$endgroup$
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:
$$
operatorname{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 bmod 2 right ) right ) right ]
$$
I will now move fairly quickly through the remaining bitwise operations.
OR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rceil right ] + b - b bmod 2^{left lfloor log_{2}(a) right rfloor}
$$
AND (using the floor of an addition):
$$
operatorname{and}_1(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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) }{2} right rfloor right ]
$$
AND (using multiplication):
$$
operatorname{and}_2(a,b)=sum_{n=1}^{left lfloor log_{2}(a) right rfloor} left [ n left ( left lfloor frac{a}{2^{n}} right rfloor bmod 2 right ) left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ]
$$
XOR:
$$
operatorname{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 bmod 2 right ) + left ( left lfloor frac{b}{2^{n}} right rfloor bmod 2 right ) right ) bmod 2 right ) right ] + b - b bmod 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.
edited Jan 4 at 6:16
Henri Menke
10211
10211
answered Dec 20 '13 at 0:56
MmarssMmarss
8112
8112
add a comment |
add a comment |
$begingroup$
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).
$endgroup$
$begingroup$
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?
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 5:24
add a comment |
$begingroup$
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).
$endgroup$
$begingroup$
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?
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 5:24
add a comment |
$begingroup$
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).
$endgroup$
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).
answered Dec 22 '10 at 4:55
Yuval FilmusYuval Filmus
48.7k472145
48.7k472145
$begingroup$
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?
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 5:24
add a comment |
$begingroup$
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?
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 5:24
$begingroup$
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?
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 5:24
$begingroup$
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?
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 5:24
add a comment |
$begingroup$
I have no references; I've just puzzled a bit on this nice question. I'm not sure the result resembles what you were looking for, but one simple way is to use induction to define bitwise and.
$
newcommand{bnot}{mathop{boxed{lnot}}}
newcommand{band}{mathbin{boxed{land}}}
newcommand{bor}{mathbin{boxed{lor}}}
newcommand{bxor}{mathbin{boxed{notequiv}}}
newcommand{beq}{mathbin{boxed{equiv}}}
newcommand{divtwo}[1]{text{div2}(#1)}
newcommand{modtwo}[1]{text{mod2}(#1)}
$In this answer $;a,b;$ range over $;mathbb Z;$, and for consistency I will write $;bnot a;$ for bitwise negation, and similarly $;a band b;$, $;a bor b;$, $;a bxor b;$, and $;a beq b;$ for bitwise and, or, xor, and equivalence, all of which are again values in $;mathbb Z;$.
Of course we have the standard 2-complements negation
$$
bnot a ;=; -a-1
$$
And assuming we have $;band;$, then $;bor;$ can be derived by the nice
$$
a + b ;=; (a band b) + (a bor b)
$$
(This should be equivalent to DeMorgan's laws, using the above definition of $;bnot;$.)
And for equivalence and xor we have the standard
begin{align}
a beq b & ;=; (a band b) bor bnot(a bor b) \
a bxor b & ;=; bnot (a beq b) \
end{align}
Finally, to define $;band;$ inductively, we note that every $;a;$ can uniquely be split into its "leading part" and its "least significant bit":
$$
a ;=; divtwo{a} times 2 + modtwo{a}
$$
where
$$
divtwo{a} = lfloor a/2 rfloor
$$
As far as I can see, $;band;$ is now fully defined by
begin{align}
divtwo{a band b} & ;=; divtwo{a} band divtwo{b} \
modtwo{a band b} & ;=; modtwo{a} times modtwo{b} \
end{align}
I haven't done any proofs yet, but I think the above equations fully define the bitwise operators on $;mathbb Z;$. I may be missing some details, though.
So in summary, this means that the bitwise operators can be inductively defined in terms of addition, subtraction, multiplication, and 'floored division' by 2.
In principle, this is no different than the infinite sums of the first answer
$endgroup$
$begingroup$
See repl.it/HQio/11 (or gist.github.com/marnix/219cdb40a907fe0a88d556d7c722d034) for a Haskell implementation of most of the above.
$endgroup$
– Marnix Klooster
Apr 20 '17 at 18:04
add a comment |
$begingroup$
I have no references; I've just puzzled a bit on this nice question. I'm not sure the result resembles what you were looking for, but one simple way is to use induction to define bitwise and.
$
newcommand{bnot}{mathop{boxed{lnot}}}
newcommand{band}{mathbin{boxed{land}}}
newcommand{bor}{mathbin{boxed{lor}}}
newcommand{bxor}{mathbin{boxed{notequiv}}}
newcommand{beq}{mathbin{boxed{equiv}}}
newcommand{divtwo}[1]{text{div2}(#1)}
newcommand{modtwo}[1]{text{mod2}(#1)}
$In this answer $;a,b;$ range over $;mathbb Z;$, and for consistency I will write $;bnot a;$ for bitwise negation, and similarly $;a band b;$, $;a bor b;$, $;a bxor b;$, and $;a beq b;$ for bitwise and, or, xor, and equivalence, all of which are again values in $;mathbb Z;$.
Of course we have the standard 2-complements negation
$$
bnot a ;=; -a-1
$$
And assuming we have $;band;$, then $;bor;$ can be derived by the nice
$$
a + b ;=; (a band b) + (a bor b)
$$
(This should be equivalent to DeMorgan's laws, using the above definition of $;bnot;$.)
And for equivalence and xor we have the standard
begin{align}
a beq b & ;=; (a band b) bor bnot(a bor b) \
a bxor b & ;=; bnot (a beq b) \
end{align}
Finally, to define $;band;$ inductively, we note that every $;a;$ can uniquely be split into its "leading part" and its "least significant bit":
$$
a ;=; divtwo{a} times 2 + modtwo{a}
$$
where
$$
divtwo{a} = lfloor a/2 rfloor
$$
As far as I can see, $;band;$ is now fully defined by
begin{align}
divtwo{a band b} & ;=; divtwo{a} band divtwo{b} \
modtwo{a band b} & ;=; modtwo{a} times modtwo{b} \
end{align}
I haven't done any proofs yet, but I think the above equations fully define the bitwise operators on $;mathbb Z;$. I may be missing some details, though.
So in summary, this means that the bitwise operators can be inductively defined in terms of addition, subtraction, multiplication, and 'floored division' by 2.
In principle, this is no different than the infinite sums of the first answer
$endgroup$
$begingroup$
See repl.it/HQio/11 (or gist.github.com/marnix/219cdb40a907fe0a88d556d7c722d034) for a Haskell implementation of most of the above.
$endgroup$
– Marnix Klooster
Apr 20 '17 at 18:04
add a comment |
$begingroup$
I have no references; I've just puzzled a bit on this nice question. I'm not sure the result resembles what you were looking for, but one simple way is to use induction to define bitwise and.
$
newcommand{bnot}{mathop{boxed{lnot}}}
newcommand{band}{mathbin{boxed{land}}}
newcommand{bor}{mathbin{boxed{lor}}}
newcommand{bxor}{mathbin{boxed{notequiv}}}
newcommand{beq}{mathbin{boxed{equiv}}}
newcommand{divtwo}[1]{text{div2}(#1)}
newcommand{modtwo}[1]{text{mod2}(#1)}
$In this answer $;a,b;$ range over $;mathbb Z;$, and for consistency I will write $;bnot a;$ for bitwise negation, and similarly $;a band b;$, $;a bor b;$, $;a bxor b;$, and $;a beq b;$ for bitwise and, or, xor, and equivalence, all of which are again values in $;mathbb Z;$.
Of course we have the standard 2-complements negation
$$
bnot a ;=; -a-1
$$
And assuming we have $;band;$, then $;bor;$ can be derived by the nice
$$
a + b ;=; (a band b) + (a bor b)
$$
(This should be equivalent to DeMorgan's laws, using the above definition of $;bnot;$.)
And for equivalence and xor we have the standard
begin{align}
a beq b & ;=; (a band b) bor bnot(a bor b) \
a bxor b & ;=; bnot (a beq b) \
end{align}
Finally, to define $;band;$ inductively, we note that every $;a;$ can uniquely be split into its "leading part" and its "least significant bit":
$$
a ;=; divtwo{a} times 2 + modtwo{a}
$$
where
$$
divtwo{a} = lfloor a/2 rfloor
$$
As far as I can see, $;band;$ is now fully defined by
begin{align}
divtwo{a band b} & ;=; divtwo{a} band divtwo{b} \
modtwo{a band b} & ;=; modtwo{a} times modtwo{b} \
end{align}
I haven't done any proofs yet, but I think the above equations fully define the bitwise operators on $;mathbb Z;$. I may be missing some details, though.
So in summary, this means that the bitwise operators can be inductively defined in terms of addition, subtraction, multiplication, and 'floored division' by 2.
In principle, this is no different than the infinite sums of the first answer
$endgroup$
I have no references; I've just puzzled a bit on this nice question. I'm not sure the result resembles what you were looking for, but one simple way is to use induction to define bitwise and.
$
newcommand{bnot}{mathop{boxed{lnot}}}
newcommand{band}{mathbin{boxed{land}}}
newcommand{bor}{mathbin{boxed{lor}}}
newcommand{bxor}{mathbin{boxed{notequiv}}}
newcommand{beq}{mathbin{boxed{equiv}}}
newcommand{divtwo}[1]{text{div2}(#1)}
newcommand{modtwo}[1]{text{mod2}(#1)}
$In this answer $;a,b;$ range over $;mathbb Z;$, and for consistency I will write $;bnot a;$ for bitwise negation, and similarly $;a band b;$, $;a bor b;$, $;a bxor b;$, and $;a beq b;$ for bitwise and, or, xor, and equivalence, all of which are again values in $;mathbb Z;$.
Of course we have the standard 2-complements negation
$$
bnot a ;=; -a-1
$$
And assuming we have $;band;$, then $;bor;$ can be derived by the nice
$$
a + b ;=; (a band b) + (a bor b)
$$
(This should be equivalent to DeMorgan's laws, using the above definition of $;bnot;$.)
And for equivalence and xor we have the standard
begin{align}
a beq b & ;=; (a band b) bor bnot(a bor b) \
a bxor b & ;=; bnot (a beq b) \
end{align}
Finally, to define $;band;$ inductively, we note that every $;a;$ can uniquely be split into its "leading part" and its "least significant bit":
$$
a ;=; divtwo{a} times 2 + modtwo{a}
$$
where
$$
divtwo{a} = lfloor a/2 rfloor
$$
As far as I can see, $;band;$ is now fully defined by
begin{align}
divtwo{a band b} & ;=; divtwo{a} band divtwo{b} \
modtwo{a band b} & ;=; modtwo{a} times modtwo{b} \
end{align}
I haven't done any proofs yet, but I think the above equations fully define the bitwise operators on $;mathbb Z;$. I may be missing some details, though.
So in summary, this means that the bitwise operators can be inductively defined in terms of addition, subtraction, multiplication, and 'floored division' by 2.
In principle, this is no different than the infinite sums of the first answer
answered Jan 19 '15 at 6:11
Marnix KloosterMarnix Klooster
4,22122149
4,22122149
$begingroup$
See repl.it/HQio/11 (or gist.github.com/marnix/219cdb40a907fe0a88d556d7c722d034) for a Haskell implementation of most of the above.
$endgroup$
– Marnix Klooster
Apr 20 '17 at 18:04
add a comment |
$begingroup$
See repl.it/HQio/11 (or gist.github.com/marnix/219cdb40a907fe0a88d556d7c722d034) for a Haskell implementation of most of the above.
$endgroup$
– Marnix Klooster
Apr 20 '17 at 18:04
$begingroup$
See repl.it/HQio/11 (or gist.github.com/marnix/219cdb40a907fe0a88d556d7c722d034) for a Haskell implementation of most of the above.
$endgroup$
– Marnix Klooster
Apr 20 '17 at 18:04
$begingroup$
See repl.it/HQio/11 (or gist.github.com/marnix/219cdb40a907fe0a88d556d7c722d034) for a Haskell implementation of most of the above.
$endgroup$
– Marnix Klooster
Apr 20 '17 at 18:04
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f15141%2fexpressing-bitwise-operations-in-terms-of-other-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
$begingroup$
@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
$endgroup$
– Quixotic
Dec 22 '10 at 6:29
2
$begingroup$
@Deb: The discussion there seems to be for fractions; here, I am considering bitwise operations on integers.
$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 6:31
1
$begingroup$
@J.M.: Note that the formula -1-n for bitwise NOT(n) involves an identity of machine representations rather than of integers, inasmuch as the machine representation of "-1" in twos-complement is the integer $2^w-1$ for suitable word size w. Hence that formula relies on a fixed word size and limited range of values.
$endgroup$
– hardmath
Dec 22 '10 at 12:15
1
$begingroup$
@hardmath: I'm aware that such limitations exist in C and other such languages; I got curious on how Mathematica did it with this snippet: "Bitwise operations in Mathematica in effect allow integers to have an unlimited number of digits. When an integer is negative, it is taken to be represented in two's complement form, with an infinite sequence of ones on the left. This allows
BitNot[n]to be equivalent simply to $-1-n$"$endgroup$
– J. M. is not a mathematician
Dec 22 '10 at 12:21
1
$begingroup$
@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!
$endgroup$
– hardmath
Dec 22 '10 at 13:32