From a1447e824c952daf754f2be5721a49cf67ee0df3 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Mon, 26 Feb 2024 09:23:01 -0700 Subject: [PATCH] Two's-complement addition and consolidate `shifts.md`. --- .../plugins/obsidian-to-anki-plugin/data.json | 6 +- notes/_journal/2024-02-25.md | 11 - notes/_journal/2024-02-26.md | 14 + notes/_journal/2024-02/2024-02-25.md | 28 ++ notes/binary/integer-encoding.md | 304 +++++++++++++++++- notes/binary/shifts.md | 196 ----------- 6 files changed, 346 insertions(+), 213 deletions(-) delete mode 100644 notes/_journal/2024-02-25.md create mode 100644 notes/_journal/2024-02-26.md create mode 100644 notes/_journal/2024-02/2024-02-25.md delete mode 100644 notes/binary/shifts.md diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index f92a703..3404d3f 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -172,7 +172,7 @@ "algorithms/binary-search.md": "dbbaf8d4be7aabef1ed232c1906b4c99", "_journal/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048", "_journal/2024-02/2024-02-16.md": "e701902e369ec53098fc2deed4ec14fd", - "binary/integer-encoding.md": "aaad7d74b5084093e4aa90fbb2212384", + "binary/integer-encoding.md": "bb73ce7edf88620fb4b59f33850277e2", "combinatorics/index.md": "9a85e8858c50c9797243d6d01e1dcbe7", "_journal/2024-02-18.md": "67e36dbbb2cac699d4533b5a2eaeb629", "_journal/2024-02/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048", @@ -200,7 +200,9 @@ "_journal/2024-02-24.md": "9bb319d5014caf962a9ce3141076cff4", "_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7", "_journal/2024-02-25.md": "fb1a48208c11d12262facc647749ca6f", - "_journal/2024-02/2024-02-24.md": "ee92fd63ebbda5f8ffa0abd06700ead1" + "_journal/2024-02/2024-02-24.md": "ee92fd63ebbda5f8ffa0abd06700ead1", + "_journal/2024-02-26.md": "67582808809eeb149c134d8dd021edd6", + "_journal/2024-02/2024-02-25.md": "426be827fe8483e4ab432304a2aa6df3" }, "fields_dict": { "Basic": [ diff --git a/notes/_journal/2024-02-25.md b/notes/_journal/2024-02-25.md deleted file mode 100644 index 2f22f4f..0000000 --- a/notes/_journal/2024-02-25.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -title: "2024-02-25" ---- - -- [x] Anki Flashcards -- [ ] KoL -- [ ] Sheet Music (10 min.) -- [ ] OGS (1 Life & Death Problem) -- [ ] Korean (Read 1 Story) -- [ ] Interview Prep (1 Practice Problem) -- [ ] Log Work Hours (Max 3 hours) \ No newline at end of file diff --git a/notes/_journal/2024-02-26.md b/notes/_journal/2024-02-26.md new file mode 100644 index 0000000..ea5beca --- /dev/null +++ b/notes/_journal/2024-02-26.md @@ -0,0 +1,14 @@ +--- +title: "2024-02-26" +--- + +- [x] Anki Flashcards +- [x] KoL +- [ ] Sheet Music (10 min.) +- [ ] OGS (1 Life & Death Problem) +- [ ] Korean (Read 1 Story) +- [ ] Interview Prep (1 Practice Problem) +- [ ] Log Work Hours (Max 3 hours) + +* Added flashcards on two's-complement addition. +* Consolidated `integer-encoding.md` with `shifts.md`. \ No newline at end of file diff --git a/notes/_journal/2024-02/2024-02-25.md b/notes/_journal/2024-02/2024-02-25.md new file mode 100644 index 0000000..716cba0 --- /dev/null +++ b/notes/_journal/2024-02/2024-02-25.md @@ -0,0 +1,28 @@ +--- +title: "2024-02-25" +--- + +- [x] Anki Flashcards +- [x] KoL +- [ ] Sheet Music (10 min.) +- [x] OGS (1 Life & Death Problem) +- [x] Korean (Read 1 Story) +- [ ] Interview Prep (1 Practice Problem) +- [ ] Log Work Hours (Max 3 hours) + +* Read 흥부와 놀부 (Heungbuwa Nolbu). +* 101weiqi (serial numbers) + * Q-79448 + * Q-5508 + * Q-359007 + * Q-12406 + * Q-72569 + * Q-259158 + * Q-93867 + * Q-84628 + * Q-342740 + * Q-54992 + * Q-49888 + * Q-10602 + * Q-261719 + * Q-178790 \ No newline at end of file diff --git a/notes/binary/integer-encoding.md b/notes/binary/integer-encoding.md index f6c94f0..c153029 100644 --- a/notes/binary/integer-encoding.md +++ b/notes/binary/integer-encoding.md @@ -822,6 +822,190 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +## Shifting + +Left shift operations (`<<`) drop the `k` most significant bits and fills the right end of the result with `k` zeros. Right shift operations (`>>`) are classified in two ways: + +* **Logical** + * Drops the `k` least significant bits and fills the left end of the result with `k` zeros. + * This mode is always used when calling `>>` on unsigned data. + * Sometimes denoted as `>>>` to disambiguate from arithmetic right shifts. +* **Arithmetic** + * Drops the `k` least significant bits and fills the left end of the result with `k` copies of the most significant bit. + * This mode is usually used when calling `>>` on signed data. + +%%ANKI +Basic +How is decimal value $2^n$ written in binary? +Back: As `1` followed by $n$ zeros. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What kinds of left shift operations are there? +Back: Just logical. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How many significant bits are dropped on a left shift by `k`? +Back: `k` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How many `0`s exist in the result of a left shift by `k`? +Back: At least `k`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What kinds of right shift operations are there? +Back: Logical and arithmetic +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What is a logical right shift operation? +Back: One that fills the left end of the result with zeros. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What is an arithmetic right shift operation? +Back: One that fills the left end of the result with copies of the most significant bit. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What kind of right shift operation is *usually* applied to signed numbers? +Back: Arithmetic. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What kind of right shift operation is applied to unsigned numbers? +Back: Logical. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What portability issue do shift operations introduce? +Back: There is no standard on whether right shifts of signed numbers are logical or arithmetic. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Cloze +{1:Arithmetic} right shifts are to {1:signed} numbers whereas {2:logical} right shifts are to {2:unsigned} numbers. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +In C, it is undefined behavior to shift by more than the width $w$ of an integral type. + +%%ANKI +Basic +Assuming two's-complement, what is the result of shifting an `int32_t` value by `32`? +Back: It is undefined behavior. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +Assuming two's-complement, what is the result of shifting an `int32_t` value by `31`? +Back: $-2^{31}$ +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +What is the result of shifting an `int32_t` value by `-1`? +Back: It is undefined behavior. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +What is the result of shifting an `uint32_t` value by `32`? +Back: It is undefined behavior. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +What is the result of shifting an `uint32_t` value by `31`? +Back: $2^{31}$ +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +What is the result of shifting an `uint32_t` value by `-1`? +Back: It is undefined behavior. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +How is $2^n$ written using bitwise shift operators? +Back: `1 << n` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +What decimal value does `1 << n` translate to? +Back: $2^n$ +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +How is $x \bmod 2^k$ equivalently written as a bit mask? +Back: `x & ((1 << k) - 1)` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + ## Arithmetic ### Addition @@ -871,8 +1055,8 @@ 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} +x + y - 2^w & \text{if } x + y \geq 2^w \\ +x + y & \text{otherwise} \end{cases}$$ This is more simply expressed as $x +_w^u y = (x + y) \bmod 2^w$. @@ -928,14 +1112,14 @@ END%% %%ANKI Cloze -Without using modulo arithmetic, $x +_w^u y =$ {$x + y$} if {$x + y < 2^w$}. +Without using modular 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. END%% %%ANKI Cloze -Without using modulo arithmetic, $x +_w^u y =$ {$x + y - 2^w$} if {$x + y \geq 2^w$}. +Without using modular 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. END%% @@ -995,7 +1179,119 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +Two's-complement addition, denoted $+_w^t$ operates similarly: + +$$x +_w^u y = \begin{cases} +x + y - 2^w & \text{if } x + y \geq 2^{w-1} \\ +x + y + 2^w & \text{if } x + y < -2^{w-1} \\ +x + y & \text{otherwise} +\end{cases}$$ + +Unlike with unsigned addition, there is no simpler modulus operation that can be applied. + +%%ANKI +Basic +What kind of overflows does two's-complement addition potentially exhibit? +Back: Positive and negative overflow. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Why is two's-complement addition overflow UB? +Back: Because the C standard does not mandate any particular signed integer encoding. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c17 + +END%% + +%%ANKI +Basic +What does $+_w^t$ denote? +Back: Two's-complement 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. + +END%% + +%%ANKI +Basic +*Why* doesn't two's-complement addition perform modular arithmetic? +Back: Because negative values are representable. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +$x +_w^t y =$ {$x + y - 2^w$} if {$x + y \geq 2^{w-1}$}. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +$x +_w^t y =$ {$x + y + 2^w$} if {$x + y < -2^{w-1}$}. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +$x +_w^t y =$ {$x + y$} if {$-2^{w-1} \leq x + y < 2^{w-1}$}. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we detect $x +_w^t y$ positive overflowed? +Back: This happens iff $x > 0$, $y > 0$, and $x +_w^t y \leq 0$. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we detect $x +_w^t y$ negative overflowed? +Back: This happens iff $x < 0$, $y < 0$, and $x +_w^t y \geq 0$. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How can we write $x +_w^t y$ in terms of unsigned addition? +Back: $x +_w^t y = U2T_w(T2U_w(x) +_w^u T2U_w(y))$ +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +*Why* are we able to characterize $+_w^t$ in terms of $+_w^u$? +Back: Because two's-complement addition has the same bit-level representation as unsigned addition. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +END%% + +%%ANKI +Basic +How would you complete the body of this function? +```c +/* Determine whether arguments can be added without overflow */ +int tadd_ok(int x, int y); +``` +Back: +```c +int pos_over = x > 0 && y > 0 && (x + y) <= 0; +int neg_over = x < 0 && y < 0 && (x + y) >= 0; +return !pos_over && !neg_over; +``` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + ## References * Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +* Ronald L. Graham, Donald Ervin Knuth, and Oren Patashnik, *Concrete Mathematics: A Foundation for Computer Science*, 2nd ed (Reading, Mass: Addison-Wesley, 1994). * “Two’s-Complement.” In *Wikipedia*, January 9, 2024. [https://en.wikipedia.org/w/index.php?title=Two%27s_complement&oldid=1194543561](https://en.wikipedia.org/w/index.php?title=Two%27s_complement&oldid=1194543561). diff --git a/notes/binary/shifts.md b/notes/binary/shifts.md deleted file mode 100644 index b0128d4..0000000 --- a/notes/binary/shifts.md +++ /dev/null @@ -1,196 +0,0 @@ ---- -title: Shifts -TARGET DECK: Obsidian::STEM -FILE TAGS: binary -tags: - - binary ---- - -## Overview - -Left shift operations (`<<`) drop the `k` most significant bits and fills the right end of the result with `k` zeros. Right shift operations (`>>`) are classified in two ways: - -* **Logical** - * Drops the `k` least significant bits and fills the left end of the result with `k` zeros. - * This mode is always used when calling `>>` on unsigned data. - * Sometimes denoted as `>>>` to disambiguate from arithmetic right shifts. -* **Arithmetic** - * Drops the `k` least significant bits and fills the left end of the result with `k` copies of the most significant bit. - * This mode is usually used when calling `>>` on signed data. - -%%ANKI -Basic -How is decimal value $2^n$ written in binary? -Back: As `1` followed by $n$ zeros. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What kinds of left shift operations are there? -Back: Just logical. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How many significant bits are dropped on a left shift by `k`? -Back: `k` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How many `0`s exist in the result of a left shift by `k`? -Back: At least `k`. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What kinds of right shift operations are there? -Back: Logical and arithmetic -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What is a logical right shift operation? -Back: One that fills the left end of the result with zeros. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What is an arithmetic right shift operation? -Back: One that fills the left end of the result with copies of the most significant bit. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What kind of right shift operation is *usually* applied to signed numbers? -Back: Arithmetic. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What kind of right shift operation is applied to unsigned numbers? -Back: Logical. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What portability issue do shift operations introduce? -Back: There is no standard on whether right shifts of signed numbers are logical or arithmetic. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Cloze -{1:Arithmetic} right shifts are to {1:signed} numbers whereas {2:logical} right shifts are to {2:unsigned} numbers. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -In C, it is undefined behavior to shift by more than the width $w$ of an integral type. - -%%ANKI -Basic -Assuming two's-complement, what is the result of shifting an `int32_t` value by `32`? -Back: It is undefined behavior. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -Assuming two's-complement, what is the result of shifting an `int32_t` value by `31`? -Back: $-2^{31}$ -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -What is the result of shifting an `int32_t` value by `-1`? -Back: It is undefined behavior. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -What is the result of shifting an `uint32_t` value by `32`? -Back: It is undefined behavior. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -What is the result of shifting an `uint32_t` value by `31`? -Back: $2^{31}$ -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -What is the result of shifting an `uint32_t` value by `-1`? -Back: It is undefined behavior. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -How is $2^n$ written using bitwise shift operators? -Back: `1 << n` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -What decimal value does `1 << n` translate to? -Back: $2^n$ -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -%%ANKI -Basic -How is $x \bmod 2^k$ equivalently written as a bit mask? -Back: `x & ((1 << k) - 1)` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -Tags: c17 - -END%% - -## References - -* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. -* Ronald L. Graham, Donald Ervin Knuth, and Oren Patashnik, *Concrete Mathematics: A Foundation for Computer Science*, 2nd ed (Reading, Mass: Addison-Wesley, 1994).