Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added content/number_rep/astronaut-alien.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 60 additions & 1 deletion content/number_rep/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,63 @@ title: "Number Representation"
subtitle: "UC Berkeley, CS 61C"
---

## TODO
![](astronaut-alien.png){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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change . to ?

**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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the decimal number and/or 116 -> $116_{10}$


::: {.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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log => $\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)
78 changes: 77 additions & 1 deletion content/number_rep/signed.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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 $n-1$? maybe i am just not understanding

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have an example from discussion slides that we use here?

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
:::


9 changes: 6 additions & 3 deletions content/number_rep/unsigned.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for example, but up to Lisa discretion

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have an example from discussion slides that we use here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CleanShot 2025-11-18 at 12 51 57@2x

$$
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.