Add more notes on two's-complement.
parent
2678c94882
commit
d93f0d3cb6
|
@ -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": "32d4c6e59a86f6dc75392eeec6175d11",
|
"binary/integer-encoding.md": "fc9e88adabcbbf106c50718016e238c7",
|
||||||
"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",
|
||||||
|
@ -184,7 +184,7 @@
|
||||||
"_journal/2024-02-20.md": "b85ba0eeeb16e30a602ccefabcc9763e",
|
"_journal/2024-02-20.md": "b85ba0eeeb16e30a602ccefabcc9763e",
|
||||||
"_journal/2024-02/2024-02-19.md": "df1a9ab7ab89244021b3003c84640c78",
|
"_journal/2024-02/2024-02-19.md": "df1a9ab7ab89244021b3003c84640c78",
|
||||||
"combinatorics/inclusion-exclusion.md": "4d5ba716bc90cd378c7c4c816b224c75",
|
"combinatorics/inclusion-exclusion.md": "4d5ba716bc90cd378c7c4c816b224c75",
|
||||||
"_journal/2024-02-21.md": "d7545ab01e651e565c056ded99455286",
|
"_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": "1171835a864103fbc4cbe21d56fcfa1f"
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,4 +8,8 @@ title: "2024-02-21"
|
||||||
- [ ] OGS (1 Life & Death Problem)
|
- [ ] OGS (1 Life & Death Problem)
|
||||||
- [ ] Korean (Read 1 Story)
|
- [ ] Korean (Read 1 Story)
|
||||||
- [ ] Interview Prep (1 Practice Problem)
|
- [ ] Interview Prep (1 Practice Problem)
|
||||||
- [ ] Log Work Hours (Max 3 hours)
|
- [ ] Log Work Hours (Max 3 hours)
|
||||||
|
|
||||||
|
* 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.
|
|
@ -8,7 +8,7 @@ tags:
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
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:
|
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$
|
Value | $w = 8$ | $w = 16$ | $w = 32$
|
||||||
-------- | ------- | -------- | ------------
|
-------- | ------- | -------- | ------------
|
||||||
|
@ -108,6 +108,38 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
||||||
<!--ID: 1708455064696-->
|
<!--ID: 1708455064696-->
|
||||||
END%%
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What does $TMin_w$ evaluate to?
|
||||||
|
Back: $-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: 1708545383252-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What does $TMax_w$ evaluate to?
|
||||||
|
Back: $2^{w-1} - 1$
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708545383255-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What does $UMin_w$ evaluate to?
|
||||||
|
Back: $0$
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708545383256-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What does $UMax_w$ evaluate to?
|
||||||
|
Back: $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: 1708545383258-->
|
||||||
|
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}) = \sum_{i=0}^{w-1} 2^ix_i$$
|
||||||
|
@ -240,6 +272,30 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
||||||
<!--ID: 1708453398403-->
|
<!--ID: 1708453398403-->
|
||||||
END%%
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How does `n` relate to `~n` in unsigned encoding?
|
||||||
|
Back: `~n = UMax - 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).
|
||||||
|
<!--ID: 1708545383259-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
With 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).
|
||||||
|
<!--ID: 1708545574154-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Regardless of word size, what bitwise operations yield $UMax$?
|
||||||
|
Back: `~0`
|
||||||
|
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).
|
||||||
|
<!--ID: 1708545383261-->
|
||||||
|
END%%
|
||||||
|
|
||||||
### Two's-Complement
|
### Two's-Complement
|
||||||
|
|
||||||
Represents negative numbers along with nonnegative ones. Given an integral type $\vec{x}$ of $w$ bits, we convert binary to its twos'-complement encoding with: $$B2T_w(\vec{x}) = -2^{w-1}x_{w-1} + \sum_{i=0}^{w-2} 2^ix_i$$
|
Represents negative numbers along with nonnegative ones. Given an integral type $\vec{x}$ of $w$ bits, we convert binary to its twos'-complement encoding with: $$B2T_w(\vec{x}) = -2^{w-1}x_{w-1} + \sum_{i=0}^{w-2} 2^ix_i$$
|
||||||
|
@ -451,6 +507,112 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
||||||
<!--ID: 1708453398469-->
|
<!--ID: 1708453398469-->
|
||||||
END%%
|
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 an integer is the other 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?
|
||||||
|
Back: `-1` and `0`
|
||||||
|
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).
|
||||||
|
<!--ID: 1708545383262-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
In two's-complement, how many negative values can be encoded compared to nonnegative values?
|
||||||
|
Back: The same amount.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708545383264-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Cloze
|
||||||
|
In two's-complement, the {sign bit} partitions the encoding range into two sets.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708545383265-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%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).
|
||||||
|
<!--ID: 1708545383267-->
|
||||||
|
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).
|
||||||
|
<!--ID: 1708545383268-->
|
||||||
|
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).
|
||||||
|
<!--ID: 1708545383270-->
|
||||||
|
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).
|
||||||
|
<!--ID: 1708545383271-->
|
||||||
|
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).
|
||||||
|
<!--ID: 1708545383273-->
|
||||||
|
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).
|
||||||
|
<!--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: “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).
|
||||||
|
<!--ID: 1708545383276-->
|
||||||
|
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).
|
||||||
|
<!--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.
|
||||||
|
<!--ID: 1708545383279-->
|
||||||
|
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).
|
||||||
|
<!--ID: 1708545383280-->
|
||||||
|
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.
|
||||||
|
* “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).
|
||||||
|
|
Loading…
Reference in New Issue