Why for two binary numbers $x,y$ is $neg (neg x + y) = x - y $ true?












0












$begingroup$


Iv'e noticed, empirically, that for two integers $x$ and $y$ in binary representation (two's complement) it holds that $$neg (neg x + y) = x - y $$



Where $neg$ is the bitwise "not" operation and we discard the overflow bit if it exists. (Say the two numbers are of a fixed width $k$ and so all operations are modulo $2^k$.)



Why does this work? (or am I wrong and there exists a counter example?)










share|cite|improve this question









$endgroup$












  • $begingroup$
    I'm guessing some form of De Morgan's laws would apply here, together with the definition of subtraction in terms of addition.
    $endgroup$
    – Shaun
    Jan 22 at 17:18










  • $begingroup$
    @DonThousand In two's complement, $neg x +x=-1$
    $endgroup$
    – saulspatz
    Jan 22 at 17:48










  • $begingroup$
    @saulspatz Correct, my bad. My argument could be modified to hold regardless.
    $endgroup$
    – Don Thousand
    Jan 22 at 17:52
















0












$begingroup$


Iv'e noticed, empirically, that for two integers $x$ and $y$ in binary representation (two's complement) it holds that $$neg (neg x + y) = x - y $$



Where $neg$ is the bitwise "not" operation and we discard the overflow bit if it exists. (Say the two numbers are of a fixed width $k$ and so all operations are modulo $2^k$.)



Why does this work? (or am I wrong and there exists a counter example?)










share|cite|improve this question









$endgroup$












  • $begingroup$
    I'm guessing some form of De Morgan's laws would apply here, together with the definition of subtraction in terms of addition.
    $endgroup$
    – Shaun
    Jan 22 at 17:18










  • $begingroup$
    @DonThousand In two's complement, $neg x +x=-1$
    $endgroup$
    – saulspatz
    Jan 22 at 17:48










  • $begingroup$
    @saulspatz Correct, my bad. My argument could be modified to hold regardless.
    $endgroup$
    – Don Thousand
    Jan 22 at 17:52














0












0








0





$begingroup$


Iv'e noticed, empirically, that for two integers $x$ and $y$ in binary representation (two's complement) it holds that $$neg (neg x + y) = x - y $$



Where $neg$ is the bitwise "not" operation and we discard the overflow bit if it exists. (Say the two numbers are of a fixed width $k$ and so all operations are modulo $2^k$.)



Why does this work? (or am I wrong and there exists a counter example?)










share|cite|improve this question









$endgroup$




Iv'e noticed, empirically, that for two integers $x$ and $y$ in binary representation (two's complement) it holds that $$neg (neg x + y) = x - y $$



Where $neg$ is the bitwise "not" operation and we discard the overflow bit if it exists. (Say the two numbers are of a fixed width $k$ and so all operations are modulo $2^k$.)



Why does this work? (or am I wrong and there exists a counter example?)







boolean-algebra binary






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Jan 22 at 17:07









H.RappeportH.Rappeport

6801510




6801510












  • $begingroup$
    I'm guessing some form of De Morgan's laws would apply here, together with the definition of subtraction in terms of addition.
    $endgroup$
    – Shaun
    Jan 22 at 17:18










  • $begingroup$
    @DonThousand In two's complement, $neg x +x=-1$
    $endgroup$
    – saulspatz
    Jan 22 at 17:48










  • $begingroup$
    @saulspatz Correct, my bad. My argument could be modified to hold regardless.
    $endgroup$
    – Don Thousand
    Jan 22 at 17:52


















  • $begingroup$
    I'm guessing some form of De Morgan's laws would apply here, together with the definition of subtraction in terms of addition.
    $endgroup$
    – Shaun
    Jan 22 at 17:18










  • $begingroup$
    @DonThousand In two's complement, $neg x +x=-1$
    $endgroup$
    – saulspatz
    Jan 22 at 17:48










  • $begingroup$
    @saulspatz Correct, my bad. My argument could be modified to hold regardless.
    $endgroup$
    – Don Thousand
    Jan 22 at 17:52
















$begingroup$
I'm guessing some form of De Morgan's laws would apply here, together with the definition of subtraction in terms of addition.
$endgroup$
– Shaun
Jan 22 at 17:18




$begingroup$
I'm guessing some form of De Morgan's laws would apply here, together with the definition of subtraction in terms of addition.
$endgroup$
– Shaun
Jan 22 at 17:18












$begingroup$
@DonThousand In two's complement, $neg x +x=-1$
$endgroup$
– saulspatz
Jan 22 at 17:48




$begingroup$
@DonThousand In two's complement, $neg x +x=-1$
$endgroup$
– saulspatz
Jan 22 at 17:48












$begingroup$
@saulspatz Correct, my bad. My argument could be modified to hold regardless.
$endgroup$
– Don Thousand
Jan 22 at 17:52




$begingroup$
@saulspatz Correct, my bad. My argument could be modified to hold regardless.
$endgroup$
– Don Thousand
Jan 22 at 17:52










1 Answer
1






active

oldest

votes


















1












$begingroup$

If you discard overflow, then for any $x$ you get $neg x+x+1=11...11+00...01=00...00=0$,
so $neg x=-x-1$. Using only this identity, we can find
begin{align}
neg(neg x+y)
&=-(neg x+y)-1
\&=-(-x-1+y)-1
\&=x-y.
end{align}

For this reason, when your computer needs to calculate $-x$ from $x$, it actually calculates $neg x+1$. Also, any expression of the form $x-y$ will likely be compiled into $x+neg y+1$.



To sidetrack even more and relate this to some pure maths just for fun, recall $sum_{n=1}^infty x^n=(1-x)^{-1}$ holds for $|x|<1$. Now notice that plugging in $x=2$ the left hand side becomes $sum_{n=1}^infty2^n$ and the right hand side becomes $-1$. If you write down the partial sums of $sum_{n=1}^infty2^n$, you get $11...11$. This is not a coincidence :)






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Thank you for the insightful answer. Regarding the second paragraph, I'm intrigued, would you care to add a hint?
    $endgroup$
    – H.Rappeport
    Jan 23 at 9:57












  • $begingroup$
    I am by no means an expert on this particular topic, but I am fascinated by it as well. What I know is that this is related to $p$-adic numbers and the $p$-adic metric. There is a great video by the amazing channel 3Blue1Brown on YouTube about how one might invent the $p$-adic numbers to interpret this result. youtube.com/watch?v=XFDM1ip5HdU
    $endgroup$
    – SmileyCraft
    Jan 23 at 15:19











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3083414%2fwhy-for-two-binary-numbers-x-y-is-neg-neg-x-y-x-y-true%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1












$begingroup$

If you discard overflow, then for any $x$ you get $neg x+x+1=11...11+00...01=00...00=0$,
so $neg x=-x-1$. Using only this identity, we can find
begin{align}
neg(neg x+y)
&=-(neg x+y)-1
\&=-(-x-1+y)-1
\&=x-y.
end{align}

For this reason, when your computer needs to calculate $-x$ from $x$, it actually calculates $neg x+1$. Also, any expression of the form $x-y$ will likely be compiled into $x+neg y+1$.



To sidetrack even more and relate this to some pure maths just for fun, recall $sum_{n=1}^infty x^n=(1-x)^{-1}$ holds for $|x|<1$. Now notice that plugging in $x=2$ the left hand side becomes $sum_{n=1}^infty2^n$ and the right hand side becomes $-1$. If you write down the partial sums of $sum_{n=1}^infty2^n$, you get $11...11$. This is not a coincidence :)






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Thank you for the insightful answer. Regarding the second paragraph, I'm intrigued, would you care to add a hint?
    $endgroup$
    – H.Rappeport
    Jan 23 at 9:57












  • $begingroup$
    I am by no means an expert on this particular topic, but I am fascinated by it as well. What I know is that this is related to $p$-adic numbers and the $p$-adic metric. There is a great video by the amazing channel 3Blue1Brown on YouTube about how one might invent the $p$-adic numbers to interpret this result. youtube.com/watch?v=XFDM1ip5HdU
    $endgroup$
    – SmileyCraft
    Jan 23 at 15:19
















1












$begingroup$

If you discard overflow, then for any $x$ you get $neg x+x+1=11...11+00...01=00...00=0$,
so $neg x=-x-1$. Using only this identity, we can find
begin{align}
neg(neg x+y)
&=-(neg x+y)-1
\&=-(-x-1+y)-1
\&=x-y.
end{align}

For this reason, when your computer needs to calculate $-x$ from $x$, it actually calculates $neg x+1$. Also, any expression of the form $x-y$ will likely be compiled into $x+neg y+1$.



To sidetrack even more and relate this to some pure maths just for fun, recall $sum_{n=1}^infty x^n=(1-x)^{-1}$ holds for $|x|<1$. Now notice that plugging in $x=2$ the left hand side becomes $sum_{n=1}^infty2^n$ and the right hand side becomes $-1$. If you write down the partial sums of $sum_{n=1}^infty2^n$, you get $11...11$. This is not a coincidence :)






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Thank you for the insightful answer. Regarding the second paragraph, I'm intrigued, would you care to add a hint?
    $endgroup$
    – H.Rappeport
    Jan 23 at 9:57












  • $begingroup$
    I am by no means an expert on this particular topic, but I am fascinated by it as well. What I know is that this is related to $p$-adic numbers and the $p$-adic metric. There is a great video by the amazing channel 3Blue1Brown on YouTube about how one might invent the $p$-adic numbers to interpret this result. youtube.com/watch?v=XFDM1ip5HdU
    $endgroup$
    – SmileyCraft
    Jan 23 at 15:19














1












1








1





$begingroup$

If you discard overflow, then for any $x$ you get $neg x+x+1=11...11+00...01=00...00=0$,
so $neg x=-x-1$. Using only this identity, we can find
begin{align}
neg(neg x+y)
&=-(neg x+y)-1
\&=-(-x-1+y)-1
\&=x-y.
end{align}

For this reason, when your computer needs to calculate $-x$ from $x$, it actually calculates $neg x+1$. Also, any expression of the form $x-y$ will likely be compiled into $x+neg y+1$.



To sidetrack even more and relate this to some pure maths just for fun, recall $sum_{n=1}^infty x^n=(1-x)^{-1}$ holds for $|x|<1$. Now notice that plugging in $x=2$ the left hand side becomes $sum_{n=1}^infty2^n$ and the right hand side becomes $-1$. If you write down the partial sums of $sum_{n=1}^infty2^n$, you get $11...11$. This is not a coincidence :)






share|cite|improve this answer









$endgroup$



If you discard overflow, then for any $x$ you get $neg x+x+1=11...11+00...01=00...00=0$,
so $neg x=-x-1$. Using only this identity, we can find
begin{align}
neg(neg x+y)
&=-(neg x+y)-1
\&=-(-x-1+y)-1
\&=x-y.
end{align}

For this reason, when your computer needs to calculate $-x$ from $x$, it actually calculates $neg x+1$. Also, any expression of the form $x-y$ will likely be compiled into $x+neg y+1$.



To sidetrack even more and relate this to some pure maths just for fun, recall $sum_{n=1}^infty x^n=(1-x)^{-1}$ holds for $|x|<1$. Now notice that plugging in $x=2$ the left hand side becomes $sum_{n=1}^infty2^n$ and the right hand side becomes $-1$. If you write down the partial sums of $sum_{n=1}^infty2^n$, you get $11...11$. This is not a coincidence :)







share|cite|improve this answer












share|cite|improve this answer



share|cite|improve this answer










answered Jan 22 at 18:24









SmileyCraftSmileyCraft

3,626519




3,626519












  • $begingroup$
    Thank you for the insightful answer. Regarding the second paragraph, I'm intrigued, would you care to add a hint?
    $endgroup$
    – H.Rappeport
    Jan 23 at 9:57












  • $begingroup$
    I am by no means an expert on this particular topic, but I am fascinated by it as well. What I know is that this is related to $p$-adic numbers and the $p$-adic metric. There is a great video by the amazing channel 3Blue1Brown on YouTube about how one might invent the $p$-adic numbers to interpret this result. youtube.com/watch?v=XFDM1ip5HdU
    $endgroup$
    – SmileyCraft
    Jan 23 at 15:19


















  • $begingroup$
    Thank you for the insightful answer. Regarding the second paragraph, I'm intrigued, would you care to add a hint?
    $endgroup$
    – H.Rappeport
    Jan 23 at 9:57












  • $begingroup$
    I am by no means an expert on this particular topic, but I am fascinated by it as well. What I know is that this is related to $p$-adic numbers and the $p$-adic metric. There is a great video by the amazing channel 3Blue1Brown on YouTube about how one might invent the $p$-adic numbers to interpret this result. youtube.com/watch?v=XFDM1ip5HdU
    $endgroup$
    – SmileyCraft
    Jan 23 at 15:19
















$begingroup$
Thank you for the insightful answer. Regarding the second paragraph, I'm intrigued, would you care to add a hint?
$endgroup$
– H.Rappeport
Jan 23 at 9:57






$begingroup$
Thank you for the insightful answer. Regarding the second paragraph, I'm intrigued, would you care to add a hint?
$endgroup$
– H.Rappeport
Jan 23 at 9:57














$begingroup$
I am by no means an expert on this particular topic, but I am fascinated by it as well. What I know is that this is related to $p$-adic numbers and the $p$-adic metric. There is a great video by the amazing channel 3Blue1Brown on YouTube about how one might invent the $p$-adic numbers to interpret this result. youtube.com/watch?v=XFDM1ip5HdU
$endgroup$
– SmileyCraft
Jan 23 at 15:19




$begingroup$
I am by no means an expert on this particular topic, but I am fascinated by it as well. What I know is that this is related to $p$-adic numbers and the $p$-adic metric. There is a great video by the amazing channel 3Blue1Brown on YouTube about how one might invent the $p$-adic numbers to interpret this result. youtube.com/watch?v=XFDM1ip5HdU
$endgroup$
– SmileyCraft
Jan 23 at 15:19


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3083414%2fwhy-for-two-binary-numbers-x-y-is-neg-neg-x-y-x-y-true%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory