From a42ccc880a7c408ec7f87f6eceeaf9d435e68cc4 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Thu, 22 Feb 2024 14:21:08 -0700 Subject: [PATCH] More integer encoding notes. --- .../plugins/obsidian-to-anki-plugin/data.json | 12 +- notes/_journal/2024-02-21.md | 15 - notes/_journal/2024-02-22.md | 19 + notes/_journal/2024-02/2024-02-21.md | 20 + notes/algebra/radices.md | 18 + notes/binary/integer-encoding.md | 372 +++++++++++------- notes/binary/shifts.md | 4 +- notes/c/declarations.md | 149 +++++++ notes/logic/equiv-trans.md | 2 +- 9 files changed, 445 insertions(+), 166 deletions(-) delete mode 100644 notes/_journal/2024-02-21.md create mode 100644 notes/_journal/2024-02-22.md create mode 100644 notes/_journal/2024-02/2024-02-21.md diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index 33c5b9a..77da5cd 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -118,7 +118,7 @@ "_journal/2024-02-02.md": "a3b222daee8a50bce4cbac699efc7180", "_journal/2024-02-01.md": "3aa232387d2dc662384976fd116888eb", "_journal/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970", - "logic/equiv-trans.md": "4d825c23bf54e8b1e645584d52b2b993", + "logic/equiv-trans.md": "08ad9c814f0a46981b218a9c7cc8a3b8", "_journal/2024-02-07.md": "8d81cd56a3b33883a7706d32e77b5889", "algorithms/loop-invariants.md": "cbefc346842c21a6cce5c5edce451eb2", "algorithms/loop-invariant.md": "d883dfc997ee28a7a1e24b995377792b", @@ -132,7 +132,7 @@ "_journal/2024-02-09.md": "a798d35f0b2bd1da130f7ac766166109", "c/types.md": "cf3e66e5aee58a94db3fdf0783908555", "logic/quantification.md": "5d7579a511e9ff683edeec62bcc291b8", - "c/declarations.md": "7b0277d89b83fb330970deeb37e664c4", + "c/declarations.md": "94c85cb1a09efed2cad6b5b80e9d0be3", "algorithms/sorting/bubble-sort.md": "16dad1016dc6555163e42ba20f1d152d", "_journal/2024-02-10.md": "562b01f60ea36a3c78181e39b1c02b9f", "_journal/2024-01/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970", @@ -159,7 +159,7 @@ "logic/boolean-algebra.md": "f9101b2dfdedb73dc13c34c1a70a0010", "_journal/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0", "_journal/2024-02/2024-02-12.md": "618c0035a69b48227119379236a02f44", - "binary/shifts.md": "c7fb2a5722a69bb23047b603edcd0b91", + "binary/shifts.md": "ce6f2d80536b8a8b3f05383ce2190e72", "_journal/2024-02-14.md": "76d1b607470305fb3f00a47b8e9ece27", "_journal/2024-02/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0", "_journal/2024-02-15.md": "575ba46d692795d9606de9e635d1f4ac", @@ -171,7 +171,7 @@ "algorithms/binary-search.md": "08cb6dc2dfb204a665d8e8333def20ca", "_journal/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048", "_journal/2024-02/2024-02-16.md": "e701902e369ec53098fc2deed4ec14fd", - "binary/integer-encoding.md": "193566bf1b9e88257002d32bb2bc0bf2", + "binary/integer-encoding.md": "afe5e82b534fc5ab409b188addab36f4", "combinatorics/index.md": "f9de9671fdb6068ef2bb5e63051734be", "_journal/2024-02-18.md": "67e36dbbb2cac699d4533b5a2eaeb629", "_journal/2024-02/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048", @@ -186,7 +186,9 @@ "combinatorics/inclusion-exclusion.md": "4d5ba716bc90cd378c7c4c816b224c75", "_journal/2024-02-21.md": "b9d944ecebe625da5dd72aeea6a916a2", "_journal/2024-02/2024-02-20.md": "af2ef10727726200c4defe2eafc7d841", - "algebra/radices.md": "1171835a864103fbc4cbe21d56fcfa1f" + "algebra/radices.md": "0fcd901c798eaed8075ff1375e2429dd", + "_journal/2024-02-22.md": "e01f1d4bd2f7ac2a667cdfd500885a2a", + "_journal/2024-02/2024-02-21.md": "a627f267700f36abd5ceb9b4c294479e" }, "fields_dict": { "Basic": [ diff --git a/notes/_journal/2024-02-21.md b/notes/_journal/2024-02-21.md deleted file mode 100644 index e948404..0000000 --- a/notes/_journal/2024-02-21.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -title: "2024-02-21" ---- - -- [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) - -* Read through "Stars and Bars" section in "Discrete Mathematics: An Open Introduction". -* Added notes on radices. -* Spent time thinking about how two's-complement works. \ No newline at end of file diff --git a/notes/_journal/2024-02-22.md b/notes/_journal/2024-02-22.md new file mode 100644 index 0000000..ccbf315 --- /dev/null +++ b/notes/_journal/2024-02-22.md @@ -0,0 +1,19 @@ +--- +title: "2024-02-22" +--- + +- [x] Anki Flashcards +- [x] KoL +- [ ] Sheet Music (10 min.) +- [ ] OGS (1 Life & Death Problem) +- [ ] Korean (Read 1 Story) +- [x] Interview Prep (1 Practice Problem) +- [x] Log Work Hours (Max 3 hours) + +* Briefly discussed first chapter of "Designing Data-Intensive Applications" with Mike. +* Continued reading more on integer encodings. + * Focused on conversions between unsigned encoding and two's-complement, though still need to create flashcards for these. +* Did Leetcode problems + * [3Sum Closest](https://leetcode.com/problems/3sum-closest/description/) + * [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) + * [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) \ No newline at end of file diff --git a/notes/_journal/2024-02/2024-02-21.md b/notes/_journal/2024-02/2024-02-21.md new file mode 100644 index 0000000..636c851 --- /dev/null +++ b/notes/_journal/2024-02/2024-02-21.md @@ -0,0 +1,20 @@ +--- +title: "2024-02-21" +--- + +- [x] Anki Flashcards +- [x] KoL +- [ ] Sheet Music (10 min.) +- [ ] OGS (1 Life & Death Problem) +- [ ] Korean (Read 1 Story) +- [x] Interview Prep (1 Practice Problem) +- [ ] Log Work Hours (Max 3 hours) + +* Read through "Stars and Bars" section in "Discrete Mathematics: An Open Introduction". +* Added notes on radices. +* Spent time thinking about how two's-complement works. +* Read the first chapter on "Designing Data-Intensive Applications". +* Leetcode Problems + * [Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/) + * [Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/) + * [Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/) \ No newline at end of file diff --git a/notes/algebra/radices.md b/notes/algebra/radices.md index 1ed56f5..2eee839 100644 --- a/notes/algebra/radices.md +++ b/notes/algebra/radices.md @@ -267,6 +267,24 @@ Tags: binary::hex END%% +%%ANKI +Basic +Which hexadecimal digits have a leading `1` bit? +Back: `8` through `F` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: binary::hex + +END%% + +%%ANKI +Basic +Which hexadecimal digits have a leading `0` bit? +Back: `0` through `7` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: binary::hex + +END%% + ## References * Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. diff --git a/notes/binary/integer-encoding.md b/notes/binary/integer-encoding.md index 9cac23b..642f4c7 100644 --- a/notes/binary/integer-encoding.md +++ b/notes/binary/integer-encoding.md @@ -54,7 +54,7 @@ END%% %%ANKI Basic -An integral value of 0 has what encoding? +An integral value of $0_{10}$ likely has what encoding? Back: Either unsigned or two's-complement. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. @@ -62,7 +62,7 @@ END%% %%ANKI Basic -An integral value of 100 has what encoding? +An integral value of $100_{10}$ likely has what encoding? Back: Either unsigned or two's-complement. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. @@ -70,7 +70,7 @@ END%% %%ANKI Basic -An integral value of -100 has what encoding? +An integral value of $-100_{10}$ likely has what encoding? Back: Two's-complement. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. @@ -87,7 +87,7 @@ END%% %%ANKI Basic What integral values share the same binary representation in unsigned encoding and two's-complement? -Back: Nonnegative values $\leq |TMax|$. +Back: Nonnegative values $\leq TMax$. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. END%% @@ -108,9 +108,83 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +%%ANKI +Basic +According to the C standard, Is `unsigned` underflow/overflow safe? +Back: Yes +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 +According to the C standard, Is `signed` underflow/overflow safe? +Back: No +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 `signed` underflow/overflow considered UB? +Back: Because there is no requirement on how `signed` integers are encoded. +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 does $UMax$ relate to $TMax$? +Back: $UMax = 2 \cdot TMax + 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 +*Why* is it $UMax = 2 \cdot TMax + 1$? +Back: All bit patterns denoting negative numbers in two's-complement are positive in unsigned encoding. +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 are the binary encodings of $UMax_4$ and $TMax_4$? +Back: $1111_2$ and $0111_2$ +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 +Reinterpret $TMax$ in unsigned encoding. What arithmetic operations yield $UMax$? +Back: Multiply by two and add one. +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 +Reinterpret $TMax$ in unsigned encoding. What bitwise operations yield $UMax$? +Back: One-bit left shift and add one. +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 +Reinterpret $UMax$ in two's-complement. What decimal value do you have? +Back: $-1$ +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + ### Unsigned Encoding -Always represents nonnegative numbers. Given an integral type $\vec{x}$ of $w$ bits, we convert binary to its unsigned encoding with: $$B2U_w(\vec{x}) = \sum_{i=0}^{w-1} 2^ix_i$$ +Always represents nonnegative numbers. Given an integral type $\vec{x}$ of $w$ bits, we convert binary to its unsigned encoding with: $$B2U_w(\vec{x}) = 2^{w-1}x_{w-1} + \sum_{i=0}^{w-2} 2^ix_i$$ + +Note we unfold the summation on the RHS by one term to make it's relationship to $T2U_w$ clearer. %%ANKI Basic @@ -171,7 +245,7 @@ END%% %%ANKI Basic How does Bryant et al. define $B2U_w$? -Back: $B2U_w(\vec{x}) = \sum_{i=0}^{w-1} 2^ix_i$ +Back: $B2U_w(\vec{x}) = 2^{w-1}x_{w-1} + \sum_{i=0}^{w-2} 2^ix_i$ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. END%% @@ -179,11 +253,19 @@ END%% %%ANKI Basic What is $B2U_w$ an acronym for? -Back: **B**inary "to" **u**nsigned. +Back: **B**inary to **u**nsigned, width $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 +Basic +What is $U2B_w$ an acronym for? +Back: **U**nsigned to **b**inary, width $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 Basic What does $w$ in $B2U_w$ represent? @@ -195,11 +277,19 @@ END%% %%ANKI Basic What is the domain of $B2U_w$? -Back: Bit vectors of size $w$. +Back: Bit strings of size $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 +Basic +What is the domain of $U2B_w$? +Back: $[0, 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 Basic What is the range of $B2U_w$? @@ -210,10 +300,10 @@ END%% %%ANKI Basic -What is the hexadecimal representation of $UMin_4$? -Back: `0x0000` +What is the range of $U2B_w$? +Back: Bit strings of length $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 @@ -224,14 +314,6 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% -%%ANKI -Basic -What is the hexadecimal representation of $UMax_4$? -Back: `0xFFFF` -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 is the largest unsigned integer formatted in hexadecimal? @@ -250,7 +332,7 @@ END%% %%ANKI Basic -With unsigned encoding, *why* does `n + ~n = UMax`? +Using unsigned encoding, *why* does `n + ~n = UMax`? Back: Because this always yields a bit string of all `1`s. Reference: “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). @@ -284,6 +366,13 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +%%ANKI +Basic +How do $TMin$ and $TMax$ relate to one another? +Back: $TMin = -TMax - 1$ + +END%% + %%ANKI Basic What half-open interval represents the possible $w$-bit two's-complement decimal values? @@ -326,7 +415,6 @@ END%% Basic What is the weight of the sign bit in $w$-bit two's-complement? Back: $-2^{w-1}$ -The {sign bit} refers to the {most significant bit} in two's-complement. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. END%% @@ -350,11 +438,19 @@ END%% %%ANKI Basic What is $B2T_w$ an acronym for? -Back: **B**inary "to" **t**wo's-complement. +Back: **B**inary to **t**wo's-complement, width $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 +Basic +What is $T2B_w$ an acronym for? +Back: **T**wo's-complement to **b**inary, width $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 Basic What does $w$ in $B2T_w$ represent? @@ -366,11 +462,19 @@ END%% %%ANKI Basic What is the domain of $B2T_w$? -Back: Bit vectors of size $w$. +Back: Bit strings of size $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 +Basic +What is the domain of $T2B_w$? +Back: $[-2^{w-1}, 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 What is the range of $B2T_w$? @@ -381,10 +485,10 @@ END%% %%ANKI Basic -What is the hexadecimal representation of $TMin_4$? -Back: `0x8000` +What is the range of $T2B_w$? +Back: Bit strings of length $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 @@ -395,14 +499,6 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% -%%ANKI -Basic -What is the hexadecimal representation of $TMax_4$? -Back: `0x7FFF` -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 is the largest two's-complement integer formatted in hexadecimal? @@ -429,54 +525,12 @@ END%% %%ANKI Basic -Why is two's-complement encoding's range asymmetric? -Back: Leading `1`s correspond to negatives but leading `0`s corerspond to nonnegative numbers (which include $0$.) +Why is two's-complement's encoding range asymmetric? +Back: Leading `1`s correspond to negatives but leading `0`s corerspond to nonnegative numbers (which include $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 does $UMax$ relate to $TMax$? -Back: $|UMax| = 2|TMax| + 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 -What are the binary encodings of $UMax_4$ and $TMax_4$? -Back: $1111_2$ and $0111_2$ -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 -Reinterpret $TMax$ in unsigned encoding. What arithmetic operations yield $UMax$? -Back: Multiply by two and add one. -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 -Reinterpret $TMax$ in unsigned encoding. What bitwise operations yield $UMax$? -Back: One-bit left shift and add one. -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 -Reinterpret $UMax$ in two's-complement. What decimal value do you have? -Back: $-1$ -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -The "two's-complement" of an integer often refers to the binary representation of the integer's additive inverse. For example, $0110_2 = 6$ has two's-complement $1010_2 = -6$. In contrast, the "complement" of a nonnegative integer is the other nonnegative integer such that together add up to $2^{w-1}$. For example, $0110_2 = 6$ has complement $0010_2 = 2$ (with respect to $2^3 = 8$). - %%ANKI Basic What are the median values of two's-complement's encoding range? @@ -492,90 +546,122 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +## Casting + +Most implementations of C cast an object of one type to another by simply re-interpreting their binary representation. This casting may happen implicitly if comparing or operating on e.g. `signed` and `unsigned` objects in the same expression. $T2U$ and $U2T$ reflect this method of casting: + +$$T2U_w(x) = \begin{cases} +x + 2^w & x < 0 \\ +x & x \geq 0 +\end{cases}$$ + +$$U2T_w(x) = \begin{cases} +x & x \leq TMax_w \\ +x - 2^w & x > TMax_w +\end{cases}$$ + %%ANKI Basic -How does `n` relate to `~n` in two's-complement? -Back: `~n = -n - 1` -Reference: “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). - +How do most implementations of C perform casting? +Back: As a reinterpretation of the same byte pattern of the object being casted. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c + END%% %%ANKI Basic -What value does not have a two's-complement? -Back: $TMin$ -Reference: “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). - +What is $T2U_w$ an acronym for? +Back: **T**wo's-complement to **u**nsigned, width $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 Basic -*Why* doesn't $TMin$ have a two's-complement? -Back: Because there is one less representable positive number than negative number. -Reference: “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). - +For what decimal values $x$ does $T2U_w$ and $U2T_w$ serve as the identity function? +Back: $0 \leq x \leq TMax_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 +Basic +What values $x$ are unaffected when casting from `signed` to `unsigned`? +Back: $0 \leq x \leq TMax_w$ +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c + +END%% + +%%ANKI +Basic +What values $x$ are unaffected when casting from `unsigned` to `signed`? +Back: $0 \leq x \leq TMax_w$ +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c + +END%% + +%%ANKI +Basic +How are casts implicitly performed in operations containing `signed` and `unsigned` objects? +Back: `signed` objects are cast to `unsigned` objects. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Tags: c + END%% %%ANKI Cloze -$TMin$ is called the "{weird number}" because {it is it's own two's-complement}. -Reference: “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). -END%% - -%%ANKI -Basic -What is the result of applying the two's-complement operation on $TMin$? -Back: $TMin$ -Reference: “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). - -END%% - -%%ANKI -Basic -What does the two's-complement of $n$ refer to? -Back: The binary representation of $-n$. -Reference: “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). - -END%% - -%%ANKI -Basic -Using two's-complement, what is the *complement* of $0101_2$? -Back: $0011_2$ -Reference: “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). - -END%% - -%%ANKI -Basic -Using two's-complement, the *complement* of $0101_2$ is found with respect to what value? -Back: $2^3 = 8$ -Reference: “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). - -END%% - -%%ANKI -Basic -What bitwise operations yield the two's-complement of $n$? -Back: `~n + 1` -Reference: “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). - -END%% - -%%ANKI -Basic -Why is two's-complement named the way it is? -Back: For $x \geq 0$, $-x$'s $w$-bit representation is found using $2^w - x$. +For {$x < 0$}, $T2U_w(x) =$ {$x + 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 +For {$x \geq 0$}, $T2U_w(x) =$ {$x$}. +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 identity show $3$ and $5$ are complements with respect to $2^3$? -Back: $3 + 5 = 8$ -Reference: “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). - +How is $T2U_w$ written as a function composition? +Back: $T2U_w = B2U_w \circ T2B_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 +Basic +What is $U2T_w$ an acronym for? +Back: **U**nsigned to **t**wo's-complement, width $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 +Basic +How is $U2T_w$ written as a function composition? +Back: $U2T_w = B2T_w \circ U2B_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 +For {$x > TMax_w$}, $U2T_w(x) =$ {$x - 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 +For {$x \leq TMax_w$}, $U2T_w(x) =$ {$x$}. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + END%% ## References diff --git a/notes/binary/shifts.md b/notes/binary/shifts.md index 9d3efee..cfb0b32 100644 --- a/notes/binary/shifts.md +++ b/notes/binary/shifts.md @@ -45,7 +45,7 @@ END%% %%ANKI Basic What is a logical right shift operation? -Back: One that fills the left end of the result with `k` zeros. +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%% @@ -53,7 +53,7 @@ END%% %%ANKI Basic What is an arithmetic right shift operation? -Back: One that fills the left end of the result with `k` copies of the most significant bit. +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%% diff --git a/notes/c/declarations.md b/notes/c/declarations.md index 156558c..9d1f6b8 100644 --- a/notes/c/declarations.md +++ b/notes/c/declarations.md @@ -28,6 +28,14 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +%%ANKI +Basic +Which header file contains `INT32_MAX`? +Back: `` +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 does the "width" of an integer type refer to? @@ -195,6 +203,147 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +## Integer Literals + +Negative integer literals are typed in a counterintuitive way. When the compiler sees a number of form `-X`, the type of `X` is first determined *before* then being negated. Promotion rules are as follows: + +C90 (Decimal) | C90 (Other) | C99 (Decimal) | C99 (Other) +--------------- | --------------- | ------------- | --------- +`int` | `int` | `int` | `int` +`long` | `unsigned` | `long` | `unsigned` +`unsigned` | `long` | `long long` | `long` +`unsigned long` | `unsigned long` | `-` | `unsigned long` +`-` | `-` | `-` | `long long` +`-` | `-` | `-` | `unsigned long long` + +%%ANKI +Basic +How does the compiler process integer literal `-X`? +Back: By first determining the type of `X` and then negating the value. +Reference: 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 simplification did C99 introduce to decimal integer literals? +Back: The integer constant is guaranteed a `signed` type. +Reference: 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 +Since what standard was it guaranteed decimal integer literals were `signed`? +Back: C99 +Reference: 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 +In ISO C90, what integer literals are guaranteed `signed`? +Back: None. +Reference: 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 +In ISO C99, what integer literals are guaranteed `signed`? +Back: Decimal integer constants. +Reference: 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 avoid negative octal integer literals? +Back: Depending on value, the resulting type may be `unsigned`. +Reference: 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 avoid negative hexadecimal integer literals? +Back: Depending on value, the resulting type may be `unsigned`. +Reference: 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 +Which header file contains `INT_MAX`? +Back: `` +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 +{`INT_MAX`} is to `signed` whereas {`UINT_MAX`} is to `unsigned`. +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 does `` define `INT_MIN`? +Back: As `(-INT_MAX - 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 +*Why* is `INT_MIN` defined as `(-INT_MAX - 1)` instead of directly as e.g. `-2147483648`? +Back: Because `2147483648` (without `-`) would be sized as a non-`int` before being negated. +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 +`INT_MAX` is to {``} whereas `INT32_MAX` is to {``}. +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 suffix can be used to denote an `unsigned` integer literal? +Back: Case-insensitive `U`. +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 suffix can be used to denote a `long` integer literal? +Back: Case-insensitive `L`. +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 suffix can be used to denote a `long long` integer literal? +Back: Case-insensitive `LL`. +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 suffix can be used to denote an `unsigned long long` integer literal? +Back: Case-insensitive `ULL`. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +## Pointers + Pointers have the same size as the machine's word size since it should be able to refer to any virtual address. %%ANKI diff --git a/notes/logic/equiv-trans.md b/notes/logic/equiv-trans.md index e4f9fe2..151ac7e 100644 --- a/notes/logic/equiv-trans.md +++ b/notes/logic/equiv-trans.md @@ -788,7 +788,7 @@ END%% %%ANKI Basic How is $(s; x{:}v)$ written instead using set-theoretical notation? -Back: $(s - \{(x, s(x))\}) \cup \{(x, v)\}$ +Back: $(s - \{\langle x, s(x) \rangle\}) \cup \{\langle x, v \rangle\}$ Reference: Gries, David. *The Science of Programming*. Texts and Monographs in Computer Science. New York: Springer-Verlag, 1981. END%%