From 1fa7bd767588411a16caaf1f4e15d3e7ecd893d8 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Sun, 29 Sep 2024 05:42:17 -0600 Subject: [PATCH] C exact-width integer types. --- .../plugins/obsidian-to-anki-plugin/data.json | 13 +- notes/_journal/2024-09-28.md | 8 +- notes/binary/index.md | 84 ---- notes/c17/types/index.md | 14 + notes/c17/types/simple.md | 421 ++++++++++++++++-- .../endianness.md => encoding/binary.md} | 84 +++- notes/encoding/floating-point.md | 14 +- notes/encoding/integer.md | 39 +- 8 files changed, 538 insertions(+), 139 deletions(-) delete mode 100644 notes/binary/index.md rename notes/{binary/endianness.md => encoding/binary.md} (58%) diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index e45481d..52f18ec 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -337,7 +337,7 @@ "filesystems/cas.md": "d41c0d2e943adecbadd10a03fd1e4274", "git/objects.md": "887ebb42a6fb7c0ff448dd482ba53922", "git/index.md": "ca842957bda479dfa1170ae85f2f37b8", - "encoding/integer.md": "54130a02e1fc0a6033ce6ab7a781b0ae", + "encoding/integer.md": "f98ca0221b24c16b80aea45d01fcbfd7", "_journal/2024-02-29.md": "f610f3caed659c1de3eed5f226cab508", "_journal/2024-02/2024-02-28.md": "7489377c014a2ff3c535d581961b5b82", "_journal/2024-03-01.md": "a532486279190b0c12954966cbf8c3fe", @@ -382,7 +382,7 @@ "_journal/2024-03/2024-03-15.md": "e54b2513beac5f46313b4c37622adf39", "_journal/2024-03-17.md": "72e99c7630085aee2c7f340a06b5ada7", "_journal/2024-03/2024-03-16.md": "ab7629c24ebe70838072cf6acec47cb0", - "encoding/floating-point.md": "88e305245e5115b6f9b6593b59364225", + "encoding/floating-point.md": "90c36f4b93312e28e5cfec3e2a8231f4", "_journal/2024-03-18.md": "8479f07f63136a4e16c9cd07dbf2f27f", "_journal/2024-03/2024-03-17.md": "23f9672f5c93a6de52099b1b86834e8b", "set/directed-graph.md": "b4b8ad1be634a0a808af125fe8577a53", @@ -783,11 +783,11 @@ "c17/enums.md": "9414fb67aa256a0a11b7240534c67bf6", "c17/derived-types.md": "6fb8f23a2423f05d5bdccb6672a32e38", "c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875", - "c17/types/simple.md": "0fab7afc623e441cd17ae23497f6babe", + "c17/types/simple.md": "0edbd5da2138aa46669135a03b935db9", "c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d", "c17/types/derived.md": "aff0d2b6d218fb67af3cc92ead924de3", "c17/types/basic.md": "5064e21e683c0218890058882e06b6f3", - "c17/types/index.md": "7f8541016f9ac91cc6477fe864fdccd3", + "c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b", "_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2", "_journal/2024-08/2024-08-24.md": "563fad24740e44734a87d7c3ec46cec4", "algebra/abs-val.md": "a47bc08db62304eb526d15ede3e300cf", @@ -844,7 +844,10 @@ "_journal/2024-09/2024-09-25.md": "c8a3414e27c8ce635fe995c2dfbf6019", "c17/macros.md": "f7bade849d2cf714cd2ff79c4ed003d4", "_journal/2024-09-27.md": "dd82b2c5c5389b6a35c4c2fcf857417c", - "_journal/2024-09/2024-09-26.md": "2d3e8325e7ab63168c460f18e7aa1afc" + "_journal/2024-09/2024-09-26.md": "2d3e8325e7ab63168c460f18e7aa1afc", + "_journal/2024-09-28.md": "7726baed125a2561def07dcaf48bf5a0", + "_journal/2024-09/2024-09-27.md": "d788fa04c029009f42387317c549d93e", + "encoding/binary.md": "0b9beb6913906aa2523d8ab193c67f67" }, "fields_dict": { "Basic": [ diff --git a/notes/_journal/2024-09-28.md b/notes/_journal/2024-09-28.md index f76ae29..81b862f 100644 --- a/notes/_journal/2024-09-28.md +++ b/notes/_journal/2024-09-28.md @@ -4,6 +4,10 @@ title: "2024-09-28" - [ ] Anki Flashcards - [x] KoL -- [ ] OGS +- [x] OGS - [ ] Sheet Music (10 min.) -- [ ] Korean (Read 1 Story) \ No newline at end of file +- [ ] Korean (Read 1 Story) + +* Walked through the proof of the recursion theorem on $\omega$. +* Additional notes on [[simple#Exact-Width Integer Types|exact-width integer types]]. +* Severy more notes on C types/limits/etc. \ No newline at end of file diff --git a/notes/binary/index.md b/notes/binary/index.md deleted file mode 100644 index 98cb4eb..0000000 --- a/notes/binary/index.md +++ /dev/null @@ -1,84 +0,0 @@ ---- -title: Binary -TARGET DECK: Obsidian::STEM -FILE TAGS: binary -tags: - - binary ---- - -## Overview - -A binary digit or **bit** is a `0` or `1` character. A **bit string** is then a contiguous sequence of bits. It's **weight** is a reference to the number of `1`s in the bit string. Compare the below operation to the method for converting from one numerical base to another (e.g. [[radices#Hexadecimal|hexadecimal]]). - -```c -unsigned int bit_weight(int64_t n) { - unsigned int count = 0; - while (n) { - count += (n % 2 == 0) ? 0 : 1; - n /= 2; - } - return count; -} -``` - -%%ANKI -Basic -Why is a "bit" named the way it is? -Back: It is short for **b**inary dig**it**. -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). - -END%% - -%%ANKI -Basic -What does the weight of a bit string refer to? -Back: The number of `1`s in the string. -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). - -END%% - -%%ANKI -Basic -How might you use C to find the weight of a bit string? -Back: Repeatedly divide by `2`, counting all remainders of `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). -Tags: c17 - -END%% - -%%ANKI -Cloze -A byte consists of {8} bits. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -A byte consists of {2} nibbles. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Cloze -A nibble consists of {4} bits. -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 -What process is used to convert from e.g. decimal to another base? -Back: Divide repeatedly by the base. Maintain remainders right to left. -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 -Why does converting from e.g. decimal to another base involve repeated division? -Back: The position of a digit corresponds to the base raised to that position. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% \ No newline at end of file diff --git a/notes/c17/types/index.md b/notes/c17/types/index.md index 71d09b3..55f13df 100644 --- a/notes/c17/types/index.md +++ b/notes/c17/types/index.md @@ -312,6 +312,20 @@ Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70 END%% +%%ANKI +Cloze +Integer types are to {1:$\mathbb{N}$} and {2:$\mathbb{Z}$}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +Floating-point types are to {1:$\mathbb{R}$} and {2:$\mathbb{C}$}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + %%ANKI Cloze The {real} types contrast the {complex} types. diff --git a/notes/c17/types/simple.md b/notes/c17/types/simple.md index 37c77e8..1b6809e 100644 --- a/notes/c17/types/simple.md +++ b/notes/c17/types/simple.md @@ -61,28 +61,26 @@ END%% %%ANKI Basic -What does the "width" of an integer type refer to? -Back: The number of bits used to represent its value. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI What two variants does a C integral type declaration have? Back: Signed and unsigned. 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 What does it mean for an integer to be "signed"? Back: It can represent negative, zero, and positive values. 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 What does it mean for an integer to be "unsigned"? Back: It can only represent nonnegative values. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + END%% %%ANKI @@ -206,7 +204,7 @@ END%% %%ANKI Cloze -{1:`float`} has {2:4} byte precision whereas {2:`double`} has {1:8} byte precision. +{1:`float`} has {2:4} byte width whereas {2:`double`} has {1:8} byte width. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. END%% @@ -265,19 +263,17 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co END%% -### Unsigned +### Unsigned Integers -These correspond to nonnegative integer values. - -| Name | Narrow | Rank | -| -------------------- | ------ | ---- | -| `bool` | Yes | 0 | -| `char` (maybe) | Yes | 1 | -| `unsigned char` | Yes | 1 | -| `unsighed short` | Yes | 2 | -| `unsigned int` | No | 3 | -| `unsigned long` | No | 4 | -| `unsigned long long` | No | 5 | +| Name | Narrow | Rank | Minimum Width | +| -------------------- | ------ | ---- | ------------- | +| `bool` | Yes | 0 | 1 | +| `char` (maybe) | Yes | 1 | - | +| `unsigned char` | Yes | 1 | 8 | +| `unsighed short` | Yes | 2 | 16 | +| `unsigned int` | No | 3 | 16 | +| `unsigned long` | No | 4 | 32 | +| `unsigned long long` | No | 5 | 64 | %%ANKI Basic @@ -351,21 +347,154 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co END%% -### Signed +%%ANKI +Basic +What is the minimum width of a `bool`? +Back: $1$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% -These correspond to possibly negative integer values. +%%ANKI +Basic +What is the minimum precision of a `bool`? +Back: $1$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% -| Name | Narrow | Rank | -| -------------------- | ------ | ---- | -| `char` (maybe) | Yes | 1 | -| `signed char` | Yes | 1 | -| `signed short` | Yes | 2 | -| `signed int` | No | 3 | -| `signed long` | No | 4 | -| `signed long long` | No | 5 | -| `float` | - | - | -| `double` | - | - | -| `long double` | - | - | +%%ANKI +Basic +What is the minimum width of an `unsigned char`? +Back: $8$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned char`? +Back: $8$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the maximum precision of an `unsigned char`? +Back: N/A. C does not define maximum precisions. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned short`? +Back: $16$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode an `unsigned short`? +Back: Unsigned encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned short`? +Back: $16$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned int`? +Back: $16$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned int`? +Back: $16$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned long`? +Back: $32$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode an `unsigned long`? +Back: Unsigned encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned long`? +Back: $32$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of an `unsigned long`? +Back: $32$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +END%% + +%%ANKI +Basic +What is the minimum precision of an `unsigned long long`? +Back: $32$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the smallest possible `unsigned long long`? +Back: As `0`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the largest possible `unsigned long long`? +Back: As `ULLONG_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +### Signed Integers + +| Name | Narrow | Rank | Minimum Width | +| ------------------ | ------ | ---- | ------------- | +| `char` (maybe) | Yes | 1 | - | +| `signed char` | Yes | 1 | 8 | +| `signed short` | Yes | 2 | 16 | +| `signed int` | No | 3 | 16 | +| `signed long` | No | 4 | 32 | +| `signed long long` | No | 5 | 64 | +| `float` | - | - | - | +| `double` | - | - | - | +| `long double` | - | - | - | %%ANKI Basic @@ -431,6 +560,117 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co END%% +%%ANKI +Basic +What is the minimum width of a `signed char`? +Back: $8$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode a `signed char`? +Back: N/A. This is implementation specific. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed char`? +Back: $7$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed short`? +Back: $16$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed short`? +Back: $15$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed int`? +Back: $16$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the underlying binary representation used to encode a `signed char`? +Back: N/A. This is implementation specific. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed int`? +Back: $15$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed long`? +Back: $32$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed long`? +Back: $31$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum width of a `signed long long`? +Back: $64$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is the minimum precision of a `signed long long`? +Back: $63$ +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the smallest possible `signed int`? +Back: As `INT_MIN`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the largest possible `signed int`? +Back: As `INT_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + ### Literals Negative integer literals are typed in a counterintuitive way. When the compiler sees a number of form `-X`, the type of `X` is determined *before* being negated. Promotion follows the **first fit rule** described as follows: @@ -675,7 +915,7 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% -Integer constants can be forced to be unsigned or to be a type with minimal width by using the following suffixes: +Integer constants can be made a certain signedness or type by using the following suffixes: | Suffix | Type | | ------ | -------------------- | @@ -826,7 +1066,7 @@ END%% ### Literals -Floating-point constants can be forced to be a type with minimal width by using the following suffixes: +Floating-point constants can be made a certain type by using the following suffixes: | Suffix | Type | | ------ | ------------- | @@ -905,6 +1145,117 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +%%ANKI +Cloze +`INT_MAX` is to {``} whereas `DBL_MAX` is to {``}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does `DBL_MIN` refer to? +Back: The smallest `double` strictly greater than 0. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the smallest possible `double`? +Back: As `-DBL_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How do we portably compute the largest possible `double`? +Back: As `DBL_MAX`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +## Exact-Width Integer Types + +The `stdint.h` library contains **exact-width integer types**. These are aliases to types that represent an exact width and sign representation: + +* If the type `uintN_t` is provided, it is an unsigned integer type with exactly $N$ bits of width and precision. +* If the type `intN_t` is provided, is is signed, with two's complement representation, has a width of exactly $N$ bits and a precision of $N - 1$. + +The C standard says these `typedef`s *must* be defined if they can be satisfied. Otherwise they may not exist. + +%%ANKI +Cloze +The {``} library contains {exact-width integer types}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `uintN_t` exists. What is its width? +Back: `N` bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `uintN_t` exists. What is its precision? +Back: `N` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `uintN_t` exists. It is encoded using what binary representation? +Back: Unsigned encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `intN_t` exists. What is its width? +Back: `N` bits. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `intN_t` exists. What is its precision? +Back: `N - 1` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose `intN_t` exists. It is encoded using what binary representation? +Back: Two's-complement encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose a platform has a 32-bit signed integer type. Why might `int32_t` not be defined? +Back: The platform may not use two's-complement encoding. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Suppose a platform has a 32-bit signed two's-complement integer type. Why might `int32_t` not be defined? +Back: N/A. The C standard states `int32_t` *must* be defined. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + ## Enumerated Types An `enum` is a mapping of identifiers with integer values. They have general form: diff --git a/notes/binary/endianness.md b/notes/encoding/binary.md similarity index 58% rename from notes/binary/endianness.md rename to notes/encoding/binary.md index f0360c4..bf557a7 100644 --- a/notes/binary/endianness.md +++ b/notes/encoding/binary.md @@ -1,14 +1,90 @@ --- -title: Endianness -TARGET DECK: Obsidian::STEM -FILE TAGS: binary::endian +title: Binary +TARGET DECK: Obsidian::H&SS +FILE TAGS: binary tags: - binary - - endian --- ## Overview +A binary digit or **bit** is a `0` or `1` character. A **bit string** is then a contiguous sequence of bits. It's **weight** is a reference to the number of `1`s in the bit string. Compare the below operation to the method for converting from one numerical base to another (e.g. [[radices#Hexadecimal|hexadecimal]]). + +```c +unsigned int bit_weight(int64_t n) { + unsigned int count = 0; + while (n) { + count += (n % 2 == 0) ? 0 : 1; + n /= 2; + } + return count; +} +``` + +%%ANKI +Basic +Why is a "bit" named the way it is? +Back: It is short for **b**inary dig**it**. +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). + +END%% + +%%ANKI +Basic +What does the weight of a bit string refer to? +Back: The number of `1`s in the string. +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). + +END%% + +%%ANKI +Basic +How might you use C to find the weight of a bit string? +Back: Repeatedly divide by `2`, counting all remainders of `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). +Tags: c17 + +END%% + +%%ANKI +Cloze +A byte consists of {8} bits. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +A byte consists of {2} nibbles. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Cloze +A nibble consists of {4} bits. +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 +What process is used to convert from e.g. decimal to another base? +Back: Divide repeatedly by the base. Maintain remainders right to left. +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 +Why does converting from e.g. decimal to another base involve repeated division? +Back: The position of a digit corresponds to the base raised to that position. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +## Endianness + Platforms with multi-byte objects must establish the object's address and byte ordering. Objects are typically addressed by the smallest address of the bytes used. Bytes are ordered either in **big-endian** or **little-endian**. In big-endian, the most significant byte is listed first. In little-endian, the least significant byte is ordered first. %%ANKI diff --git a/notes/encoding/floating-point.md b/notes/encoding/floating-point.md index b2b48b1..bd97674 100644 --- a/notes/encoding/floating-point.md +++ b/notes/encoding/floating-point.md @@ -33,6 +33,8 @@ Declaration | Sign Bit | Exponent Field | Fractional Field `float` | `1` | `8` | `23` `double` | `1` | `11` | `52` +The **precision** of a floating-point type refers to the number of bits found in the fractional field. + %%ANKI Basic In base-10 scientific notation, what form do nonzero numbers take on? @@ -1032,6 +1034,14 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program END%% +%%ANKI +Basic +What does the precision of a floating-point number refer to? +Back: The number of bits found in its fractional field. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + ## Rounding Because floating-point arithmetic can't represent every real number, it must round results to the "nearest" representable number, however "nearest" is defined. The IEEE floating-point standard defines four **rounding modes** to influence this behavior: @@ -1268,8 +1278,8 @@ END%% %%ANKI Basic Is $+^f$ commutative? -Back: Yes. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. +Back: No. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% diff --git a/notes/encoding/integer.md b/notes/encoding/integer.md index b2dceb7..1922559 100644 --- a/notes/encoding/integer.md +++ b/notes/encoding/integer.md @@ -11,13 +11,38 @@ tags: 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` +| 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` | +The **width** of an integer type refers to the number of bits used in the type's binary representation. It's **precision** refers to the number of bits used in the type's binary representation, excluding those used for signedness. + +%%ANKI +Basic +What does the "width" of an integer type refer to? +Back: The number of bits used to represent its value. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does the "precision" of an integer type refer to? +Back: The number of bits used to represent its value, excluding bits used for sign representations. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What distinguishes the "width" from the "precision" of an integer type? +Back: The latter does not consider bits used for sign representations. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% %%ANKI Basic @@ -2180,4 +2205,4 @@ END%% * Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. * Finley, Thomas. “Two’s Complement,” April 2000. [https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html](https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html). * 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). \ No newline at end of file +* “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).