C exact-width integer types.

main
Joshua Potter 2024-09-29 05:42:17 -06:00
parent a940234a8d
commit 1fa7bd7675
8 changed files with 538 additions and 139 deletions

View File

@ -337,7 +337,7 @@
"filesystems/cas.md": "d41c0d2e943adecbadd10a03fd1e4274", "filesystems/cas.md": "d41c0d2e943adecbadd10a03fd1e4274",
"git/objects.md": "887ebb42a6fb7c0ff448dd482ba53922", "git/objects.md": "887ebb42a6fb7c0ff448dd482ba53922",
"git/index.md": "ca842957bda479dfa1170ae85f2f37b8", "git/index.md": "ca842957bda479dfa1170ae85f2f37b8",
"encoding/integer.md": "54130a02e1fc0a6033ce6ab7a781b0ae", "encoding/integer.md": "f98ca0221b24c16b80aea45d01fcbfd7",
"_journal/2024-02-29.md": "f610f3caed659c1de3eed5f226cab508", "_journal/2024-02-29.md": "f610f3caed659c1de3eed5f226cab508",
"_journal/2024-02/2024-02-28.md": "7489377c014a2ff3c535d581961b5b82", "_journal/2024-02/2024-02-28.md": "7489377c014a2ff3c535d581961b5b82",
"_journal/2024-03-01.md": "a532486279190b0c12954966cbf8c3fe", "_journal/2024-03-01.md": "a532486279190b0c12954966cbf8c3fe",
@ -382,7 +382,7 @@
"_journal/2024-03/2024-03-15.md": "e54b2513beac5f46313b4c37622adf39", "_journal/2024-03/2024-03-15.md": "e54b2513beac5f46313b4c37622adf39",
"_journal/2024-03-17.md": "72e99c7630085aee2c7f340a06b5ada7", "_journal/2024-03-17.md": "72e99c7630085aee2c7f340a06b5ada7",
"_journal/2024-03/2024-03-16.md": "ab7629c24ebe70838072cf6acec47cb0", "_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-18.md": "8479f07f63136a4e16c9cd07dbf2f27f",
"_journal/2024-03/2024-03-17.md": "23f9672f5c93a6de52099b1b86834e8b", "_journal/2024-03/2024-03-17.md": "23f9672f5c93a6de52099b1b86834e8b",
"set/directed-graph.md": "b4b8ad1be634a0a808af125fe8577a53", "set/directed-graph.md": "b4b8ad1be634a0a808af125fe8577a53",
@ -783,11 +783,11 @@
"c17/enums.md": "9414fb67aa256a0a11b7240534c67bf6", "c17/enums.md": "9414fb67aa256a0a11b7240534c67bf6",
"c17/derived-types.md": "6fb8f23a2423f05d5bdccb6672a32e38", "c17/derived-types.md": "6fb8f23a2423f05d5bdccb6672a32e38",
"c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875", "c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875",
"c17/types/simple.md": "0fab7afc623e441cd17ae23497f6babe", "c17/types/simple.md": "0edbd5da2138aa46669135a03b935db9",
"c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d", "c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d",
"c17/types/derived.md": "aff0d2b6d218fb67af3cc92ead924de3", "c17/types/derived.md": "aff0d2b6d218fb67af3cc92ead924de3",
"c17/types/basic.md": "5064e21e683c0218890058882e06b6f3", "c17/types/basic.md": "5064e21e683c0218890058882e06b6f3",
"c17/types/index.md": "7f8541016f9ac91cc6477fe864fdccd3", "c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b",
"_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2", "_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2",
"_journal/2024-08/2024-08-24.md": "563fad24740e44734a87d7c3ec46cec4", "_journal/2024-08/2024-08-24.md": "563fad24740e44734a87d7c3ec46cec4",
"algebra/abs-val.md": "a47bc08db62304eb526d15ede3e300cf", "algebra/abs-val.md": "a47bc08db62304eb526d15ede3e300cf",
@ -844,7 +844,10 @@
"_journal/2024-09/2024-09-25.md": "c8a3414e27c8ce635fe995c2dfbf6019", "_journal/2024-09/2024-09-25.md": "c8a3414e27c8ce635fe995c2dfbf6019",
"c17/macros.md": "f7bade849d2cf714cd2ff79c4ed003d4", "c17/macros.md": "f7bade849d2cf714cd2ff79c4ed003d4",
"_journal/2024-09-27.md": "dd82b2c5c5389b6a35c4c2fcf857417c", "_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": { "fields_dict": {
"Basic": [ "Basic": [

View File

@ -4,6 +4,10 @@ title: "2024-09-28"
- [ ] Anki Flashcards - [ ] Anki Flashcards
- [x] KoL - [x] KoL
- [ ] OGS - [x] OGS
- [ ] Sheet Music (10 min.) - [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story) - [ ] 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.

View File

@ -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).
<!--ID: 1707432641557-->
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).
<!--ID: 1708366788645-->
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
<!--ID: 1708366788648-->
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.
<!--ID: 1707432641557-->
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.
<!--ID: 1707432641560-->
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.
<!--ID: 1707432641562-->
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.
<!--ID: 1707432641591-->
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.
<!--ID: 1707432641592-->
END%%

View File

@ -312,6 +312,20 @@ Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70
<!--ID: 1724543977383--> <!--ID: 1724543977383-->
END%% 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).
<!--ID: 1727551341581-->
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).
<!--ID: 1727551341582-->
END%%
%%ANKI %%ANKI
Cloze Cloze
The {real} types contrast the {complex} types. The {real} types contrast the {complex} types.

View File

@ -61,28 +61,26 @@ END%%
%%ANKI %%ANKI
Basic 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.
<!--ID: 1707835869737-->
END%%
%%ANKI
What two variants does a C integral type declaration have? What two variants does a C integral type declaration have?
Back: Signed and unsigned. 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1727551341544-->
END%% END%%
%%ANKI %%ANKI
Basic
What does it mean for an integer to be "signed"? What does it mean for an integer to be "signed"?
Back: It can represent negative, zero, and positive values. 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1727551341545-->
END%% END%%
%%ANKI %%ANKI
Basic
What does it mean for an integer to be "unsigned"? What does it mean for an integer to be "unsigned"?
Back: It can only represent nonnegative values. 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1727551341546-->
END%% END%%
%%ANKI %%ANKI
@ -206,7 +204,7 @@ END%%
%%ANKI %%ANKI
Cloze 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. Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707493017242--> <!--ID: 1707493017242-->
END%% END%%
@ -265,19 +263,17 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
<!--ID: 1723859121972--> <!--ID: 1723859121972-->
END%% END%%
### Unsigned ### Unsigned Integers
These correspond to nonnegative integer values. | Name | Narrow | Rank | Minimum Width |
| -------------------- | ------ | ---- | ------------- |
| Name | Narrow | Rank | | `bool` | Yes | 0 | 1 |
| -------------------- | ------ | ---- | | `char` (maybe) | Yes | 1 | - |
| `bool` | Yes | 0 | | `unsigned char` | Yes | 1 | 8 |
| `char` (maybe) | Yes | 1 | | `unsighed short` | Yes | 2 | 16 |
| `unsigned char` | Yes | 1 | | `unsigned int` | No | 3 | 16 |
| `unsighed short` | Yes | 2 | | `unsigned long` | No | 4 | 32 |
| `unsigned int` | No | 3 | | `unsigned long long` | No | 5 | 64 |
| `unsigned long` | No | 4 |
| `unsigned long long` | No | 5 |
%%ANKI %%ANKI
Basic Basic
@ -351,21 +347,154 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
<!--ID: 1723859122023--> <!--ID: 1723859122023-->
END%% 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).
<!--ID: 1727551341547-->
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).
<!--ID: 1727551341548-->
END%%
| Name | Narrow | Rank | %%ANKI
| -------------------- | ------ | ---- | Basic
| `char` (maybe) | Yes | 1 | What is the minimum width of an `unsigned char`?
| `signed char` | Yes | 1 | Back: $8$
| `signed short` | Yes | 2 | Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
| `signed int` | No | 3 | <!--ID: 1727551341549-->
| `signed long` | No | 4 | END%%
| `signed long long` | No | 5 |
| `float` | - | - | %%ANKI
| `double` | - | - | Basic
| `long double` | - | - | What is the minimum precision of an `unsigned char`?
Back: $8$
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1727551341550-->
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).
<!--ID: 1727551341551-->
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).
<!--ID: 1727551341552-->
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).
<!--ID: 1727551341553-->
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).
<!--ID: 1727551341554-->
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).
<!--ID: 1727551341555-->
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).
<!--ID: 1727551341556-->
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).
<!--ID: 1727551341557-->
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).
<!--ID: 1727551341558-->
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).
<!--ID: 1727551341559-->
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).
<!--ID: 1727551341560-->
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).
<!--ID: 1727552157085-->
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).
<!--ID: 1727552157086-->
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 %%ANKI
Basic Basic
@ -431,6 +560,117 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
<!--ID: 1723859122080--> <!--ID: 1723859122080-->
END%% 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).
<!--ID: 1727551341561-->
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).
<!--ID: 1727551341562-->
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).
<!--ID: 1727551341563-->
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).
<!--ID: 1727551341564-->
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).
<!--ID: 1727551341565-->
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).
<!--ID: 1727551341566-->
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).
<!--ID: 1727551341567-->
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).
<!--ID: 1727551341568-->
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).
<!--ID: 1727551341569-->
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).
<!--ID: 1727551341570-->
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).
<!--ID: 1727551341571-->
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).
<!--ID: 1727552157087-->
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).
<!--ID: 1727552157088-->
END%%
### Literals ### 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: 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
<!--ID: 1714677608769--> <!--ID: 1714677608769-->
END%% 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 | | Suffix | Type |
| ------ | -------------------- | | ------ | -------------------- |
@ -826,7 +1066,7 @@ END%%
### Literals ### 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 | | Suffix | Type |
| ------ | ------------- | | ------ | ------------- |
@ -905,6 +1145,117 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1723938382436--> <!--ID: 1723938382436-->
END%% END%%
%%ANKI
Cloze
`INT_MAX` is to {`<limits.h>`} whereas `DBL_MAX` is to {`<float.h>`}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1727552157090-->
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).
<!--ID: 1727552157091-->
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).
<!--ID: 1727552157092-->
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).
<!--ID: 1727552157093-->
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 {`<stdint.h>`} library contains {exact-width integer types}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1727551341572-->
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).
<!--ID: 1727551341573-->
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).
<!--ID: 1727551341574-->
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).
<!--ID: 1727551341575-->
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).
<!--ID: 1727551341576-->
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).
<!--ID: 1727551341577-->
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).
<!--ID: 1727551341578-->
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).
<!--ID: 1727551341579-->
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).
<!--ID: 1727551341580-->
END%%
## Enumerated Types ## Enumerated Types
An `enum` is a mapping of identifiers with integer values. They have general form: An `enum` is a mapping of identifiers with integer values. They have general form:

View File

@ -1,14 +1,90 @@
--- ---
title: Endianness title: Binary
TARGET DECK: Obsidian::STEM TARGET DECK: Obsidian::H&SS
FILE TAGS: binary::endian FILE TAGS: binary
tags: tags:
- binary - binary
- endian
--- ---
## Overview ## 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).
<!--ID: 1707432641557-->
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).
<!--ID: 1708366788645-->
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
<!--ID: 1708366788648-->
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.
<!--ID: 1707432641557-->
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.
<!--ID: 1707432641560-->
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.
<!--ID: 1707432641562-->
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.
<!--ID: 1707432641591-->
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.
<!--ID: 1707432641592-->
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. 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 %%ANKI

View File

@ -33,6 +33,8 @@ Declaration | Sign Bit | Exponent Field | Fractional Field
`float` | `1` | `8` | `23` `float` | `1` | `8` | `23`
`double` | `1` | `11` | `52` `double` | `1` | `11` | `52`
The **precision** of a floating-point type refers to the number of bits found in the fractional field.
%%ANKI %%ANKI
Basic Basic
In base-10 scientific notation, what form do nonzero numbers take on? 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
<!--ID: 1710672470805--> <!--ID: 1710672470805-->
END%% 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).
<!--ID: 1727552157081-->
END%%
## Rounding ## 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: 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 %%ANKI
Basic Basic
Is $+^f$ commutative? Is $+^f$ commutative?
Back: Yes. Back: No.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1710680824810--> <!--ID: 1710680824810-->
END%% END%%

View File

@ -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: 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$ |
-------- | ------- | -------- | ------------ | -------- | ------- | -------- | ------------ |
$UMin_w$ | `0x00` | `0x0000` | `0x00000000` | $UMin_w$ | `0x00` | `0x0000` | `0x00000000` |
$UMax_w$ | `0xFF` | `0xFFFF` | `0xFFFFFFFF` | $UMax_w$ | `0xFF` | `0xFFFF` | `0xFFFFFFFF` |
$TMin_w$ | `0x80` | `0x8000` | `0x80000000` | $TMin_w$ | `0x80` | `0x8000` | `0x80000000` |
$TMax_w$ | `0x7F` | `0x7FFF` | `0x7FFFFFFF` | $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).
<!--ID: 1707835869737-->
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).
<!--ID: 1727551341542-->
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).
<!--ID: 1727551341543-->
END%%
%%ANKI %%ANKI
Basic Basic