BitNot does not flip bits in the way I expected
$begingroup$
Can anyone explain why the last result in these statements is not the bit-flipped version of arr?
(Debug) In[189]:= arr = {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) Out[189]= {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) In[190]:= FromDigits[%, 2]
(Debug) Out[190]= 34
(Debug) In[191]:= BitNot[%]
(Debug) Out[191]= -35
(Debug) In[192]:= IntegerDigits[%, 2, 8]
(Debug) Out[192]= {0, 0, 1, 0, 0, 0, 1, 1}
binary
$endgroup$
|
show 5 more comments
$begingroup$
Can anyone explain why the last result in these statements is not the bit-flipped version of arr?
(Debug) In[189]:= arr = {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) Out[189]= {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) In[190]:= FromDigits[%, 2]
(Debug) Out[190]= 34
(Debug) In[191]:= BitNot[%]
(Debug) Out[191]= -35
(Debug) In[192]:= IntegerDigits[%, 2, 8]
(Debug) Out[192]= {0, 0, 1, 0, 0, 0, 1, 1}
binary
$endgroup$
5
$begingroup$
"IntegerDigits[n] discards the sign of n."
$endgroup$
– kglr
Mar 12 at 21:37
$begingroup$
Is there a work around?
$endgroup$
– bc888
Mar 12 at 21:43
$begingroup$
not any I know of.
$endgroup$
– kglr
Mar 12 at 21:44
9
$begingroup$
Integers can have arbitrary length, so how many leading zeros should be flipped? The documentation clarifies: "Integers are assumed to be represented in two's complement form, with an unlimited number of digits, so thatBitNot[n]
is simply equivalent to-1-n
."
$endgroup$
– Chip Hurst
Mar 12 at 22:00
1
$begingroup$
If you just want to flip "bits" in an array of 1/0 elements without the need to go between integer representation, just useBitXor[1,array]
...
$endgroup$
– ciao
Mar 14 at 6:34
|
show 5 more comments
$begingroup$
Can anyone explain why the last result in these statements is not the bit-flipped version of arr?
(Debug) In[189]:= arr = {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) Out[189]= {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) In[190]:= FromDigits[%, 2]
(Debug) Out[190]= 34
(Debug) In[191]:= BitNot[%]
(Debug) Out[191]= -35
(Debug) In[192]:= IntegerDigits[%, 2, 8]
(Debug) Out[192]= {0, 0, 1, 0, 0, 0, 1, 1}
binary
$endgroup$
Can anyone explain why the last result in these statements is not the bit-flipped version of arr?
(Debug) In[189]:= arr = {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) Out[189]= {0, 0, 1, 0, 0, 0, 1, 0}
(Debug) In[190]:= FromDigits[%, 2]
(Debug) Out[190]= 34
(Debug) In[191]:= BitNot[%]
(Debug) Out[191]= -35
(Debug) In[192]:= IntegerDigits[%, 2, 8]
(Debug) Out[192]= {0, 0, 1, 0, 0, 0, 1, 1}
binary
binary
edited Mar 12 at 22:45
m_goldberg
87.7k872198
87.7k872198
asked Mar 12 at 21:30
bc888bc888
764
764
5
$begingroup$
"IntegerDigits[n] discards the sign of n."
$endgroup$
– kglr
Mar 12 at 21:37
$begingroup$
Is there a work around?
$endgroup$
– bc888
Mar 12 at 21:43
$begingroup$
not any I know of.
$endgroup$
– kglr
Mar 12 at 21:44
9
$begingroup$
Integers can have arbitrary length, so how many leading zeros should be flipped? The documentation clarifies: "Integers are assumed to be represented in two's complement form, with an unlimited number of digits, so thatBitNot[n]
is simply equivalent to-1-n
."
$endgroup$
– Chip Hurst
Mar 12 at 22:00
1
$begingroup$
If you just want to flip "bits" in an array of 1/0 elements without the need to go between integer representation, just useBitXor[1,array]
...
$endgroup$
– ciao
Mar 14 at 6:34
|
show 5 more comments
5
$begingroup$
"IntegerDigits[n] discards the sign of n."
$endgroup$
– kglr
Mar 12 at 21:37
$begingroup$
Is there a work around?
$endgroup$
– bc888
Mar 12 at 21:43
$begingroup$
not any I know of.
$endgroup$
– kglr
Mar 12 at 21:44
9
$begingroup$
Integers can have arbitrary length, so how many leading zeros should be flipped? The documentation clarifies: "Integers are assumed to be represented in two's complement form, with an unlimited number of digits, so thatBitNot[n]
is simply equivalent to-1-n
."
$endgroup$
– Chip Hurst
Mar 12 at 22:00
1
$begingroup$
If you just want to flip "bits" in an array of 1/0 elements without the need to go between integer representation, just useBitXor[1,array]
...
$endgroup$
– ciao
Mar 14 at 6:34
5
5
$begingroup$
"IntegerDigits[n] discards the sign of n."
$endgroup$
– kglr
Mar 12 at 21:37
$begingroup$
"IntegerDigits[n] discards the sign of n."
$endgroup$
– kglr
Mar 12 at 21:37
$begingroup$
Is there a work around?
$endgroup$
– bc888
Mar 12 at 21:43
$begingroup$
Is there a work around?
$endgroup$
– bc888
Mar 12 at 21:43
$begingroup$
not any I know of.
$endgroup$
– kglr
Mar 12 at 21:44
$begingroup$
not any I know of.
$endgroup$
– kglr
Mar 12 at 21:44
9
9
$begingroup$
Integers can have arbitrary length, so how many leading zeros should be flipped? The documentation clarifies: "Integers are assumed to be represented in two's complement form, with an unlimited number of digits, so that
BitNot[n]
is simply equivalent to -1-n
."$endgroup$
– Chip Hurst
Mar 12 at 22:00
$begingroup$
Integers can have arbitrary length, so how many leading zeros should be flipped? The documentation clarifies: "Integers are assumed to be represented in two's complement form, with an unlimited number of digits, so that
BitNot[n]
is simply equivalent to -1-n
."$endgroup$
– Chip Hurst
Mar 12 at 22:00
1
1
$begingroup$
If you just want to flip "bits" in an array of 1/0 elements without the need to go between integer representation, just use
BitXor[1,array]
...$endgroup$
– ciao
Mar 14 at 6:34
$begingroup$
If you just want to flip "bits" in an array of 1/0 elements without the need to go between integer representation, just use
BitXor[1,array]
...$endgroup$
– ciao
Mar 14 at 6:34
|
show 5 more comments
4 Answers
4
active
oldest
votes
$begingroup$
I don't think there is a built-in function to generate the two's complement representation. Easy to implement though.
twosComplement[x_, n_] := IntegerDigits[2^x - n, 2, n]
twosComplement[35, 8]
(* {1, 1, 0, 1, 1, 1, 0, 1} *)
$endgroup$
add a comment |
$begingroup$
twosComplement[x_, n_] := UnitBox@IntegerDigits[x, 2, n]
twosComplement[35, 8]
{1, 1, 0, 1, 1, 1, 0, 1}
$endgroup$
add a comment |
$begingroup$
Without using IntegerDigits
:
With[{n = 34},
{n, BitXor[BitShiftLeft[1, BitLength[n]] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11101₂}
With[{n = 34, p = 8},
{n, BitXor[BitShiftLeft[1, p] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11011101₂}
$endgroup$
add a comment |
$begingroup$
FlipBits[num_Integer, len_.] :=
Module[{arr}, arr = IntegerDigits[num, 2, len];
1 - arr]
$endgroup$
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: "387"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
},
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%2fmathematica.stackexchange.com%2fquestions%2f193136%2fbitnot-does-not-flip-bits-in-the-way-i-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I don't think there is a built-in function to generate the two's complement representation. Easy to implement though.
twosComplement[x_, n_] := IntegerDigits[2^x - n, 2, n]
twosComplement[35, 8]
(* {1, 1, 0, 1, 1, 1, 0, 1} *)
$endgroup$
add a comment |
$begingroup$
I don't think there is a built-in function to generate the two's complement representation. Easy to implement though.
twosComplement[x_, n_] := IntegerDigits[2^x - n, 2, n]
twosComplement[35, 8]
(* {1, 1, 0, 1, 1, 1, 0, 1} *)
$endgroup$
add a comment |
$begingroup$
I don't think there is a built-in function to generate the two's complement representation. Easy to implement though.
twosComplement[x_, n_] := IntegerDigits[2^x - n, 2, n]
twosComplement[35, 8]
(* {1, 1, 0, 1, 1, 1, 0, 1} *)
$endgroup$
I don't think there is a built-in function to generate the two's complement representation. Easy to implement though.
twosComplement[x_, n_] := IntegerDigits[2^x - n, 2, n]
twosComplement[35, 8]
(* {1, 1, 0, 1, 1, 1, 0, 1} *)
answered Mar 12 at 22:03
Rohit NamjoshiRohit Namjoshi
1,4701213
1,4701213
add a comment |
add a comment |
$begingroup$
twosComplement[x_, n_] := UnitBox@IntegerDigits[x, 2, n]
twosComplement[35, 8]
{1, 1, 0, 1, 1, 1, 0, 1}
$endgroup$
add a comment |
$begingroup$
twosComplement[x_, n_] := UnitBox@IntegerDigits[x, 2, n]
twosComplement[35, 8]
{1, 1, 0, 1, 1, 1, 0, 1}
$endgroup$
add a comment |
$begingroup$
twosComplement[x_, n_] := UnitBox@IntegerDigits[x, 2, n]
twosComplement[35, 8]
{1, 1, 0, 1, 1, 1, 0, 1}
$endgroup$
twosComplement[x_, n_] := UnitBox@IntegerDigits[x, 2, n]
twosComplement[35, 8]
{1, 1, 0, 1, 1, 1, 0, 1}
answered Mar 12 at 22:14
Okkes DulgerciOkkes Dulgerci
5,4141919
5,4141919
add a comment |
add a comment |
$begingroup$
Without using IntegerDigits
:
With[{n = 34},
{n, BitXor[BitShiftLeft[1, BitLength[n]] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11101₂}
With[{n = 34, p = 8},
{n, BitXor[BitShiftLeft[1, p] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11011101₂}
$endgroup$
add a comment |
$begingroup$
Without using IntegerDigits
:
With[{n = 34},
{n, BitXor[BitShiftLeft[1, BitLength[n]] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11101₂}
With[{n = 34, p = 8},
{n, BitXor[BitShiftLeft[1, p] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11011101₂}
$endgroup$
add a comment |
$begingroup$
Without using IntegerDigits
:
With[{n = 34},
{n, BitXor[BitShiftLeft[1, BitLength[n]] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11101₂}
With[{n = 34, p = 8},
{n, BitXor[BitShiftLeft[1, p] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11011101₂}
$endgroup$
Without using IntegerDigits
:
With[{n = 34},
{n, BitXor[BitShiftLeft[1, BitLength[n]] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11101₂}
With[{n = 34, p = 8},
{n, BitXor[BitShiftLeft[1, p] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11011101₂}
answered Mar 12 at 22:33
J. M. is slightly pensive♦J. M. is slightly pensive
98.1k10306465
98.1k10306465
add a comment |
add a comment |
$begingroup$
FlipBits[num_Integer, len_.] :=
Module[{arr}, arr = IntegerDigits[num, 2, len];
1 - arr]
$endgroup$
add a comment |
$begingroup$
FlipBits[num_Integer, len_.] :=
Module[{arr}, arr = IntegerDigits[num, 2, len];
1 - arr]
$endgroup$
add a comment |
$begingroup$
FlipBits[num_Integer, len_.] :=
Module[{arr}, arr = IntegerDigits[num, 2, len];
1 - arr]
$endgroup$
FlipBits[num_Integer, len_.] :=
Module[{arr}, arr = IntegerDigits[num, 2, len];
1 - arr]
answered Mar 12 at 21:55
bc888bc888
764
764
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica 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%2fmathematica.stackexchange.com%2fquestions%2f193136%2fbitnot-does-not-flip-bits-in-the-way-i-expected%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
5
$begingroup$
"IntegerDigits[n] discards the sign of n."
$endgroup$
– kglr
Mar 12 at 21:37
$begingroup$
Is there a work around?
$endgroup$
– bc888
Mar 12 at 21:43
$begingroup$
not any I know of.
$endgroup$
– kglr
Mar 12 at 21:44
9
$begingroup$
Integers can have arbitrary length, so how many leading zeros should be flipped? The documentation clarifies: "Integers are assumed to be represented in two's complement form, with an unlimited number of digits, so that
BitNot[n]
is simply equivalent to-1-n
."$endgroup$
– Chip Hurst
Mar 12 at 22:00
1
$begingroup$
If you just want to flip "bits" in an array of 1/0 elements without the need to go between integer representation, just use
BitXor[1,array]
...$endgroup$
– ciao
Mar 14 at 6:34