Begin notes on unsigned arithmetic.
parent
439e5f09e1
commit
794e9a63ed
|
@ -172,7 +172,7 @@
|
||||||
"algorithms/binary-search.md": "dbbaf8d4be7aabef1ed232c1906b4c99",
|
"algorithms/binary-search.md": "dbbaf8d4be7aabef1ed232c1906b4c99",
|
||||||
"_journal/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048",
|
"_journal/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048",
|
||||||
"_journal/2024-02/2024-02-16.md": "e701902e369ec53098fc2deed4ec14fd",
|
"_journal/2024-02/2024-02-16.md": "e701902e369ec53098fc2deed4ec14fd",
|
||||||
"binary/integer-encoding.md": "ef9e9c7938f9719f8a07d8019e63de56",
|
"binary/integer-encoding.md": "05049f4827556643f46315678d640cb3",
|
||||||
"combinatorics/index.md": "9a85e8858c50c9797243d6d01e1dcbe7",
|
"combinatorics/index.md": "9a85e8858c50c9797243d6d01e1dcbe7",
|
||||||
"_journal/2024-02-18.md": "67e36dbbb2cac699d4533b5a2eaeb629",
|
"_journal/2024-02-18.md": "67e36dbbb2cac699d4533b5a2eaeb629",
|
||||||
"_journal/2024-02/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048",
|
"_journal/2024-02/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048",
|
||||||
|
@ -197,7 +197,7 @@
|
||||||
"c17/escape-sequences.md": "ebc63c6cdfbe60bbc2708c1b0c8da8bb",
|
"c17/escape-sequences.md": "ebc63c6cdfbe60bbc2708c1b0c8da8bb",
|
||||||
"c17/declarations.md": "46b135d583a992991c889d518fec1c0f",
|
"c17/declarations.md": "46b135d583a992991c889d518fec1c0f",
|
||||||
"algorithms/sorting/merge-sort.md": "f66f482e5bd551c765fcba564c938d67",
|
"algorithms/sorting/merge-sort.md": "f66f482e5bd551c765fcba564c938d67",
|
||||||
"_journal/2024-02-24.md": "d447c08432793bbda79ae9a50c416ce4",
|
"_journal/2024-02-24.md": "9bb319d5014caf962a9ce3141076cff4",
|
||||||
"_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7"
|
"_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7"
|
||||||
},
|
},
|
||||||
"fields_dict": {
|
"fields_dict": {
|
||||||
|
|
|
@ -5,7 +5,7 @@ title: "2024-02-24"
|
||||||
- [x] Anki Flashcards
|
- [x] Anki Flashcards
|
||||||
- [x] KoL
|
- [x] KoL
|
||||||
- [ ] Sheet Music (10 min.)
|
- [ ] Sheet Music (10 min.)
|
||||||
- [ ] OGS (1 Life & Death Problem)
|
- [x] OGS (1 Life & Death Problem)
|
||||||
- [ ] Korean (Read 1 Story)
|
- [ ] Korean (Read 1 Story)
|
||||||
- [ ] Interview Prep (1 Practice Problem)
|
- [ ] Interview Prep (1 Practice Problem)
|
||||||
- [ ] Log Work Hours (Max 3 hours)
|
- [ ] Log Work Hours (Max 3 hours)
|
||||||
|
|
|
@ -822,6 +822,163 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
||||||
<!--ID: 1708701087982-->
|
<!--ID: 1708701087982-->
|
||||||
END%%
|
END%%
|
||||||
|
|
||||||
|
## Arithmetic
|
||||||
|
|
||||||
|
### Addition
|
||||||
|
|
||||||
|
Addition of two unsigned or two two's-complement numbers operate in much the same way as grade-school arithmetic. Digits are added one-by-one and overflows "carried" to the next summation. Overflows are truncated; the final carry bit is discarded in the underlying bit adder.
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
*Why* is adding $w$-bit integral types equal to $w$-bit truncation?
|
||||||
|
Back: The underlying bit adder discards any final carry bit.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678721-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Why should you generally prefer `x < y` over `x - y < 0`?
|
||||||
|
Back: The former avoids possible underflows.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678725-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How is `x - y < 0` rewritten more safely?
|
||||||
|
Back: `x < y`
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678728-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What hardware-level advantage does two's-complement introduce over other signed encodings?
|
||||||
|
Back: The same circuits can be used for unsigned and two's-complement addition.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678732-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What representational-level advantage does two's-complement introduce over other signed encodings?
|
||||||
|
Back: `0` is encoded in only one way.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678736-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
Unsigned addition of $w$-bit integral types, denoted $+_w^u$, behaves like so:
|
||||||
|
|
||||||
|
$$x +_w^u y = \begin{cases}
|
||||||
|
x + y & \text{if } x + y < 2^w \\
|
||||||
|
x + y - 2^w & \text{otherwise}
|
||||||
|
\end{cases}$$
|
||||||
|
|
||||||
|
This is more simply expressed as $x +_w^u y = (x + y) \bmod 2^w$.
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What kind of overflows does unsigned addition potentially exhibit?
|
||||||
|
Back: Positive overflow.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678739-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Why is unsigned addition overflow *not* UB?
|
||||||
|
Back: Because the C standard enforces unsigned encoding of `unsigned` data types.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708799678742-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What does $+_w^u$ denote?
|
||||||
|
Back: Unsigned addition of $w$-bit integral types.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678745-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Unsigned addition overflow is equivalent to what bit-level manipulation tactic?
|
||||||
|
Back: Truncation.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678748-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What is the result of $x +_w^u y$?
|
||||||
|
Back: $(x + y) \bmod 2^w$
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678751-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
*Why* does $x +_w^u y = (x + y) \bmod 2^w$?
|
||||||
|
Back: Because discarding any carry bit is equivalent to truncating the sum to $w$ bits.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678755-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Cloze
|
||||||
|
Without using modulo arithmetic, $x +_w^u y =$ {$x + y$} if {$x + y < 2^w$}.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678758-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Cloze
|
||||||
|
Without using modulo arithmetic, $x +_w^u y =$ {$x + y - 2^w$} if {$x + y \geq 2^w$}.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678761-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How do you detect whether unsigned addition $s \coloneqq x +_w^u y$ overflowed?
|
||||||
|
Back: Overflow occurs if and only if $s < x$.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678765-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How would you complete the body of this function?
|
||||||
|
```c
|
||||||
|
/* Determine whether arguments can be added without overflow */
|
||||||
|
int uadd_ok(unsigned x, unsigned y);
|
||||||
|
```
|
||||||
|
Back:
|
||||||
|
```c
|
||||||
|
return (x + y) >= x;
|
||||||
|
```
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678769-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Does unsigned overflow detection depend on the left or right operand of $s \coloneqq x +_w^u y$?
|
||||||
|
Back: Either.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678772-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Why can we compare $s$ to $x$ or $y$ when detecting overflow of $s \coloneqq x +_w^u y$?
|
||||||
|
Back: Because unsigned addition is commutative.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708799678776-->
|
||||||
|
END%%
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
|
Loading…
Reference in New Issue