More integer encoding notes.

c-declarations
Joshua Potter 2024-02-22 14:21:08 -07:00
parent 2538533aee
commit a42ccc880a
9 changed files with 445 additions and 166 deletions

View File

@ -118,7 +118,7 @@
"_journal/2024-02-02.md": "a3b222daee8a50bce4cbac699efc7180", "_journal/2024-02-02.md": "a3b222daee8a50bce4cbac699efc7180",
"_journal/2024-02-01.md": "3aa232387d2dc662384976fd116888eb", "_journal/2024-02-01.md": "3aa232387d2dc662384976fd116888eb",
"_journal/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970", "_journal/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970",
"logic/equiv-trans.md": "4d825c23bf54e8b1e645584d52b2b993", "logic/equiv-trans.md": "08ad9c814f0a46981b218a9c7cc8a3b8",
"_journal/2024-02-07.md": "8d81cd56a3b33883a7706d32e77b5889", "_journal/2024-02-07.md": "8d81cd56a3b33883a7706d32e77b5889",
"algorithms/loop-invariants.md": "cbefc346842c21a6cce5c5edce451eb2", "algorithms/loop-invariants.md": "cbefc346842c21a6cce5c5edce451eb2",
"algorithms/loop-invariant.md": "d883dfc997ee28a7a1e24b995377792b", "algorithms/loop-invariant.md": "d883dfc997ee28a7a1e24b995377792b",
@ -132,7 +132,7 @@
"_journal/2024-02-09.md": "a798d35f0b2bd1da130f7ac766166109", "_journal/2024-02-09.md": "a798d35f0b2bd1da130f7ac766166109",
"c/types.md": "cf3e66e5aee58a94db3fdf0783908555", "c/types.md": "cf3e66e5aee58a94db3fdf0783908555",
"logic/quantification.md": "5d7579a511e9ff683edeec62bcc291b8", "logic/quantification.md": "5d7579a511e9ff683edeec62bcc291b8",
"c/declarations.md": "7b0277d89b83fb330970deeb37e664c4", "c/declarations.md": "94c85cb1a09efed2cad6b5b80e9d0be3",
"algorithms/sorting/bubble-sort.md": "16dad1016dc6555163e42ba20f1d152d", "algorithms/sorting/bubble-sort.md": "16dad1016dc6555163e42ba20f1d152d",
"_journal/2024-02-10.md": "562b01f60ea36a3c78181e39b1c02b9f", "_journal/2024-02-10.md": "562b01f60ea36a3c78181e39b1c02b9f",
"_journal/2024-01/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970", "_journal/2024-01/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970",
@ -159,7 +159,7 @@
"logic/boolean-algebra.md": "f9101b2dfdedb73dc13c34c1a70a0010", "logic/boolean-algebra.md": "f9101b2dfdedb73dc13c34c1a70a0010",
"_journal/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0", "_journal/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0",
"_journal/2024-02/2024-02-12.md": "618c0035a69b48227119379236a02f44", "_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-14.md": "76d1b607470305fb3f00a47b8e9ece27",
"_journal/2024-02/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0", "_journal/2024-02/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0",
"_journal/2024-02-15.md": "575ba46d692795d9606de9e635d1f4ac", "_journal/2024-02-15.md": "575ba46d692795d9606de9e635d1f4ac",
@ -171,7 +171,7 @@
"algorithms/binary-search.md": "08cb6dc2dfb204a665d8e8333def20ca", "algorithms/binary-search.md": "08cb6dc2dfb204a665d8e8333def20ca",
"_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": "193566bf1b9e88257002d32bb2bc0bf2", "binary/integer-encoding.md": "afe5e82b534fc5ab409b188addab36f4",
"combinatorics/index.md": "f9de9671fdb6068ef2bb5e63051734be", "combinatorics/index.md": "f9de9671fdb6068ef2bb5e63051734be",
"_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",
@ -186,7 +186,9 @@
"combinatorics/inclusion-exclusion.md": "4d5ba716bc90cd378c7c4c816b224c75", "combinatorics/inclusion-exclusion.md": "4d5ba716bc90cd378c7c4c816b224c75",
"_journal/2024-02-21.md": "b9d944ecebe625da5dd72aeea6a916a2", "_journal/2024-02-21.md": "b9d944ecebe625da5dd72aeea6a916a2",
"_journal/2024-02/2024-02-20.md": "af2ef10727726200c4defe2eafc7d841", "_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": { "fields_dict": {
"Basic": [ "Basic": [

View File

@ -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.

View File

@ -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/)

View File

@ -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/)

View File

@ -267,6 +267,24 @@ Tags: binary::hex
<!--ID: 1707432641596--> <!--ID: 1707432641596-->
END%% 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
<!--ID: 1708631918825-->
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
<!--ID: 1708631918829-->
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.

View File

@ -54,7 +54,7 @@ END%%
%%ANKI %%ANKI
Basic 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. 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708177246105--> <!--ID: 1708177246105-->
@ -62,7 +62,7 @@ END%%
%%ANKI %%ANKI
Basic 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. 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708177246109--> <!--ID: 1708177246109-->
@ -70,7 +70,7 @@ END%%
%%ANKI %%ANKI
Basic Basic
An integral value of -100 has what encoding? An integral value of $-100_{10}$ likely has what encoding?
Back: Two's-complement. 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708177246114--> <!--ID: 1708177246114-->
@ -87,7 +87,7 @@ END%%
%%ANKI %%ANKI
Basic Basic
What integral values share the same binary representation in unsigned encoding and two's-complement? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708454709515--> <!--ID: 1708454709515-->
END%% END%%
@ -108,9 +108,83 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1708455064696--> <!--ID: 1708455064696-->
END%% 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.
<!--ID: 1708551236389-->
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.
<!--ID: 1708551236392-->
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.
<!--ID: 1708551236395-->
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.
<!--ID: 1708453398445-->
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.
<!--ID: 1708613447880-->
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.
<!--ID: 1708453398449-->
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.
<!--ID: 1708453398454-->
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.
<!--ID: 1708453398459-->
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.
<!--ID: 1708453398469-->
END%%
### Unsigned Encoding ### 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 %%ANKI
Basic Basic
@ -171,7 +245,7 @@ END%%
%%ANKI %%ANKI
Basic Basic
How does Bryant et al. define $B2U_w$? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179147785--> <!--ID: 1708179147785-->
END%% END%%
@ -179,11 +253,19 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is $B2U_w$ an acronym for? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179147791--> <!--ID: 1708179147791-->
END%% 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.
<!--ID: 1708613447885-->
END%%
%%ANKI %%ANKI
Basic Basic
What does $w$ in $B2U_w$ represent? What does $w$ in $B2U_w$ represent?
@ -195,11 +277,19 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is the domain of $B2U_w$? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179147798--> <!--ID: 1708179147798-->
END%% 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.
<!--ID: 1708613447888-->
END%%
%%ANKI %%ANKI
Basic Basic
What is the range of $B2U_w$? What is the range of $B2U_w$?
@ -210,10 +300,10 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is the hexadecimal representation of $UMin_4$? What is the range of $U2B_w$?
Back: `0x0000` 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398386--> <!--ID: 1708613447891-->
END%% END%%
%%ANKI %%ANKI
@ -224,14 +314,6 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1708453398392--> <!--ID: 1708453398392-->
END%% 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.
<!--ID: 1708453398397-->
END%%
%%ANKI %%ANKI
Basic Basic
How is the largest unsigned integer formatted in hexadecimal? How is the largest unsigned integer formatted in hexadecimal?
@ -250,7 +332,7 @@ END%%
%%ANKI %%ANKI
Basic 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. Back: Because this always yields a bit string of all `1`s.
Reference: “Twos-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). Reference: “Twos-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).
<!--ID: 1708545574154--> <!--ID: 1708545574154-->
@ -284,6 +366,13 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1708545383255--> <!--ID: 1708545383255-->
END%% END%%
%%ANKI
Basic
How do $TMin$ and $TMax$ relate to one another?
Back: $TMin = -TMax - 1$
<!--ID: 1708609869518-->
END%%
%%ANKI %%ANKI
Basic Basic
What half-open interval represents the possible $w$-bit two's-complement decimal values? What half-open interval represents the possible $w$-bit two's-complement decimal values?
@ -326,7 +415,6 @@ END%%
Basic Basic
What is the weight of the sign bit in $w$-bit two's-complement? What is the weight of the sign bit in $w$-bit two's-complement?
Back: $-2^{w-1}$ 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179649887--> <!--ID: 1708179649887-->
END%% END%%
@ -350,11 +438,19 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is $B2T_w$ an acronym for? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179649907--> <!--ID: 1708179649907-->
END%% 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.
<!--ID: 1708613447895-->
END%%
%%ANKI %%ANKI
Basic Basic
What does $w$ in $B2T_w$ represent? What does $w$ in $B2T_w$ represent?
@ -366,11 +462,19 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is the domain of $B2T_w$? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179649921--> <!--ID: 1708179649921-->
END%% 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.
<!--ID: 1708613447899-->
END%%
%%ANKI %%ANKI
Basic Basic
What is the range of $B2T_w$? What is the range of $B2T_w$?
@ -381,10 +485,10 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is the hexadecimal representation of $TMin_4$? What is the range of $T2B_w$?
Back: `0x8000` 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398408--> <!--ID: 1708613447903-->
END%% END%%
%%ANKI %%ANKI
@ -395,14 +499,6 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1708453398413--> <!--ID: 1708453398413-->
END%% 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.
<!--ID: 1708453398418-->
END%%
%%ANKI %%ANKI
Basic Basic
How is the largest two's-complement integer formatted in hexadecimal? How is the largest two's-complement integer formatted in hexadecimal?
@ -429,54 +525,12 @@ END%%
%%ANKI %%ANKI
Basic Basic
Why is two's-complement encoding's range asymmetric? 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$.) 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398440--> <!--ID: 1708453398440-->
END%% 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.
<!--ID: 1708453398445-->
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.
<!--ID: 1708453398449-->
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.
<!--ID: 1708453398454-->
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.
<!--ID: 1708453398459-->
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.
<!--ID: 1708453398469-->
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 %%ANKI
Basic Basic
What are the median values of two's-complement's encoding range? 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
<!--ID: 1708545383265--> <!--ID: 1708545383265-->
END%% 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 %%ANKI
Basic Basic
How does `n` relate to `~n` in two's-complement? How do most implementations of C perform casting?
Back: `~n = -n - 1` Back: As a reinterpretation of the same byte pattern of the object being casted.
Reference: “Twos-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). Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708545383267--> Tags: c
<!--ID: 1708615249879-->
END%% END%%
%%ANKI %%ANKI
Basic Basic
What value does not have a two's-complement? What is $T2U_w$ an acronym for?
Back: $TMin$ Back: **T**wo's-complement to **u**nsigned, width $w$.
Reference: “Twos-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). Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708545383268--> <!--ID: 1708615249883-->
END%% END%%
%%ANKI %%ANKI
Basic Basic
*Why* doesn't $TMin$ have a two's-complement? For what decimal values $x$ does $T2U_w$ and $U2T_w$ serve as the identity function?
Back: Because there is one less representable positive number than negative number. Back: $0 \leq x \leq TMax_w$
Reference: “Twos-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). Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708545383270--> <!--ID: 1708615249887-->
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
<!--ID: 1708615249891-->
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
<!--ID: 1708615249897-->
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
<!--ID: 1708615249903-->
END%% END%%
%%ANKI %%ANKI
Cloze Cloze
$TMin$ is called the "{weird number}" because {it is it's own two's-complement}. For {$x < 0$}, $T2U_w(x) =$ {$x + 2^w$}.
Reference: “Twos-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: “Twos-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).
<!--ID: 1708545383271-->
END%%
%%ANKI
Basic
What does the two's-complement of $n$ refer to?
Back: The binary representation of $-n$.
Reference: “Twos-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).
<!--ID: 1708545383273-->
END%%
%%ANKI
Basic
Using two's-complement, what is the *complement* of $0101_2$?
Back: $0011_2$
Reference: “Twos-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).
<!--ID: 1708545383274-->
END%%
%%ANKI
Basic
Using two's-complement, the *complement* of $0101_2$ is found with respect to what value?
Back: $2^3 = 8$
Reference: “Twos-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).
<!--ID: 1708545383276-->
END%%
%%ANKI
Basic
What bitwise operations yield the two's-complement of $n$?
Back: `~n + 1`
Reference: “Twos-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).
<!--ID: 1708545383277-->
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$.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708545383279--> <!--ID: 1708615249908-->
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.
<!--ID: 1708615249914-->
END%% END%%
%%ANKI %%ANKI
Basic Basic
What identity show $3$ and $5$ are complements with respect to $2^3$? How is $T2U_w$ written as a function composition?
Back: $3 + 5 = 8$ Back: $T2U_w = B2U_w \circ T2B_w$
Reference: “Twos-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). Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708545383280--> <!--ID: 1708615249920-->
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.
<!--ID: 1708615249925-->
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.
<!--ID: 1708615249930-->
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.
<!--ID: 1708615249935-->
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.
<!--ID: 1708615249939-->
END%% END%%
## References ## References

View File

@ -45,7 +45,7 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is a logical right shift operation? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707854589786--> <!--ID: 1707854589786-->
END%% END%%
@ -53,7 +53,7 @@ END%%
%%ANKI %%ANKI
Basic Basic
What is an arithmetic right shift operation? 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707854589789--> <!--ID: 1707854589789-->
END%% END%%

View File

@ -28,6 +28,14 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1707835869728--> <!--ID: 1707835869728-->
END%% END%%
%%ANKI
Basic
Which header file contains `INT32_MAX`?
Back: `<stdint.h>`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708615249870-->
END%%
%%ANKI %%ANKI
Basic Basic
What does the "width" of an integer type refer to? 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
<!--ID: 1707493017244--> <!--ID: 1707493017244-->
END%% 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.
<!--ID: 1708631820805-->
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.
<!--ID: 1708631820816-->
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.
<!--ID: 1708631820820-->
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.
<!--ID: 1708631820823-->
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.
<!--ID: 1708631820826-->
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.
<!--ID: 1708631820829-->
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.
<!--ID: 1708631820833-->
END%%
%%ANKI
Basic
Which header file contains `INT_MAX`?
Back: `<limits.h>`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708615249864-->
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.
<!--ID: 1708631820837-->
END%%
%%ANKI
Basic
How does `<limits.h>` 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.
<!--ID: 1708631820840-->
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.
<!--ID: 1708631820843-->
END%%
%%ANKI
Cloze
`INT_MAX` is to {`<limits.h>`} whereas `INT32_MAX` is to {`<stdint.h>`}.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708615249873-->
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.
<!--ID: 1708615249876-->
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.
<!--ID: 1708631820847-->
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.
<!--ID: 1708631820850-->
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.
<!--ID: 1708631820856-->
END%%
## Pointers
Pointers have the same size as the machine's word size since it should be able to refer to any virtual address. Pointers have the same size as the machine's word size since it should be able to refer to any virtual address.
%%ANKI %%ANKI

View File

@ -788,7 +788,7 @@ END%%
%%ANKI %%ANKI
Basic Basic
How is $(s; x{:}v)$ written instead using set-theoretical notation? 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. Reference: Gries, David. *The Science of Programming*. Texts and Monographs in Computer Science. New York: Springer-Verlag, 1981.
<!--ID: 1707937867053--> <!--ID: 1707937867053-->
END%% END%%