Notes on binary shifting.

c-declarations
Joshua Potter 2024-02-13 18:23:48 -07:00
parent 9e902af312
commit 07097ccc7b
4 changed files with 46 additions and 5 deletions

View File

@ -124,7 +124,7 @@
"algorithms/sorting/selection-sort.md": "73a077a726afd376650d1bd9e2d0bed9",
"algorithms/index 1.md": "6fada1f3d5d3af64687719eb465a5b97",
"binary/hexadecimal.md": "033246347c06b6911ea497ce6da25263",
"binary/index.md": "3c672f105d6a98b21f66547d0731c1ab",
"binary/index.md": "4c12fdcd93422987d499cd193737b4c1",
"_journal/2024-02-09.md": "a798d35f0b2bd1da130f7ac766166109",
"c/types.md": "cf3e66e5aee58a94db3fdf0783908555",
"logic/quantification.md": "37655276de8da531ca2b12706a639224",
@ -153,9 +153,9 @@
"logic/truth-tables.md": "7892ceaa416c9a65acc79ca1e6ff778f",
"logic/short-circuit.md": "26d300f407f14883022d0ef8dc4f7300",
"logic/boolean-algebra.md": "c5d699d407e03d280aa1f201bc3e8b02",
"_journal/2024-02-13.md": "ed5e2692c31226489d1115d301c660ef",
"_journal/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0",
"_journal/2024-02/2024-02-12.md": "618c0035a69b48227119379236a02f44",
"binary/shifts.md": "c9237b5b949519694ab55deb9b740bd1"
"binary/shifts.md": "24a1f56268fdbae4fe22e92c18366abc"
},
"fields_dict": {
"Basic": [

View File

@ -14,4 +14,5 @@ title: "2024-02-13"
* Copied over my bash and neovim configs onto my desktop.
* Continued practicing One Summer's Day.
* Read 혹부리 할아버지 (The Old Man with the Lump).
* Begin adding notes/flashcards on `printf`. It's format specifier is used in enough places to warrant it.
* Begin adding notes/flashcards on `printf`. It's format specifier is used in enough places to warrant it.
* Started playing Nier Automata.

View File

@ -1,5 +1,7 @@
---
title: Binary
TARGET DECK: Obsidian::STEM
FILE TAGS: binary
tags:
- binary
---

View File

@ -156,6 +156,44 @@ Tags: c
<!--ID: 1707854589813-->
END%%
In C, it is undefined behavior to shift by more than the width $w$ of an integral type. Typically though, only the last $w$ bits are considered in the computation. For example, given `int32_t x`, `(x << 32) = (x << 0)`.
%%ANKI
Basic
According to the C standard, 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: c
<!--ID: 1707873410765-->
END%%
%%ANKI
Basic
According to the C standard, what is the result of shifting an `int32_t` value by a negative value?
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: c
<!--ID: 1707873410773-->
END%%
%%ANKI
Basic
Ignoring UB, what *typically* happens when shifting an `int32_t` by `k ≥ 32` bits?
Back: The shift value is interpreted as `k mod 32`.
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: 1707873410777-->
END%%
%%ANKI
Basic
How is $x \bmod 2^k$ equivalently written as a bit mask?
Back: `x & (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: c
<!--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.
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.