Skip to content

Commit 5845602

Browse files
authored
Merge pull request #797 from jeqcho/master
Fixing condition to prevent two's complement overflow when summing
2 parents 0517ba9 + 9ba630e commit 5845602

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

acknowledgements.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ If you make a pull request, please also add your name here in the alphabetical o
1515
* Michelle Chiang
1616
* Gabriel Chiong
1717
* Daniel Chiu
18+
* Je-Qin Chooi
1819
* Chi-Ning Chou
1920
* Michael Colavita
2021
* Brenna Courtney

lec_02_representation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Hence the two's complement representation of such a number $k$ is a string of le
254254

255255

256256
Another way to say this is that we represent a potentially negative number $k \in \{ -2^n,\ldots, 2^n-1 \}$ as the non-negative number $k \mod 2^{n+1}$ (see also [twoscomplementfig](){.ref}).
257-
This means that if two (potentially negative) numbers $k$ and $k'$ are not too large (i.e., $|k|+|k'|<2^{n+1}$), then we can compute the representation of $k+k'$ by adding modulo $2^{n+1}$ the representations of $k$ and $k'$ as if they were non-negative integers.
257+
This means that if two (potentially negative) numbers $k$ and $k'$ are not too large (i.e., $ k + k' \in \{ -2^n,\ldots, 2^n-1 \}$), then we can compute the representation of $k+k'$ by adding modulo $2^{n+1}$ the representations of $k$ and $k'$ as if they were non-negative integers.
258258
This property of the two's complement representation is its main attraction since, depending on their architectures, microprocessors can often perform arithmetic operations modulo $2^w$ very efficiently (for certain values of $w$ such as $32$ and $64$).
259259
Many systems leave it to the programmer to check that values are not too large and will carry out this modular arithmetic regardless of the size of the numbers involved.
260260
For this reason, in some systems adding two large positive numbers can result in a _negative_ number (e.g., adding $2^n-100$ and $2^n-200$ might result in $-300$ since $(2^{n+1}-300) \mod 2^{n+1} = -300$, see also [twoscomplementfig](){.ref}).

0 commit comments

Comments
 (0)