[CS61C FA20] Lecture 02.3 - Number Representation: Overflow, Sign and Magnitude, One's Complement — Transcript
Full transcript
- 0:00and welcome back now let's talk about
- 0:03number representations
- 0:06what can we do with representations of
- 0:07numbers well we can
- 0:09do everything you can do you understand
- 0:11in decimal you can add them subtract the
- 0:13multiply divide compare them
- 0:15for example and by the way this is
- 0:16representation in binary
- 0:18how could we represent
- 0:22how could we do some addition here's 10
- 0:251 0 1
- 0:250 is 10. so 10 10 is 10 remember that
- 0:28memorize that guy
- 0:29and let's add that together to seven
- 0:30seven is one one one well let's do it
- 0:33together let's actually do it as we did
- 0:35you're back in first grade zero plus one
- 0:38one by the way the maximum value in any
- 0:41of those slots is a one in binary by the
- 0:43way this is these are all base two
- 0:45okay this is this is the base two and
- 0:46base two zero plus one one one plus one
- 0:49ready it's two which means zero
- 0:53carry the one because it's kind of the
- 0:54one bubbled over two
- 0:56and binary is 0. 1 plus 0 plus 1 is 2
- 0:590 plus 1 carries 1 plus 1 there again 2
- 1:030 and the 1 carries to here and you get
- 1:061 0 0 1. so
- 1:0910 plus 7 is 17. and this ladies and
- 1:13gentlemen is 17
- 1:14because that's 16 plus a 1.
- 1:18now if i only had
- 1:21four bits to do this i wouldn't have had
- 1:23enough room to store that number we're
- 1:24going to see that a little later but
- 1:26that's an important piece of that so
- 1:27anyway the point is i just did a simple
- 1:28binary addition very very easy
- 1:31subtraction just as you did before and
- 1:34how do you tell if a number is bigger if
- 1:35x
- 1:36some number of binary numbers bigger
- 1:37than the other one think about that
- 1:40is it the total length what if they're
- 1:42the same length let's think about how
- 1:43you test
- 1:44if one number is bigger than the other
- 1:46with this representation
- 1:50remember binary bit patterns are simply
- 1:52representations of numbers
- 1:54numbers are a theoretical concept the
- 1:56value pi
- 1:57theoretically theoretical concept okay
- 2:00the
- 2:01numerals are the representations of that
- 2:04so
- 2:04123 is a nice example is
- 2:08a number it's in my head one two three
- 2:10the way we
- 2:11write it down so 123 this kind of
- 2:14concept of 123 things
- 2:16that could be tally marks that can be
- 2:18anything that's the number the concept
- 2:19is a number
- 2:20the representation is a numeral
- 2:24digits are always digits of numerals you
- 2:26don't have digits of numbers if you ever
- 2:28say that's a digit of a number that's
- 2:29wrong
- 2:30it's a digital or numeral numerals are
- 2:32the written representation of that
- 2:33theoretical concept of a number so the
- 2:35idea of abstraction
- 2:36okay concept representation of that of
- 2:39that beautiful idea
- 2:41numbers really have an infinite number
- 2:42of digits we know that numerals
- 2:45yeah i just did it that's funny right
- 2:46see how i did that numerals really have
- 2:48an infinite number of digits
- 2:50we always normally the beginning
- 2:53always is uh zeros we'll talk about how
- 2:55if the beginning is ones what that means
- 2:57and other things except for a few of the
- 2:58right most digits so mostly the
- 3:00actions in the lower end up because it's
- 3:01infinite so it'll be lots of lots of
- 3:03zeros
- 3:04the leading we just don't normally show
- 3:05the leading digits there
- 3:08if the result cannot be stored in the
- 3:11number of bits we have to allocate that
- 3:13we call that overflow so here's an
- 3:16unsigned remember that what we've seen
- 3:18so far is just always positive numbers
- 3:20non-negative numbers i'll say so if i go
- 3:22to zero
- 3:24and i'm counting i'm gonna use five bits
- 3:26so five bits i can think
- 3:27five bits is zero through 31. so
- 3:30the smallest number is zero the biggest
- 3:33number is all ones as my 31
- 3:35if i add one more to that it's going to
- 3:37wrap it and only have five bits
- 3:39it's going to wrap around to zero we
- 3:42call this
- 3:43overflow okay you haven't heard that
- 3:45before
- 3:46if i am subtracting numbers and i'm at
- 3:49two
- 3:50here my two i'm at one never zero and i
- 3:53subtract one more
- 3:54it'll actually wrap around this way too
- 3:56it'll go back to all ones
- 3:57that is not underflow that is
- 4:01also overflow people often call it
- 4:03negative overflow
- 4:04it's not underflow we'll talk about
- 4:06underflow is in about three lectures
- 4:09so that's if it's too big okay
- 4:13think how do you represent negative
- 4:15numbers though
- 4:17so so far all we've seen is unsigned
- 4:19numbers and by the way if you've ever
- 4:21seen
- 4:21in c this is called an unsigned integer
- 4:24or
- 4:25u for unsigned int this end is going to
- 4:28be a number 8
- 4:2916 32 which tells you actually the width
- 4:31of it because
- 4:32this is ambiguous how many bits is an
- 4:35unsigned int it's ambiguous
- 4:36so we started in c99 which they've
- 4:39introduced in subsequent versions of c
- 4:41now up to c18
- 4:43we call them you int with a number there
- 4:46underscore t that's the type for that so
- 4:49you should use that
- 4:49it's called int types.h we'll get that
- 4:51when we talk about c later
- 4:53so far we know unsigned numbers starts
- 4:55at zero and goes up
- 4:57well how do we do a negative number
- 4:59there are numbers on the number line
- 5:00there are numbers over here
- 5:02uh be nice be able to represent those at
- 5:04some point um of course if you owed
- 5:06money maybe no
- 5:07don't represent those at all that'd be
- 5:08good not to represent those because i
- 5:10owe money so the way one way to think
- 5:12about it is let's just borrow some let's
- 5:14actually borrow some space from it so i
- 5:16got
- 5:17five bits let's borrow a bit
- 5:20let me tell you my story so there's a
- 5:23story i need to tell you here just to
- 5:24put a context to this
- 5:27it was a rich king kingdom and he had
- 5:30three wise people three wise thinkers
- 5:34and he said to the wise thinkers i want
- 5:35you to teach me all there is to know
- 5:37about
- 5:38economics and they went away and
- 5:40scurried away and they came back a year
- 5:41later
- 5:41you know five tomes they had written
- 5:43this is all we understand of economics
- 5:45we've searched the world
- 5:46this is the world's knowledge of
- 5:47economics king says no time no time
- 5:50make it shorter go back and make it
- 5:51shorter they came back we created this
- 5:54single book no time sends them away
- 5:57another year comes back
- 5:58we have this pulp fiction novel to
- 6:00explain economics no no time to they
- 6:02come back
- 6:02next year we created this comic book
- 6:04graphic novel
- 6:0510 pages nope no time left them we
- 6:07cringle a single sheet of paper
- 6:09executive summary no time no time comes
- 6:12back
- 6:13so they get grayer and older and the
- 6:15beards and the hair
- 6:17they come back he says sire king we have
- 6:21come up with the theory of economics
- 6:24in four words and how it became english
- 6:28says yes go ahead i have time for that
- 6:31ain't no free lunch
- 6:34what it means is you can't get something
- 6:35for nothing if you want to be able to
- 6:37now represent negative numbers
- 6:38you got to give something you got to
- 6:40lose some numbers that you used to
- 6:41represent now you won't be able to
- 6:43represent them anymore so
- 6:44if we borrow a bit now we can't go as
- 6:46high and positive
- 6:48but now we can do negatives so here's
- 6:49the idea the leftmost bit is going to be
- 6:52my sign
- 6:53okay so you look at that leftmost bit
- 6:55that's positive negative zero is p by
- 6:57the way it'd be really nice if we in all
- 6:59these
- 6:59in all these representations all zeros
- 7:02were zero
- 7:02okay so all zero is this that way
- 7:06the when the first when the first bit is
- 7:08zero nothing changes
- 7:10now the biggest number i can represent
- 7:11is 15.
- 7:13b4 was 31 before but this
- 7:17allows me to represent the negative
- 7:18space this is now
- 7:20uh negative zero negative
- 7:25zero negative
- 7:28one etc so now i'm going to show you
- 7:32this way of thinking about this
- 7:34this is a
- 7:38binary odometer back in your olden days
- 7:41of
- 7:41driving an old car that had an analog
- 7:44odometer the mileage reading
- 7:47as you go from all zeros you get brand
- 7:48new car all zeroes and you start to
- 7:50drive it it's all zeros let's say it's
- 7:51binary then all zero's in one then all
- 7:53zero's one zero then all zeros one zero
- 7:55zero et cetera
- 7:56by the way i want you to be able to
- 7:57think about counting
- 7:59in binary see this spinal odometer
- 8:03your hand can be this binary odometer
- 8:05watch this
- 8:06here we go okay all zeros
- 8:09repeat after me try this at home okay
- 8:11hand this is zeros
- 8:12this is my as you see it this is the uh
- 8:15one spot
- 8:17two spot four spot eight spot sixteenth
- 8:19spot let's count
- 8:21zero one two
- 8:24three four i have to block it see
- 8:27otherwise i'm on
- 8:28youtube five okay that's five
- 8:32six seven is harder
- 8:36eight nine okay let's see eight
- 8:40eight this is harder because nine okay
- 8:42go it
- 8:43ten eleven twelve
- 8:47twelve thirteen fourteen
- 8:51fourteen 15 cool right
- 8:5516 17 yos 17 dude
- 8:59okay et cetera et cetera et cetera 31.
- 9:02isn't that neat
- 9:02so what you just saw in this binary
- 9:03diameter you can do with your hand and i
- 9:05encourage you to memorize how to do that
- 9:06zero one two three that's pretty cool do
- 9:08that as fast as you can see
- 9:10see how we can do it and maybe you're
- 9:11more dexterous than i am i can't get the
- 9:13eights up there
- 9:14so well so
- 9:17notice that the odometer is good it
- 9:18starts at the beginning and goes up
- 9:20continuous
- 9:20nice as i as the binary goes up the
- 9:23number gets bigger
- 9:23kind of makes sense however watch what
- 9:26happens to the binary odometer with sine
- 9:28magnitude
- 9:30okay that's good oh wait now as odometer
- 9:33is going up i'm counting
- 9:34and going away from zero the wrong way
- 9:36i'm going positive positive positive and
- 9:38then i go negative
- 9:40that's hard we don't like that so much
- 9:42passive we went always went the same way
- 9:43always went bigger so
- 9:45think about that as one of the problems
- 9:47with sine magnitude
- 9:48so another problem so that was one
- 9:51problem is this
- 9:52when a weird direction the second is to
- 9:55build
- 9:55circuitry around this is actually hard
- 9:57because you got to deal with the fact
- 9:58that it's positive or negative and what
- 9:59if there one's a positive one's negative
- 10:01hard okay annoying you got two zeros
- 10:04that's annoying uh does that mean for
- 10:07programming is it equal to one of those
- 10:08you have to check for two patterns of a
- 10:09zero now that's
- 10:10that's annoying i'd mention the binary
- 10:12odometer does this weird thing
- 10:13um so actually we don't usually use sine
- 10:15magnitude except for in rare cases often
- 10:17in signal processing
- 10:19let's try it again how about if we just
- 10:21flip the bits let's just try it's
- 10:23another
- 10:23attempt these are all people sitting in
- 10:25a you know with a white board thinking
- 10:27let's try flipping all the bits when
- 10:29it's negative so here let's try it again
- 10:31so seven is two zeros and three ones
- 10:34okay what would negative seven be flip
- 10:38the bits
- 10:39now one one zero zero zero there's
- 10:42minus seven i get it so in a way it
- 10:45looks
- 10:46like it actually looks like that leading
- 10:48zero look at this got the same kind of
- 10:49nice thing is
- 10:50positive numbers have leading zeros
- 10:52here's the leading zeros
- 10:54negatives have leading ones but it's not
- 10:55psi magnitude
- 10:58what's minus zero zero zero well all
- 11:01ones so i still have this problem that
- 11:03there's two zeros we've we see that
- 11:05already um
- 11:07how many positive numbers okay how many
- 11:09ask yourself how many positive numbers
- 11:10and how many negative numbers you might
- 11:11have
- 11:12in n bits okay
- 11:15so but the problem you see how the
- 11:17problem is let's let's watch let's
- 11:19look at the odometer just to see ready
- 11:20here's the odometer you go up
- 11:22and then if you notice
- 11:26all ones is at the far side on the left
- 11:29so actually as the dominant goes up
- 11:30after zero zero zero one one one one and
- 11:32it wraps to the next number
- 11:33it's kind of like back in the day of you
- 11:35know you have all zero and all nines it
- 11:37wraps to
- 11:37one in all zeros in decimal
- 11:40we odometer is now fixed but this is
- 11:43still overlap so you're thinking well
- 11:44couldn't we do mostly this but just take
- 11:47this bottom here's what you want to do
- 11:48right
- 11:49take this bottom guy and just shift it
- 11:51left
- 11:52then you wouldn't have the overlap and
- 11:54it would all work out and in fact
- 11:56that's what we do that's the so the
- 11:58shortcoming
- 12:00arithmetic is complicated still got the
- 12:02two zeros although we got the nice
- 12:03odometer to do the right thing okay so
- 12:05in historically it was used for a while
- 12:07and then eventually abandoned because
- 12:09this next one is better
- 12:12we'll teach you what the next one is on
- 12:13the next lecture
- 12:15we'll see you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 02.3 - Number Representation: Overflow, Sign and Magnitude, One's Complement by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,232 words across 368 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 translate the transcript into 100+ languages.
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, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.