[CS61C FA20] Lecture 02.4 - Number Representation: Two's Complement — Transcript
Full transcript
- 0:00and welcome back. Now let's see if we
- 0:03can figure out what we can do to fix
- 0:06one's complement. Is there anything we
- 0:08can do? And guess what? It's called
- 0:10two's compliment. We'll also talk about
- 0:11something called biased encoding.
- 0:14So here's the idea. The problem is the
- 0:16negative numbers overlap with the
- 0:18positive numbers. We saw that. We don't
- 0:19want that. We want to shift the bottom
- 0:21over one. So here's what we do. Just
- 0:25like before, all the zeros are on the
- 0:27top. So Z like in the five bits before
- 0:29all zeros up to 01111 that's all on the
- 0:33top that's 0 through 15 that's the same.
- 0:35So it turns out the top never really
- 0:36changes. Doesn't change actually zero to
- 0:38all ones doesn't change in unsigned sign
- 0:41magnitude one's complement or this two's
- 0:44complement. So that's kind of the top
- 0:45stays the same. It's what happens on the
- 0:47bottom
- 0:49except all ones. We don't want it to be
- 0:51zero. We want all ones to be negative
- 0:53one. And in fact I want you to lock that
- 0:55in. All ones is always negative ones in
- 0:57this two's complement number. Okay? It's
- 1:00called two's complement. The hardware is
- 1:01now really really really easy. We went
- 1:03from being complicated because there's
- 1:05two zeros to really easy actually. Um,
- 1:08by the way, if you're a C programmer,
- 1:09we'll teach you in this next lecture.
- 1:11Um, you're going to see that in C it's
- 1:12called an int. Again, we know how how
- 1:14wide in ints are. So, we had this thing
- 1:16called intypes. We'll tell you int i for
- 1:19integer. Int with a number 8, 16 or 64.
- 1:238, 16, 32, 64, etc. is a sign integer.
- 1:26And this is two's complement. So
- 1:28whenever you think of a signed integer,
- 1:30the sign means we're doing this choose
- 1:32complement signing to be able to handle
- 1:33negative numbers. Okay, unsigned means
- 1:35only positive numbers or only non-
- 1:37negatives, zero or positive numbers.
- 1:40Okay, here's the formula. Here's the way
- 1:42to think of it. Okay, so for 32 bits,
- 1:46the same as before. All the lower bits
- 1:47are the same, right? Lowest the lowest u
- 1:52the lowest coefficient times one. the
- 1:55second lowest, second lowest bit times
- 1:57two, the third lowest bit times four,
- 1:59etc. Except that you take this big guy
- 2:04and you say it's multiplied the
- 2:06coefficient, but of the high a bit is
- 2:08it's that kind of that sign bit. Really,
- 2:10it's not really a sign bit because not
- 2:10sign magnitude, but we think about it as
- 2:12a sign bit because the negative numbers
- 2:14all have a one there. So we'll think
- 2:15about as a sign bit for this case, even
- 2:16though it's not sign magnitude times
- 2:19minus 2 31 minus 2 to the biggest
- 2:23biggest term. So, let's do this simple.
- 2:24Let's go back to a nibble now. Not five
- 2:26bits. Go back to a nibble. Four bits.
- 2:28[snorts] 11 101. Well, 11 101 in
- 2:31unsigned. Well, in hex 11 is D. Memorize
- 2:35that guy. Okay, D. That was going to be
- 2:3713. What is it? If we're talking about
- 2:41two's complement or sign a signed
- 2:43number, two's complement. Let's do it
- 2:45again. Well, this is 101. So, this is
- 2:48the that's two nos four no two one one.
- 2:52And this is -8.
- 2:56So watch this. Here's your five.
- 2:58Remember 101 is five minus 8. There's
- 3:02minus3. Okay, that's one way to think
- 3:04about it. Minus3.
- 3:09Another way to think about it is flip
- 3:11the bits and add one. And that's the
- 3:13equivalent number positively. So let's
- 3:15do this. Let's flip the bits and add
- 3:17one.
- 3:19So here we go. 11 101. Flip the bits.
- 3:25O10 add one. O11. That's the negative of
- 3:29it. So if that's a positive number, now
- 3:30it's three. That means 11 101 must have
- 3:32been -3. Let's do that. Let's reverse
- 3:35it. Now flip the bits again.
- 3:3811 10 add 1 11 101. And that's3. So I
- 3:42went from -3 to 3 to minus 3. By the
- 3:45way, this picture is an optical
- 3:48illusion. Some people see a vase. Some
- 3:50people see a face. Sometimes it flops
- 3:51between a vase and a face. The point of
- 3:53that showing you this is I can think of
- 3:55this. The way I do it, the way's a pro
- 3:57tip, the way that I think about negative
- 4:00numbers and how to do them fast is I
- 4:02actually see when I say, okay, this
- 4:04leading one now I immediately see this
- 4:07and I flip. I treat every one as a zero
- 4:10and a 0 one, which means I basically do
- 4:11this in my head. Okay? And then I just
- 4:14quickly add one. I say it's been
- 4:15negative three. So I say to myself, oh
- 4:17here's this. Now if this is the only one
- 4:20and I add one, it's minus3. See that?
- 4:23Because I quickly flipped it thinking
- 4:25that's the only one. And if I add one to
- 4:27that, I get 011. And then I go back and
- 4:29I say what's what's that in decimal?
- 4:30It's three. So I'm able to really fast
- 4:33think about how to do that fast for
- 4:34yourself.
- 4:36So flip the bits and add one.
- 4:41So here we got a number line. This
- 4:42beautiful number line. As I count
- 4:45around, as I'm counting around here in
- 4:48my binary odometer, I've taken my
- 4:49odometer and kind of made it round.
- 4:51Here's the numbers that are being
- 4:52represented. As I do this, 0 1 2 3 up to
- 4:5515. This is the same. This is always the
- 4:57same. Just like the positive numbers are
- 4:59always the same. 0 through 15. That's
- 5:01been that way for every representation
- 5:02we've seen so far. All zeros through
- 5:04zero and all ones is zero through here
- 5:06in case 15 for five bits. Then
- 5:10it jumps to minus 16.
- 5:14jumps to -6. And you might remember this
- 5:17last slide. Watch this slide here.
- 5:19Remember this. This is the negative
- 5:21number. That's this minus 8 that pulls
- 5:24it back by doing minus minus 16.
- 5:28It took this this representation which
- 5:31used to be in one's complement -15 and
- 5:33then all the ones was here. We didn't
- 5:35want that. We wanted to bring it back
- 5:37one more. So that's why
- 5:41that term that top level term -6 pulls
- 5:43that down so that this is the -6 pulling
- 5:46that value down to -6. If I then stretch
- 5:4916 more guys or 15 16 more guys there
- 5:52this will end up at minus one right -6
- 5:54to minus1 is 16 things. So that's why
- 5:57that works that cool that cool top level
- 5:59term this cool top level term it's
- 6:01because you grabbed that negative guy
- 6:03that smallest negative number and pulled
- 6:06it over by exactly one in one's comp was
- 6:08-5 and now it's -6 which is why why it
- 6:11works. So, let's look at our odometer
- 6:14now. And let's see if our we have it.
- 6:15Let's see if our overlap here. Ready?
- 6:17There we go. There's 0 to 15. And no
- 6:21overlap. And as I said before, all ones
- 6:24is negative one. Huh? With the thing.
- 6:27And then there's all ones negative 1.
- 6:30Love it. Okay.
- 6:32Two to the n minus one non- negatives.
- 6:34Two to the n minus one negatives. One
- 6:36zero. Okay. How many positives? Ask
- 6:39yourself that. And then we'll see this
- 6:41next encoding.
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 02.4 - Number Representation: Two's Complement by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,230 words across 168 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.