1
CS 61C, Fall 2024 @ UC Berkeley
Slides credit: Dan Garcia, Justin Yokota, Peyrin Kao
Slides template credit: Josh Hug, Lisa Yan
Number Representation
Lecture 1
I don't know what this is, but it was here last semester. Enjoy.
Course Logistics
Speedrunning this in live lecture.
You can read through this later at your own pace.
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
Joining the Course
Course Format
Attendance will not be part of your grade.
However…
Communication
Course website: https://cs61c.org
Email: cs61c@berkeley.edu
Course Structure: Lectures
You made it here, congrats!
Course Schedule This Week
Labs, discussions, and office hours start next week (Sep 3).
Lab 0 will be released this week and there will be no lab sections since it is setup.
Course Structure: Discussions
Smaller, 1-hour long sections led by a TA to practice course content.
Course Structure: Labs
2 hours long, with 30 min–1 hour conceptual mini-lecture from a TA, and remaining time office hour style Q&A.
Course Structure: Projects
There are 4 projects.
Course Structure: Staff Support
Ed discussion forum:
Note: We usually provide increased support for projects (in the form of increased staffing) leading up to the posted, original deadline.
Course Structure: Exams
Dates:
Policies:
Grading Breakdown
Homework: 10%
Labs: 10%
Projects: 40%
Midterm: 16%
Final: 24%
Class Policies: Collaboration
Collaboration on assignments:
Limits of collaboration:
Class Policies: Academic Misconduct
BOTH the sender and the receiver of work will be penalized.
Out of ~600 students who took 61C last spring, ~100 students were caught.
Class Policies: Academic Misconduct
We're here to help! There are plenty of staff and resources available for you.
Academic misconduct policies:
Choose your partners wisely!
Stress Management, Mental Health and DSP
We want to reduce your stress where we can.
If you have a DSP letter, please submit it through the DSP portal (AIM) as soon as possible.
Extensions:
Stress Management and Mental Health
We want to reduce your stress where we can.
Your well-being is more important than this course.
If you feel overwhelmed, there are options:
Stress Management and Mental Health
Life happens.
Class Policies: DSP
Disabled Students' Program (DSP):
Our goal is to teach you the material in our course. The more accessible we can make it, the better.
If you have a DSP letter, please submit it through the DSP portal (AIM) as soon as possible.
Big Idea: Everything is Bits
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
Big Idea: Bits Can Represent Anything
All data in your computer is stored as bits, sequences of 0s and 1s.
Everything can be represented as bits.
We should all agree on the representation.
Representing Things as Bits
If we have N bits, we can represent 2N different things.
Why?
N times
21 | 2 |
22 | 4 |
23 | 8 |
24 | 16 |
25 | 32 |
26 | 64 |
27 | 128 |
28 | 256 |
29 | 512 |
210 | 1024 |
211 | 2048 |
212 | 4096 |
Powers of 2. You will memorize them from using them over and over in this class.
Representing Things as Bits
Today's goal: Representing integers as bits.
To represent integers as bits, we'll first take a detour and think about number bases...
Numbers in Different Bases
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
Decimal System (Base 10)
The base-10 digits: 0 1 2 3 4 5 6 7 8 9
Larger numbers use multiple digits.
457610 = (4 × 103) + (5 × 102) + (7 × 101) + (6 × 100)
Base-10.
Octal System (Base 8)
The base-8 digits: 0 1 2 3 4 5 6 7
Larger numbers use multiple digits.
45768 = (4 × 83) + (5 × 82) + (7 × 81) + (6 × 80)
= 243010
To convert bases to base-10: Write out the powers of that base (e.g. powers of 8).
Base-8.
Maybe if we had 8 fingers instead of 10.
Hexadecimal System (Base 16)
The base-16 digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F
(10) (11) (12) (13) (14) (15)
Larger numbers use multiple digits.
4AC216 = (4 × 163) + (10 × 162) + (12 × 161) + (2 × 160)
= 1913810
To convert bases to base-10: Write out the powers of that base (e.g. powers of 16).
Base-16.
We use letters if we need more digits.
Converting to Base 10
Base 10
Other Base
Write out powers of the base, e.g.
(10 × 162) + (12 × 161) + (2 × 160)
Use the "leftover algorithm."
Let's see it in action.
Not an official name.
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
| | | | | |
73 items to put in boxes.
The box sizes we can use:
1024, 256, 64, 16, 4, 1.
Rules:
If you use a box, you have to fill it up.
Use as few boxes as possible.
Write how many boxes of each size we use.
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
| | | | | |
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | | | | | |
How many size-1024 boxes should we use?
0. (Remember, all boxes must be full.)
Still 73 items left.
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | | | | | |
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | | | | |
How many size-256 boxes should we use?
0.
Still 73 items left.
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | | | | |
How many size-64 boxes should we use?
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | | | |
Box of 64
1. Fits 64 items.
73 – 64 = 9 items left.
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | | | |
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | 0 | | |
How many size-16 boxes should we use?
Box of 64
0.
Still 9 items left.
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | 0 | | |
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | 0 | 2 | |
How many size-4 boxes should we use?
Box of 4
Box of 64
Box of 4
2.
9 – (2 × 4) = 1 item left.
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | 0 | 2 | |
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | 0 | 2 | 1 |
How many size-1 boxes should we use?
Box of 4
Box of 64
Box of 4
Box of 1
1.
1 – 1 = 0 items left!
"Leftover Algorithm" for Converting to Base-10
Convert 7310 to base-4.
45�1024s place | 44�256s place | 43�64s place | 42�16s place | 41 4s place | 40 Ones place |
0 | 0 | 1 | 0 | 2 | 1 |
0 items left. We're done!
7310 = 0010214 (or just 10214).
Box of 4
Box of 64
Box of 4
Box of 1
"Leftover Algorithm" for Converting to Base-10
The "leftover algorithm" converts from other bases to base-10.
Commonly Used Bases
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
Commonly Used Bases
Some commonly used bases in computer science:
Commonly Used Bases – Notation
Writing numbers is ambiguous when we have different bases.
Disambiguation rules for this class:
We sometimes add spaces for clarity: 0b1011 1001 1010 1111
If we can't figure out what base you're using on an exam, we will be sad and take off points.
Weird, but true. We basically never use this in 61C.
Converting Between Commonly-Used Bases
Binary
Base 2
Hexadecimal
Base 16
Decimal
Base 10
Leftover algorithm.
Sum up powers of 2.
Leftover algorithm.
Sum up powers of 16.
Use the table!
Converting Between Hexadecimal and Binary – Use the Table
Each 4-bit sequence corresponds to one hex digit!
Why?
To convert between hex and binary, just use the table!
Hexadecimal is a convenient shorthand for long sequences of bits.
Decimal | Binary | Hex |
0 | 0000 | 0 |
1 | 0001 | 1 |
2 | 0010 | 2 |
3 | 0011 | 3 |
4 | 0100 | 4 |
5 | 0101 | 5 |
6 | 0110 | 6 |
7 | 0111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
10 | 1010 | A |
11 | 1011 | B |
12 | 1100 | C |
13 | 1101 | D |
14 | 1110 | E |
15 | 1111 | F |
Converting Between Hexadecimal and Binary – Use the Table
Beware of padding with zeros!
Binary → hex: Left-pad if needed.
Hex → binary: Drop leading zeroes if needed.
Decimal | Binary | Hex |
0 | 0000 | 0 |
1 | 0001 | 1 |
2 | 0010 | 2 |
3 | 0011 | 3 |
4 | 0100 | 4 |
5 | 0101 | 5 |
6 | 0110 | 6 |
7 | 0111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
10 | 1010 | A |
11 | 1011 | B |
12 | 1100 | C |
13 | 1101 | D |
14 | 1110 | E |
15 | 1111 | F |
Units of Measurement
Groups of bits have special names.
Or "nybble" if you want to be cute.
Unsigned Integers
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
Unsigned Integers: Properties
We now have a way to represent numbers as bits!
Resulting data type: N-bit unsigned integer.
N = 32 is common in computers. Can represent [0, 232 – 1], roughly 4 billion numbers.
Useful if you don't need negative numbers.
Unsigned Integers: Doing Math
Math in base-10: Just like you learned in elementary school.
5 0 9 8
+ 6 8 1 7
1 1 9 1 5
1
1
1
Unsigned Integers: Doing Math
Math in base-2 is basically the same.
1 1 1 1
+ 0 1 1 0
1 0 1 0 1
In binary:
1
1
1
Unsigned Integers: Number Line View
Overflow occurs when we exceed the largest value and wrap back around to 0.
Negative overflow occurs when we try to go below 0 and wrap around to large values.
1000
1001
1010
1011
1100
1111
1110
1101
0000
0001
0010
0011
0100
0101
0110
0111
Overflow!
The odometer: Counting upwards in base-2.
Unsigned Integers: Pros and Cons
Some desirable properties of number representation systems:
Unsigned Integer | |
Can represent negative numbers. | ✘ |
Doing math is easy. | ✔ |
Every bit sequence represents a unique number. | ✔ |
Sign-Magnitude
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
Sign-Magnitude: Properties
Idea: Use the left-most bit to indicate if the number is positive (0) or negative (1).
This is called sign-magnitude representation.
Notice: There's no free lunch.
Sign-Magnitude: Number Line View
The number line is wacky in sign-magnitude!
The odometer...
0001
0010
0011
0100
0101
0111
0110
1111
1110
1101
1100
1011
1010
1001
1000
...acts wacky.
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
Sign-Magnitude: Pros and Cons
Problems:
Sign-magnitude is rarely used as-is in practice.
Sign-Magnitude | |
Can represent negative numbers. | ✔ |
Doing math is easy. | ✘ |
Every bit sequence represents a unique number. | ✘ |
Number line was wacky.
Two representations of zero.
One's�Complement
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
One's Complement: Properties
Idea: If the number is negative, flip the bits.
This is called one's complement representation.
One's Complement: Number Line View
The number line is no longer wacky in one's complement.
The odometer...
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
...isn't wacky anymore!
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
One's Complement: Pros and Cons
Solved some of our problems:
One's complement is not used in practice, but it leads to our next solution...
One's Complement | |
Can represent negative numbers. | ✔ |
Doing math is easy. | ✔ |
Every bit sequence represents a unique number. | ✘ |
Two representations of zero.
Two's Complement
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
From One's Complement to Two's Complement
Our remaining problem: We have two representations of zero.
To fix it, we can shift all the negative representations over by one.
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
–8
Two's Complement: Properties
Idea: If the number is negative, flip the bits, and add one.
This is called two's complement representation.
Because we shifted to avoid double-zero.
Two's Complement: Number Line View
Just like its cousin, one's complement, the number line isn't wacky.
The odometer...
...isn't wacky anymore!
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
–8
Two's Complement: Properties
Another definition: The left-most power of 2 is now negative, not positive.
–25�–32s place | 24�16s place | 23�8s place | 22�4s place | 21 2s place | 20 Ones place |
1 | 0 | 1 | 0 | 0 | 1 |
The odometer counts...
Eventually, the left-most bit flips from 0 to 1.
This causes the number to become negative!
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
–8
Two's Complement: Conversion Algorithm
To convert two's complement to a signed decimal number:
Example: What is 0b1110 1100 in decimal?
Two's Complement: Conversion Algorithm
To convert a signed decimal number to two's complement:
Example: What is –20 in two's complement binary?
Two's Complement: Pros and Cons
Solves our sign-magnitude problems:
Two's complement is the most common way to represent signed integers.
Two's Complement | |
Can represent negative numbers. | ✔ |
Doing math is easy. | ✔ |
Every bit sequence represents a unique number. | ✔ |
Because the number line isn't wacky.
Because we shifted.
Deep connection to modular arithmetic.
[Optional] Two's Complement: Deep Connection to Modular Arithmetic
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
–8
1
2
3
4
5
7
6
9
10
11
12
13
14
15
0
8
Two's Complement
Unsigned
23�8s place | 22�4s place | 21 2s place | 20 Ones place |
1 | 0 | 1 | 1 |
–23�–8s place | 22�4s place | 21 2s place | 20 Ones place |
1 | 0 | 1 | 1 |
Bit sequence interpreted as unsigned:
The same sequence interpreted as two's complement:
= 11
= –5
These are always equal mod 2N = 16.
[Optional] Two's Complement: Deep Connection to Modular Arithmetic
Punchline: Unsigned math and two's complement math basically work the same.
*Not really magical, just mathematical. It does have a nice ring to it.
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
–8
1
2
3
4
5
7
6
9
10
11
12
13
14
15
0
8
Two's Complement
Unsigned
Bias Notation
Lecture 1, CS 61C, Fall 2024
Course Logistics
Big Idea: Everything is Bits
Numbers in Different Bases
Representing Integers�(Including Negatives)
Why Use Bias?
Why not stop at two's complement? Some oddities:
The overflow in the middle of the odometer is kind of weird.
0001
0010
0011
0100
0101
0111
0110
1000
1001
1010
1011
1100
1101
1110
1111
0000
1
2
3
4
5
7
6
–7
–6
–5
–4
–3
–2
–1
0
–8
Introducing Bias Notation
What if I wanted to represent a different range of values?
...and shift them over to the desired range!
Idea: Take the unsigned numbers...
000
001
010
011
100
101
110
111
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unrepresentable
000
001
010
011
100
101
110
111
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unrepresentable
unrepresentable
Introducing Bias Notation
We can also take the unsigned numbers...
...and shift them to center around zero.
000
001
010
011
100
101
110
111
–7
–6
–5
–4
–3
–2
–1
0
1
2
3
4
5
6
7
unrepresentable
Odometer counts up continuously!
000
001
010
011
100
101
110
111
–7
–6
–5
–4
–3
–2
–1
0
1
2
3
4
5
6
7
unrepresentable
unrepresentable
0b000 represents the smallest number!
Bias Notation: Properties
Idea: Just like unsigned, but shifted on the number line.
This is called bias representation.
We can pick a bias based on what numbers we want to represent.
0
0
0
To represent negative numbers,�pick a very negative bias.
To center at 0, pick the�standard bias of –(2N–1 – 1).
To represent positive numbers,�pick a very positive bias.
Bias Notation: Conversion Algorithm
To convert biased notation to decimal:
Example: Assuming standard bias, what is 0b00000001 in decimal?
Intuition:
Bias Notation: Conversion Algorithm
To convert decimal to biased notation:
Example: What is –126 in 8-bit biased notation?
Note: In this class, if we don't specify the bias, assume the standard bias.
Yes, I know subtracting a negative bias is annoying, but that's the convention we use. Sorry.
Bias Notation: Pros and Cons
Problems:
Really useful if the range of values you want to represent is not centered at 0.
Bias Notation | |
Can represent negative numbers. | ✔ |
Doing math is easy. | ✘ |
Every bit sequence represents a unique number. | ✔ |
Summary: Number Representation
Everything is represented as sequences of bits in the computer.
We converted numbers between common bases: binary, decimal, hexadecimal.
We saw different ways to represent integers as bits:
| Handling negatives: | Easy to math? | No double zero? | Widely used? | Smallest Number: | Largest Number: | ||
Unsigned | N/A | ✔ | ✔ | ✔ | 0b0000...000 | 0 | 0b1111...111 | 2N – 1 |
Sign-Magnitude | Left-most bit�is the sign. | ✘ | ✘ | ✘ | 0b1111...111 | –(2N – 1 – 1) | 0b0111...111 | +(2N – 1 – 1) |
One's Complement | Flip the bits. | ✔ | ✘ | ✘ | 0b1000...000 | –(2N – 1 – 1) | 0b0111...111 | +(2N – 1 – 1) |
Two's Complement | Flip the bits, +1. | ✔ | ✔ | ✔ | 0b1000...000 | –(2N – 1) | 0b0111...111 | +(2N – 1 – 1) |
Bias Notation | Bias = –(2N–1 – 1). | ✘ | ✔ | ✔ | 0b0000...000 | bias | 0b1111...111 | 2N – 1 + bias |
If you're here to copy this on your cheat sheet, make sure you actually understand how to derive the formulas, or else they won't help on exams.