Encoding and inclusion/exclusion principle.

c-declarations
Joshua Potter 2024-02-20 16:50:26 -07:00
parent 8d6978c429
commit 886fb34762
7 changed files with 305 additions and 18 deletions

View File

@ -153,7 +153,7 @@
"_journal/2024-02/2024-02-11.md": "afee9f502b61e17de231cf2f824fbb32",
"encoding/ascii.md": "c01e50f96d0493d94dc4d520c0b6bb71",
"encoding/index.md": "071cfa6a5152efeda127b684f420d438",
"c/strings.md": "d3a5405f5d5237ee169a8894ff4e21ab",
"c/strings.md": "04b04e2c227e600adb48c74b2dee62b1",
"logic/truth-tables.md": "7892ceaa416c9a65acc79ca1e6ff778f",
"logic/short-circuit.md": "26d300f407f14883022d0ef8dc4f7300",
"logic/boolean-algebra.md": "f9101b2dfdedb73dc13c34c1a70a0010",
@ -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": "a2c8c83a20f1124fd5af0f3c23894284",
"binary/integer-encoding.md": "32d4c6e59a86f6dc75392eeec6175d11",
"combinatorics/index.md": "f9de9671fdb6068ef2bb5e63051734be",
"_journal/2024-02-18.md": "67e36dbbb2cac699d4533b5a2eaeb629",
"_journal/2024-02/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048",
@ -179,11 +179,11 @@
"combinatorics/additive-principle.md": "84dcd0243263b3c53456086ae43fa00f",
"_journal/2024-02-19.md": "30d16c5373deb9cb128d2e7934ae256a",
"_journal/2024-02/2024-02-18.md": "67e36dbbb2cac699d4533b5a2eaeb629",
"combinatorics/permutations.md": "3c76f548e22f7271280b9ed82ae584ec",
"combinatorics/combinations.md": "c00ae174e8a172be1c3f89c231a2dd03",
"_journal/2024-02-20.md": "4e6ff95c5c3163fb909f37c078759b13",
"combinatorics/permutations.md": "9351e4d5c4457c34198640cf04bdd888",
"combinatorics/combinations.md": "2e7069e018525e10e4e2b9fb46bc8291",
"_journal/2024-02-20.md": "b85ba0eeeb16e30a602ccefabcc9763e",
"_journal/2024-02/2024-02-19.md": "df1a9ab7ab89244021b3003c84640c78",
"combinatorics/inclusion-exclusion.md": "0c63d52507b87cf276615715977218cb"
"combinatorics/inclusion-exclusion.md": "4d5ba716bc90cd378c7c4c816b224c75"
},
"fields_dict": {
"Basic": [

View File

@ -7,8 +7,8 @@ title: "2024-02-20"
- [ ] Sheet Music (10 min.)
- [x] OGS (1 Life & Death Problem)
- [ ] Korean (Read 1 Story)
- [ ] Interview Prep (1 Practice Problem)
- [ ] Log Work Hours (Max 3 hours)
- [x] Interview Prep (1 Practice Problem)
- [x] Log Work Hours (Max 3 hours)
* Added `printf` `length` field notes.
* 101weiqi (serial numbers)
@ -18,4 +18,5 @@ title: "2024-02-20"
* Q-267289
* Q-275961
* Q-324650
* Q-83832
* Q-83832
* Notes on integer encodings and how unsigned encoding relates to two's-complement.

View File

@ -8,7 +8,15 @@ tags:
## Overview
Integers are typically encoded using either **unsigned encoding** or **two's complement**.
Integers are typically encoded using either **unsigned encoding** or **two's complement**. The following table highlights how the min and max of these encodings behave:
Value | $w = 8$ | $w = 16$ | $w = 32$
-------- | ------- | -------- | ------------
$UMin_w$ | `0x00` | `0x0000` | `0x00000000`
$UMax_w$ | `0xFF` | `0xFFFF` | `0xFFFFFFFF`
$TMin_w$ | `0x80` | `0x8000` | `0x80000000`
$TMax_w$ | `0x7F` | `0x7FFF` | `0x7FFFFFFF`
%%ANKI
Basic
@ -68,6 +76,38 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1708177246114-->
END%%
%%ANKI
Basic
Which of unsigned encoding or two's-complement exhibit asymmetry in their range?
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.
<!--ID: 1708453398379-->
END%%
%%ANKI
Basic
What integral values share the same binary representation in unsigned encoding and two's-complement?
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.
<!--ID: 1708454709515-->
END%%
%%ANKI
Basic
According to the C standard, how are `unsigned` integral types encoded?
Back: Using 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: 1708455064691-->
END%%
%%ANKI
Basic
According to the C standard, how are `signed` integral types encoded?
Back: The C standard leaves this unspecified.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708455064696-->
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$$
@ -170,10 +210,34 @@ END%%
%%ANKI
Basic
Why is "$B2U$" insufficient for use?
Back: We need to understand how many bits conversion is with respect to.
What is the hexadecimal representation of $UMin_4$?
Back: `0x0000`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179147804-->
<!--ID: 1708453398386-->
END%%
%%ANKI
Basic
How is the smallest unsigned integer formatted in hexadecimal?
Back: As all `0`s.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398392-->
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
Basic
How is the largest unsigned integer formatted in hexadecimal?
Back: As all `F`s.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398403-->
END%%
### Two's-Complement
@ -293,12 +357,100 @@ END%%
%%ANKI
Basic
Why is "$B2T$" insufficient for use?
Back: We need to understand how many bits conversion is with respect to.
What is the hexadecimal representation of $TMin_4$?
Back: `0x8000`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708179649935-->
<!--ID: 1708453398408-->
END%%
%%ANKI
Basic
How is the smallest two's-complement integer formatted in hexadecimal?
Back: With a leading `8` followed by `0`s.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398413-->
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
Basic
How is the largest two's-complement integer formatted in hexadecimal?
Back: With a leading `7` followed by `F`s.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398425-->
END%%
%%ANKI
Basic
How is equality $|TMin| = |TMax|$ modified so that both sides actually balance?
Back: $|TMin| = |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: 1708453398430-->
END%%
%%ANKI
Basic
Which of negative and positive numbers can two's-complement encoding express more of?
Back: Negative.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398435-->
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$.)
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708453398440-->
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%%
## 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

@ -490,6 +490,68 @@ Tags: printf
<!--ID: 1707852083126-->
END%%
%%ANKI
Basic
Given `int64_t x`, why is `printf("%d", x)` a problem?
Back: `%d` matches an `int` which is not necessarily 64-bits.
Tags: printf
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1708454462772-->
END%%
%%ANKI
Basic
What must you use when invoking `printf` with a fixed-width integer type?
Back: `printf`-specific macros.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: printf
<!--ID: 1708454462777-->
END%%
%%ANKI
Basic
What is `PRId32` an example macro for?
Back: A macro that expands to the correct specifier for a 32-bit signed integral type.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: printf
<!--ID: 1708454462780-->
END%%
%%ANKI
Cloze
{`PRId32`} is to signed whereas {`PRIu32`} is to unsigned.
Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962).
Tags: printf
<!--ID: 1708454462784-->
END%%
%%ANKI
Basic
Which C header specifies `printf` macros for fixed-width integral types?
Back: `<inttypes.h>`
Reference: “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962).
Tags: printf
<!--ID: 1708454462788-->
END%%
%%ANKI
Basic
Given `int32_t x`, how might we invoke `printf` on it?
Back: `printf("%" PRId32, x)`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: printf
<!--ID: 1708454584564-->
END%%
%%ANKI
Basic
What prefix do `printf` macros from `<inttypes.h>` share?
Back: `PRI`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: printf
<!--ID: 1708454584568-->
END%%
## References
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.

View File

@ -160,6 +160,46 @@ Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n
<!--ID: 1708368078746-->
END%%
%%ANKI
Basic
How many *increasing* injective functions exist between $\{1, 2, 3\}$ and $\{a, b, c, d, e\}$?
Back: $\binom{5}{3}$
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708446818783-->
END%%
%%ANKI
Basic
How many *decreasing* injective functions exist between $\{1, 2\}$ and $\{a, b, c, d\}$?
Back: $\binom{4}{2}$
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708446818786-->
END%%
%%ANKI
Basic
Given finite sets $A$ and $B$, what is the number of *increasing* injective functions between $A$ and $B$?
Back: Given $k = |A|$ and $n = |B|$, $\binom{n}{k}$.
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708446818788-->
END%%
%%ANKI
Basic
What combinatorial concept explains the number of *increasing* injective functions between two finite sets?
Back: Combinations.
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708446818789-->
END%%
%%ANKI
Basic
Given $k = |A|$ and $n = |B|$, *why* is the number of increasing injective functions between $A$ and $B$ equal to $\binom{n}{k}$?
Back: We are "grouping" all functions by a shared permutation (i.e. the increasing function).
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708446818791-->
END%%
## Pascal's Triangle
A visual representation of the binomial coefficient's is in the form of Pascal's Triangle:
@ -183,6 +223,14 @@ Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n
<!--ID: 1708384441360-->
END%%
%%ANKI
Basic
*Why* is it that $\binom{n}{k} = \binom{n - 1}{k - 1} + \binom{n - 1}{k}$?
Back: For each member, we either include in a subset or we don't.
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708446818792-->
END%%
%%ANKI
Basic
What name is given to the following structure?
@ -314,6 +362,14 @@ A **lattice path** is one of the shorted possible paths connecting two points on
In this example, the total number of lattice paths from point $(0, 0)$ to $(3, 2)$ is therefore $\binom{5}{2} = \binom{5}{3}$.
%%ANKI
Basic
How many lattice paths are there from $(0, 0)$ to $(n, n)$?
Back: $\binom{2n}{n}$
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708451749788-->
END%%
%%ANKI
Basic
What is the integer lattice?
@ -450,6 +506,14 @@ Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n
<!--ID: 1708384441462-->
END%%
%%ANKI
Basic
How is $7^n$ written as a sum of powers of $6$?
Back: $7^n = (1 + 6)^n$. Apply binomial expansion on the RHS.
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708451749791-->
END%%
## References
* Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).

View File

@ -61,7 +61,7 @@ END%%
%%ANKI
Basic
Using sigma notation, what identity is used to prove PIE correctly counts members?
Using sigma notation, what binomial identity is used to prove PIE correctly counts members?
Back: $\sum_{k=0}^n (-1)^k \binom{n}{k} = 0$
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708438356483-->

View File

@ -61,6 +61,14 @@ Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n
<!--ID: 1708366788573-->
END%%
%%ANKI
Basic
How is $n!$ written recursively?
Back: As $n(n - 1)!$.
Reference: Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
<!--ID: 1708451749781-->
END%%
%%ANKI
Basic
How is permutation expressed recursively?