Last active
February 5, 2017 03:37
-
-
Save veeara282/f685214ad7f3bda68f65a501380c896d to your computer and use it in GitHub Desktop.
An alternative method for subtracting two 32-bit signed integers in a two's complement system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Performs the subtraction p - q. | |
* This is an alternative method to simply computing p + (-q). | |
* TODO I'll prove why it works later. | |
*/ | |
int alt_subtract(int p, int q) { | |
// First, take the ones' complement of the minuend | |
int p1 = ~p; | |
// Next, add the subtrahend | |
int sum = p1 + q; | |
// The ones' complement of this sum is the difference p - q | |
return ~sum; | |
} | |
/** | |
* Copyright (C) 2017 Aidan Fitzgerald | |
* | |
* By themselves, the four lines of code may be ineligible for copyright. However, I claim that the | |
* combination of instructions, choice of variable and function names, and comments is copyrightable. | |
* (To be fair, most of the comment prose is this copyright notice.) | |
* | |
* To the extent that this work is copyrightable, I hereby release it under the MIT License, available here: | |
* https://opensource.org/licenses/MIT | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment