-
Notifications
You must be signed in to change notification settings - Fork 0
feat(num-rep): release #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,63 @@ title: "Number Representation" | |
| subtitle: "UC Berkeley, CS 61C" | ||
| --- | ||
|
|
||
| ## TODO | ||
| {fig-alt="A astronaut talking to an alien with 2 fingers per hand and there are 4 rocks on the ground. Alien says | ||
| 'There are 10 rocks.' Astronaut says 'Oh, you must be using base 4. See, I use base 10.' Alien says 'No. I use base 10. What is base 4?' Caption reads 'Every base is base 10'"} | ||
|
|
||
| ## Precheck Questions | ||
| 1. **True/False**: Depending on the context, the same sequence of bits may represent different things. | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| **True.** The same bits can be interpreted in many different ways with the exact same bits! The bits | ||
| can represent anything from an unsigned number to a signed number or even, as we will cover | ||
| later, a program. It is all dependent on its agreed upon interpretation. | ||
| ::: | ||
|
|
||
| 2. **True/False**: If you interpret a $N$-bit Two's complement number as an unsigned number, negative numbers would be smaller than | ||
| positive numbers. | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| **False.** In Two’s Complement, the MSB is always 1 for a negative number. This means EVERY | ||
| negative number in Two’s Complement, when converted to unsigned, will be larger than the | ||
| positive numbers. | ||
| ::: | ||
|
|
||
| 3. **True/False**: We can represent fractions and decimals in our given number representation formats (unsigned, biased, and Two’s Complement). | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| **False.** Our current representation formats has a major limitation; we can only represent and do | ||
| arithmetic with integers. To successfully represent fractional values as well as numbers with | ||
| extremely high magnitude beyond our current boundaries, we need another representation | ||
| format. | ||
| ::: | ||
|
|
||
| 4. How many numbers can be represented by an unsigned, base-4, $n$-digit number. | ||
| **A.** 1 | ||
| **B.** $2^n - 1$ | ||
| **C.** $4^n$ | ||
| **D.** $4^{n-1}$ | ||
| **E.** $4^n - 1$ | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| **C.** | ||
| ::: | ||
|
|
||
| 5. How many bits are needed to represent decimal number 116 in binary? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the decimal number and/or 116 -> |
||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| **7 bits**. $(116)_{10} =$ `0b111 0100` or $log{_2}{116} \approx 6.85$ which we round to 7 bits. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log => |
||
| ::: | ||
|
|
||
|
|
||
| ## Useful Links | ||
| [Fall 25 Slides](https://docs.google.com/presentation/d/1dmCk2fZz-P8VedzAXnVmJiYPKszVka5NKmTuLJ6hqZc/edit?slide=id.g3292f1ea280_3_0#slide=id.g3292f1ea280_3_0) | ||
| [Fall 25 Precheck](https://cs61c.org/fa25/pdfs/discussions/disc01/disc01-pre.pdf) | ||
| [Precheck Solutions](https://cs61c.org/fa25/pdfs/discussions/disc01/disc01-pre-sols.pdf) | ||
|
|
||
| ### Additional Reading | ||
| **CS Illustrated Number Rep Handout** | ||
|
|
||
| * [Comparing Binary Integer Representations](https://csillustrated.berkeley.edu/PDFs/handouts/integer-representations-1-handout.pdf) | ||
| * [Negation and Zeroes](https://csillustrated.berkeley.edu/PDFs/handouts/integer-representations-2-comparing-handout.pdf) | ||
| * [Increments and Monotonicity](https://csillustrated.berkeley.edu/PDFs/handouts/integer-representations-3-comparing-handout.pdf) | ||
| * [The Thrilling Conclusion!](https://csillustrated.berkeley.edu/PDFs/handouts/integer-representations-4-comparing-handout.pdf) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,82 @@ title: "Signed Representations" | |
| subtitle: "UC Berkeley, CS 61C" | ||
| --- | ||
|
|
||
| ## TODO | ||
| ## Precheck | ||
|
|
||
| Unsigned binary numbers work for natural numbers, but many calculations use negative numbers as well. To deal with this, a number of different schemes have been used to represent signed numbers. Here are two common schemes: | ||
|
|
||
| ### Two’s Complement | ||
|
|
||
| (a) We can write the value of an $n$-digit two's complement number as | ||
| $$ | ||
| \sum_{i=0}^{n-2} 2^i d_i - 2^{n-1} d_{n-1} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use smth less confusing here like -2^n(d_n) + \sum... (same as unsigned but up to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Propagate change to examples below too |
||
| $$ | ||
| (b) Negative numbers will have a 1 as their most significant bit (MSB). Plugging in $d_{n-1} = 1$ to the formula above gets us | ||
| $$ | ||
| \sum_{i=0}^{n-2} 2^i d_i - 2^{n-1} | ||
| $$ | ||
| (c) Meanwhile, positive numbers will have a 0 as their MSB. Plugging in $d_{n-1} = 0$ gets us | ||
| $$ | ||
| \sum_{i=0}^{n-2} 2^i d_i | ||
| $$ | ||
| which is very similar to unsigned numbers. | ||
|
|
||
| (d) To negate a two's complement number: flip all the bits and add 1. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i feel like i would put an example here, but up to Lisa's discretion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have an example from discussion slides that we use here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe so |
||
| (e) Addition is exactly the same as with an unsigned number. | ||
| (f) Only one 0, and it’s located at `0b0`. | ||
|
|
||
| ### Biased Representation | ||
| (a) The number line is shifted so that the smallest number we want to be representable would be `0b0...0`. | ||
| (b) To find out what the represented number is, read the representation as if it was an unsigned number, then add the bias. | ||
| (c) We can shift to any arbitrary bias we want to suit our needs. To represent (nearly) as much negative numbers as positive, a commonly-used bias for $N$-bits is $-(2^{N-1} - 1)$. | ||
|
|
||
| ## Summary & Questions | ||
|
|
||
| You should be able to do: | ||
|
|
||
| * Conversion between binary, decimal, and hexademical | ||
| * Conversion with signed numbers | ||
| * Arithmetic with binary and hexadecimal numbers | ||
|
|
||
| ### Conceputal Review Questions | ||
| 1. What is a bit? How many bits are in a byte? Nibble? | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| A bit is the smallest unit of digital information and it can be either 0 of 1. There are 4 bits in a nibble and 8 bits in a byte. | ||
|
|
||
| See: [Lecture 2 Slide 13](https://docs.google.com/presentation/d/1dmCk2fZz-P8VedzAXnVmJiYPKszVka5NKmTuLJ6hqZc/edit?slide=id.g2af3b38b3e2_1_154#slide=id.g2af3b38b3e2_1_154) | ||
| ::: | ||
|
|
||
| 2. What is overflow? | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| When the result of an arithmetic operation is outside the range of what is representable by given number of bits. | ||
|
|
||
| See: [Lecture 2 Slide 26](https://docs.google.com/presentation/d/1dmCk2fZz-P8VedzAXnVmJiYPKszVka5NKmTuLJ6hqZc/edit?slide=id.g2af3b38b3e2_1_186#slide=id.g2af3b38b3e2_1_186) | ||
| ::: | ||
|
|
||
| 3. What is the range of numbers representable by $n$-bit unsigned, sign-magnitude, one's complement, two's complement, and biased notation? | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| * **Unsigned**: $[0, 2^n-1]$ | ||
| * **Sign-Magnitude**: $[-(2^{n-1} - 1), 2^{n-1} - 1]$ | ||
| * **One's complement**: $[-(2^{n-1} - 1), 2^{n-1} - 1]$ | ||
| * **Two's complement**: $[-2^{n-1}, 2^{n-1} - 1]$ | ||
| * **Bias**: $[0+$bias$, 2^n-1+$bias$]$ | ||
|
|
||
| See: [Lecture 2](https://docs.google.com/presentation/d/1dmCk2fZz-P8VedzAXnVmJiYPKszVka5NKmTuLJ6hqZc/edit?slide=id.g32e4dda2ba9_0_123#slide=id.g32e4dda2ba9_0_123) | ||
| ::: | ||
|
|
||
| 4. How many ways to represent zero do these representations have, $n$-bit unsigned, sign-magnitude, one's complement, two's complement, and biased notation? | ||
|
|
||
| ::: {.callout-note title="Answer" collapse="true"} | ||
| * **Unsigned**: 1 | ||
| * **Sign-Magnitude**: 2 | ||
| * **One's complement**: 2 | ||
| * **Two's complement**: 1 | ||
| * **Bias**: 1 or 0 (depending on bias) | ||
|
|
||
| See: [Lecture 2](https://docs.google.com/presentation/d/1dmCk2fZz-P8VedzAXnVmJiYPKszVka5NKmTuLJ6hqZc/edit?slide=id.g32e4dda2ba9_0_123#slide=id.g32e4dda2ba9_0_123) | ||
| ::: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,10 @@ title: "Unsigned Representations" | |
| subtitle: "UC Berkeley, CS 61C" | ||
| --- | ||
|
|
||
| ## TODO | ||
|
|
||
|
|
||
| ## Precheck | ||
| If we have an $n$-digit unsigned numeral $d_{n-1}$ $d_{n-2}$...$d_0$ in radix (or base) $r$, then the value of that numeral is: | ||
| $$ | ||
| \sum_{i=0}^{n-1} r^i d_i | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same for example, but up to Lisa discretion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have an example from discussion slides that we use here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| $$ | ||
| which is just fancy notation to say that instead of a 10's or 100's place we have an $r$'s or $r^2$'s place. For the three radices binary, decimal, and hex, we just let $r$ be 2, 10, and 16, respectively. | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change . to ?