Two's-complement addition and consolidate `shifts.md`.
parent
eb3cc5743a
commit
a1447e824c
|
@ -172,7 +172,7 @@
|
||||||
"algorithms/binary-search.md": "dbbaf8d4be7aabef1ed232c1906b4c99",
|
"algorithms/binary-search.md": "dbbaf8d4be7aabef1ed232c1906b4c99",
|
||||||
"_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": "aaad7d74b5084093e4aa90fbb2212384",
|
"binary/integer-encoding.md": "bb73ce7edf88620fb4b59f33850277e2",
|
||||||
"combinatorics/index.md": "9a85e8858c50c9797243d6d01e1dcbe7",
|
"combinatorics/index.md": "9a85e8858c50c9797243d6d01e1dcbe7",
|
||||||
"_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",
|
||||||
|
@ -200,7 +200,9 @@
|
||||||
"_journal/2024-02-24.md": "9bb319d5014caf962a9ce3141076cff4",
|
"_journal/2024-02-24.md": "9bb319d5014caf962a9ce3141076cff4",
|
||||||
"_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7",
|
"_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7",
|
||||||
"_journal/2024-02-25.md": "fb1a48208c11d12262facc647749ca6f",
|
"_journal/2024-02-25.md": "fb1a48208c11d12262facc647749ca6f",
|
||||||
"_journal/2024-02/2024-02-24.md": "ee92fd63ebbda5f8ffa0abd06700ead1"
|
"_journal/2024-02/2024-02-24.md": "ee92fd63ebbda5f8ffa0abd06700ead1",
|
||||||
|
"_journal/2024-02-26.md": "67582808809eeb149c134d8dd021edd6",
|
||||||
|
"_journal/2024-02/2024-02-25.md": "426be827fe8483e4ab432304a2aa6df3"
|
||||||
},
|
},
|
||||||
"fields_dict": {
|
"fields_dict": {
|
||||||
"Basic": [
|
"Basic": [
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
---
|
|
||||||
title: "2024-02-25"
|
|
||||||
---
|
|
||||||
|
|
||||||
- [x] Anki Flashcards
|
|
||||||
- [ ] KoL
|
|
||||||
- [ ] Sheet Music (10 min.)
|
|
||||||
- [ ] OGS (1 Life & Death Problem)
|
|
||||||
- [ ] Korean (Read 1 Story)
|
|
||||||
- [ ] Interview Prep (1 Practice Problem)
|
|
||||||
- [ ] Log Work Hours (Max 3 hours)
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
title: "2024-02-26"
|
||||||
|
---
|
||||||
|
|
||||||
|
- [x] Anki Flashcards
|
||||||
|
- [x] KoL
|
||||||
|
- [ ] Sheet Music (10 min.)
|
||||||
|
- [ ] OGS (1 Life & Death Problem)
|
||||||
|
- [ ] Korean (Read 1 Story)
|
||||||
|
- [ ] Interview Prep (1 Practice Problem)
|
||||||
|
- [ ] Log Work Hours (Max 3 hours)
|
||||||
|
|
||||||
|
* Added flashcards on two's-complement addition.
|
||||||
|
* Consolidated `integer-encoding.md` with `shifts.md`.
|
|
@ -0,0 +1,28 @@
|
||||||
|
---
|
||||||
|
title: "2024-02-25"
|
||||||
|
---
|
||||||
|
|
||||||
|
- [x] Anki Flashcards
|
||||||
|
- [x] KoL
|
||||||
|
- [ ] Sheet Music (10 min.)
|
||||||
|
- [x] OGS (1 Life & Death Problem)
|
||||||
|
- [x] Korean (Read 1 Story)
|
||||||
|
- [ ] Interview Prep (1 Practice Problem)
|
||||||
|
- [ ] Log Work Hours (Max 3 hours)
|
||||||
|
|
||||||
|
* Read 흥부와 놀부 (Heungbuwa Nolbu).
|
||||||
|
* 101weiqi (serial numbers)
|
||||||
|
* Q-79448
|
||||||
|
* Q-5508
|
||||||
|
* Q-359007
|
||||||
|
* Q-12406
|
||||||
|
* Q-72569
|
||||||
|
* Q-259158
|
||||||
|
* Q-93867
|
||||||
|
* Q-84628
|
||||||
|
* Q-342740
|
||||||
|
* Q-54992
|
||||||
|
* Q-49888
|
||||||
|
* Q-10602
|
||||||
|
* Q-261719
|
||||||
|
* Q-178790
|
|
@ -822,6 +822,190 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
||||||
<!--ID: 1708701087982-->
|
<!--ID: 1708701087982-->
|
||||||
END%%
|
END%%
|
||||||
|
|
||||||
|
## Shifting
|
||||||
|
|
||||||
|
Left shift operations (`<<`) drop the `k` most significant bits and fills the right end of the result with `k` zeros. Right shift operations (`>>`) are classified in two ways:
|
||||||
|
|
||||||
|
* **Logical**
|
||||||
|
* Drops the `k` least significant bits and fills the left end of the result with `k` zeros.
|
||||||
|
* This mode is always used when calling `>>` on unsigned data.
|
||||||
|
* Sometimes denoted as `>>>` to disambiguate from arithmetic right shifts.
|
||||||
|
* **Arithmetic**
|
||||||
|
* Drops the `k` least significant bits and fills the left end of the result with `k` copies of the most significant bit.
|
||||||
|
* This mode is usually used when calling `>>` on signed data.
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How is decimal value $2^n$ written in binary?
|
||||||
|
Back: As `1` followed by $n$ zeros.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1707432641574-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What kinds of left shift operations are there?
|
||||||
|
Back: Just logical.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1707854589773-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How many significant bits are dropped on a left shift by `k`?
|
||||||
|
Back: `k`
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708784904518-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How many `0`s exist in the result of a left shift by `k`?
|
||||||
|
Back: At least `k`.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708784904521-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What kinds of right shift operations are there?
|
||||||
|
Back: Logical and arithmetic
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1707854589784-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What is a logical right shift operation?
|
||||||
|
Back: One that fills the left end of the result with zeros.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1707854589786-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What is an arithmetic right shift operation?
|
||||||
|
Back: One that fills the left end of the result with copies of the most significant bit.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1707854589789-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What kind of right shift operation is *usually* applied to signed numbers?
|
||||||
|
Back: Arithmetic.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1707854589801-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What kind of right shift operation is applied to unsigned numbers?
|
||||||
|
Back: Logical.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1707854589804-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What portability issue do shift operations introduce?
|
||||||
|
Back: There is no standard on whether right shifts of signed numbers are logical or arithmetic.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1707854589808-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Cloze
|
||||||
|
{1:Arithmetic} right shifts are to {1:signed} numbers whereas {2:logical} right shifts are to {2:unsigned} numbers.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1707854589813-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
In C, it is undefined behavior to shift by more than the width $w$ of an integral type.
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Assuming two's-complement, what is the result of shifting an `int32_t` value by `32`?
|
||||||
|
Back: It is undefined behavior.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708785613342-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Assuming two's-complement, what is the result of shifting an `int32_t` value by `31`?
|
||||||
|
Back: $-2^{31}$
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708785613370-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What is the result of shifting an `int32_t` value by `-1`?
|
||||||
|
Back: It is undefined behavior.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708785613376-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What is the result of shifting an `uint32_t` value by `32`?
|
||||||
|
Back: It is undefined behavior.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708785613383-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What is the result of shifting an `uint32_t` value by `31`?
|
||||||
|
Back: $2^{31}$
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708785613389-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What is the result of shifting an `uint32_t` value by `-1`?
|
||||||
|
Back: It is undefined behavior.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708785613393-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How is $2^n$ written using bitwise shift operators?
|
||||||
|
Back: `1 << n`
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708784904524-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What decimal value does `1 << n` translate to?
|
||||||
|
Back: $2^n$
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708784904526-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How is $x \bmod 2^k$ equivalently written as a bit mask?
|
||||||
|
Back: `x & ((1 << k) - 1)`
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1707873410780-->
|
||||||
|
END%%
|
||||||
|
|
||||||
## Arithmetic
|
## Arithmetic
|
||||||
|
|
||||||
### Addition
|
### Addition
|
||||||
|
@ -871,8 +1055,8 @@ END%%
|
||||||
Unsigned addition of $w$-bit integral types, denoted $+_w^u$, behaves like so:
|
Unsigned addition of $w$-bit integral types, denoted $+_w^u$, behaves like so:
|
||||||
|
|
||||||
$$x +_w^u y = \begin{cases}
|
$$x +_w^u y = \begin{cases}
|
||||||
x + y & \text{if } x + y < 2^w \\
|
x + y - 2^w & \text{if } x + y \geq 2^w \\
|
||||||
x + y - 2^w & \text{otherwise}
|
x + y & \text{otherwise}
|
||||||
\end{cases}$$
|
\end{cases}$$
|
||||||
|
|
||||||
This is more simply expressed as $x +_w^u y = (x + y) \bmod 2^w$.
|
This is more simply expressed as $x +_w^u y = (x + y) \bmod 2^w$.
|
||||||
|
@ -928,14 +1112,14 @@ END%%
|
||||||
|
|
||||||
%%ANKI
|
%%ANKI
|
||||||
Cloze
|
Cloze
|
||||||
Without using modulo arithmetic, $x +_w^u y =$ {$x + y$} if {$x + y < 2^w$}.
|
Without using modular arithmetic, $x +_w^u y =$ {$x + y$} if {$x + y < 2^w$}.
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
<!--ID: 1708799678758-->
|
<!--ID: 1708799678758-->
|
||||||
END%%
|
END%%
|
||||||
|
|
||||||
%%ANKI
|
%%ANKI
|
||||||
Cloze
|
Cloze
|
||||||
Without using modulo arithmetic, $x +_w^u y =$ {$x + y - 2^w$} if {$x + y \geq 2^w$}.
|
Without using modular arithmetic, $x +_w^u y =$ {$x + y - 2^w$} if {$x + y \geq 2^w$}.
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
<!--ID: 1708799678761-->
|
<!--ID: 1708799678761-->
|
||||||
END%%
|
END%%
|
||||||
|
@ -995,7 +1179,119 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
||||||
<!--ID: 1708808252017-->
|
<!--ID: 1708808252017-->
|
||||||
END%%
|
END%%
|
||||||
|
|
||||||
|
Two's-complement addition, denoted $+_w^t$ operates similarly:
|
||||||
|
|
||||||
|
$$x +_w^u y = \begin{cases}
|
||||||
|
x + y - 2^w & \text{if } x + y \geq 2^{w-1} \\
|
||||||
|
x + y + 2^w & \text{if } x + y < -2^{w-1} \\
|
||||||
|
x + y & \text{otherwise}
|
||||||
|
\end{cases}$$
|
||||||
|
|
||||||
|
Unlike with unsigned addition, there is no simpler modulus operation that can be applied.
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What kind of overflows does two's-complement addition potentially exhibit?
|
||||||
|
Back: Positive and negative overflow.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376220-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
Why is two's-complement addition overflow UB?
|
||||||
|
Back: Because the C standard does not mandate any particular signed integer encoding.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
Tags: c17
|
||||||
|
<!--ID: 1708964376225-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
What does $+_w^t$ denote?
|
||||||
|
Back: Two's-complement addition of $w$-bit integral types.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376228-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
*Why* doesn't two's-complement addition perform modular arithmetic?
|
||||||
|
Back: Because negative values are representable.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376231-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Cloze
|
||||||
|
$x +_w^t y =$ {$x + y - 2^w$} if {$x + y \geq 2^{w-1}$}.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376235-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Cloze
|
||||||
|
$x +_w^t y =$ {$x + y + 2^w$} if {$x + y < -2^{w-1}$}.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376238-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Cloze
|
||||||
|
$x +_w^t y =$ {$x + y$} if {$-2^{w-1} \leq x + y < 2^{w-1}$}.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376242-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How do we detect $x +_w^t y$ positive overflowed?
|
||||||
|
Back: This happens iff $x > 0$, $y > 0$, and $x +_w^t y \leq 0$.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376246-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How do we detect $x +_w^t y$ negative overflowed?
|
||||||
|
Back: This happens iff $x < 0$, $y < 0$, and $x +_w^t y \geq 0$.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376250-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How can we write $x +_w^t y$ in terms of unsigned addition?
|
||||||
|
Back: $x +_w^t y = U2T_w(T2U_w(x) +_w^u T2U_w(y))$
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376254-->
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
*Why* are we able to characterize $+_w^t$ in terms of $+_w^u$?
|
||||||
|
Back: Because two's-complement addition has the same bit-level representation as unsigned addition.
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
END%%
|
||||||
|
|
||||||
|
%%ANKI
|
||||||
|
Basic
|
||||||
|
How would you complete the body of this function?
|
||||||
|
```c
|
||||||
|
/* Determine whether arguments can be added without overflow */
|
||||||
|
int tadd_ok(int x, int y);
|
||||||
|
```
|
||||||
|
Back:
|
||||||
|
```c
|
||||||
|
int pos_over = x > 0 && y > 0 && (x + y) <= 0;
|
||||||
|
int neg_over = x < 0 && y < 0 && (x + y) >= 0;
|
||||||
|
return !pos_over && !neg_over;
|
||||||
|
```
|
||||||
|
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||||
|
<!--ID: 1708964376259-->
|
||||||
|
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.
|
||||||
|
* Ronald L. Graham, Donald Ervin Knuth, and Oren Patashnik, *Concrete Mathematics: A Foundation for Computer Science*, 2nd ed (Reading, Mass: Addison-Wesley, 1994).
|
||||||
* “Two’s-Complement.” In *Wikipedia*, January 9, 2024. [https://en.wikipedia.org/w/index.php?title=Two%27s_complement&oldid=1194543561](https://en.wikipedia.org/w/index.php?title=Two%27s_complement&oldid=1194543561).
|
* “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).
|
||||||
|
|
|
@ -1,196 +0,0 @@
|
||||||
---
|
|
||||||
title: Shifts
|
|
||||||
TARGET DECK: Obsidian::STEM
|
|
||||||
FILE TAGS: binary
|
|
||||||
tags:
|
|
||||||
- binary
|
|
||||||
---
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
Left shift operations (`<<`) drop the `k` most significant bits and fills the right end of the result with `k` zeros. Right shift operations (`>>`) are classified in two ways:
|
|
||||||
|
|
||||||
* **Logical**
|
|
||||||
* Drops the `k` least significant bits and fills the left end of the result with `k` zeros.
|
|
||||||
* This mode is always used when calling `>>` on unsigned data.
|
|
||||||
* Sometimes denoted as `>>>` to disambiguate from arithmetic right shifts.
|
|
||||||
* **Arithmetic**
|
|
||||||
* Drops the `k` least significant bits and fills the left end of the result with `k` copies of the most significant bit.
|
|
||||||
* This mode is usually used when calling `>>` on signed data.
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
How is decimal value $2^n$ written in binary?
|
|
||||||
Back: As `1` followed by $n$ zeros.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1707432641574-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What kinds of left shift operations are there?
|
|
||||||
Back: Just logical.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1707854589773-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
How many significant bits are dropped on a left shift by `k`?
|
|
||||||
Back: `k`
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1708784904518-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
How many `0`s exist in the result of a left shift by `k`?
|
|
||||||
Back: At least `k`.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1708784904521-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What kinds of right shift operations are there?
|
|
||||||
Back: Logical and arithmetic
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1707854589784-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What is a logical right shift operation?
|
|
||||||
Back: One that fills the left end of the result with zeros.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1707854589786-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What is an arithmetic right shift operation?
|
|
||||||
Back: One that fills the left end of the result with copies of the most significant bit.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1707854589789-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What kind of right shift operation is *usually* applied to signed numbers?
|
|
||||||
Back: Arithmetic.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1707854589801-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What kind of right shift operation is applied to unsigned numbers?
|
|
||||||
Back: Logical.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
<!--ID: 1707854589804-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What portability issue do shift operations introduce?
|
|
||||||
Back: There is no standard on whether right shifts of signed numbers are logical or arithmetic.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1707854589808-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Cloze
|
|
||||||
{1:Arithmetic} right shifts are to {1:signed} numbers whereas {2:logical} right shifts are to {2:unsigned} numbers.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1707854589813-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
In C, it is undefined behavior to shift by more than the width $w$ of an integral type.
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
Assuming two's-complement, what is the result of shifting an `int32_t` value by `32`?
|
|
||||||
Back: It is undefined behavior.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708785613342-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
Assuming two's-complement, what is the result of shifting an `int32_t` value by `31`?
|
|
||||||
Back: $-2^{31}$
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708785613370-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What is the result of shifting an `int32_t` value by `-1`?
|
|
||||||
Back: It is undefined behavior.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708785613376-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What is the result of shifting an `uint32_t` value by `32`?
|
|
||||||
Back: It is undefined behavior.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708785613383-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What is the result of shifting an `uint32_t` value by `31`?
|
|
||||||
Back: $2^{31}$
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708785613389-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What is the result of shifting an `uint32_t` value by `-1`?
|
|
||||||
Back: It is undefined behavior.
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708785613393-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
How is $2^n$ written using bitwise shift operators?
|
|
||||||
Back: `1 << n`
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708784904524-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
What decimal value does `1 << n` translate to?
|
|
||||||
Back: $2^n$
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1708784904526-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
%%ANKI
|
|
||||||
Basic
|
|
||||||
How is $x \bmod 2^k$ equivalently written as a bit mask?
|
|
||||||
Back: `x & ((1 << k) - 1)`
|
|
||||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
Tags: c17
|
|
||||||
<!--ID: 1707873410780-->
|
|
||||||
END%%
|
|
||||||
|
|
||||||
## References
|
|
||||||
|
|
||||||
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
|
||||||
* Ronald L. Graham, Donald Ervin Knuth, and Oren Patashnik, *Concrete Mathematics: A Foundation for Computer Science*, 2nd ed (Reading, Mass: Addison-Wesley, 1994).
|
|
Loading…
Reference in New Issue