[CS61C FA20] Lecture 02.2 - Number Representation: Conversions — Transcript
Full transcript
- 0:00and welcome back now let's see
- 0:03how to work with binary decimal and
- 0:06hexadecimal numbers and how to convert
- 0:08among that triangle binary decimal to
- 0:10hexadecimal
- 0:11so let's begin at the beginning we're in
- 0:13first grade we're learning about base 10
- 0:15numbers
- 0:16and you're thinking to yourself uh i
- 0:18paid money for this let's go
- 0:20no it makes it makes it's important to
- 0:22start from the beginning to see the
- 0:23analogies of how you were thinking about
- 0:25numbers in the first place
- 0:26so if you remember the teacher taught
- 0:28you that you had ten different
- 0:29characters
- 0:30zero through nine we're in base ten by
- 0:32the way the question of why are we in
- 0:34base ten
- 0:35ten fingers uh if we were
- 0:38in simpson's land we'd be in base eight
- 0:41because they have four fingers
- 0:42see simpsons thing so if i give you the
- 0:45number 3271
- 0:48by the way from now on starting now
- 0:51in general if i don't write any base
- 0:52it's the default is going to be base 10
- 0:55okay uh in fact i i i'm even going to
- 0:58write the letter out of t e n
- 1:00there because base 1 0 is ambiguous but
- 1:02we'll talk about that in a couple slides
- 1:05so 3271 that means is the same as 3271
- 1:09subscript 10. okay i can even grab a pen
- 1:12here
- 1:13and just annotate that so i'm going to
- 1:14write a subscript here to indicate
- 1:17that we're in base 10. what that really
- 1:19meant is
- 1:21three thousands plus two hundreds
- 1:25plus seven tens plus one
- 1:28one okay and we add them all up this is
- 1:30like a coefficient times
- 1:32times this term so it's three thousands
- 1:34two hundreds
- 1:35seven tens and one that gives you 3271
- 1:38there by the way i could have said
- 1:40look i've got three i got a lot of dash
- 1:42marks
- 1:43i've got a lot of when you're in prison
- 1:45not that i would know about
- 1:46being in prison but if you're in prison
- 1:48you might have lots of dashboards how
- 1:49many days you've been there every day
- 1:50you
- 1:51scratch something into the wall well i
- 1:52might have that number of dash marks
- 1:54just and that's actually base one you've
- 1:56just got dash marks it's called
- 1:58uh tally counting i think so if i want
- 2:00to convert that to base ten i'd have to
- 2:02count them up and then figure out how
- 2:03many
- 2:03ten how many thousands and how many tens
- 2:05it's actually an interesting process of
- 2:07counting from tally
- 2:08marks to base 10.
- 2:11base 2 is exactly the same thing as base
- 2:1310 just subtle
- 2:15where i said you had 10 characters
- 2:16represented 10 digits
- 2:18now i have 2 0 and 1.
- 2:22and in fact we're always going to start
- 2:23from 0 and count the way up when we get
- 2:25past 10 we'll see how to do this in
- 2:26hexadecimal by actually leaking into the
- 2:27letters
- 2:29so if i have 1 1 0 1 now i now need to
- 2:32indicate that 1 1 0 1 is not in base 10.
- 2:35it's in base 2.
- 2:36so i would either write 0 b in front of
- 2:38it okay if i see 0 b
- 2:40that means i'm in a binary number or i
- 2:42can write it base
- 2:442 subscript 2 or i can say 1 1 0 1
- 2:47in binary okay and i'm saying translate
- 2:491101 and bind it
- 2:511 101 from base 10 to binary that's what
- 2:53i'm really asking you to do
- 2:55well how do we do this the same way what
- 2:58we do
- 2:59is if you recall before i'm going to go
- 3:01back to this slide this 10 to the 3 this
- 3:03is the base
- 3:04to some power look at that 10 to the 0
- 3:0710 to the 1
- 3:0810 to the 2 10 to the 3. the same thing
- 3:10here let's take the base
- 3:12to the different powers and this in a
- 3:14way gives
- 3:15like a column heading you can sometimes
- 3:17i even do this sometimes i say
- 3:19draw it like this and i say 1
- 3:222 what's 2 squared 4.
- 3:26what's 2 to the 3 8 and then i say well
- 3:30what's the value in that and so i kind
- 3:32of say i write my number one
- 3:34one zero one and basically that's
- 3:37it's the the column heading times the
- 3:39value below it so you see this here it's
- 3:41one
- 3:41eight one four no twos and one one
- 3:46it's almost like have an analogy we're
- 3:47going to see in a second if i got boxes
- 3:49from amazon
- 3:50and the boxers labeled one egg in this
- 3:52one
- 3:53here's an empty box i guess it's an
- 3:54empty box this is the two box
- 3:56this is full of eggs it's four and this
- 3:58is full of eggs it's eight
- 4:00how many total eggs do i have so i think
- 4:01about that sometimes the boxes come
- 4:02empty or full but they're either only
- 4:04empty or full in here
- 4:05in this particular case so eight and
- 4:07four and one is thirteen therefore one
- 4:08one zero one
- 4:09is 13. pretty cool
- 4:13base 16. you got to be warmed up let's
- 4:16go let's rock and roll
- 4:17this is called hexadecimal so now you're
- 4:19going to say well dan i have
- 4:2016 digits but i can only i can probably
- 4:24use the first 10 from decimal that's
- 4:25right so what do i do
- 4:27you're going to leak into the letters so
- 4:30we're going to go a through f
- 4:31so all of a sudden a is now a number b
- 4:34is a number
- 4:35and the way that works is a is 10. just
- 4:37keep locked at it
- 4:38b is 11. c is 12 d is 13 e is 14
- 4:41f is 15. so i would like you to memorize
- 4:44that actually i really would like you to
- 4:46memorize that i'd like to be proud to
- 4:47say all of my 621c graduates
- 4:49know that and know how to connect that
- 4:50so i'm going to ask you here we go a5 i
- 4:52made a5
- 4:53in hexadecimal written in hexadecimal
- 4:57dollars today how much money did i make
- 4:59that's pretty cool now
- 5:00you could also have a number 1101 is a
- 5:03valid hexadecimal number so again we
- 5:04still have to indicate what our base is
- 5:06when i'm giving you a number
- 5:07so i can either say a5 a hexadecimal
- 5:10number a5 what's that in decimal that's
- 5:12what i'm asking doing here
- 5:13or i can precede it with 0x just like 0b
- 5:170x that tells you unambiguously that
- 5:21that's a hexadecimal number
- 5:23or again you can write a subscript 16.
- 5:26we do our same thing we write our
- 5:27columns here's our columns
- 5:29the first column is always one the
- 5:31second column is always b this is this
- 5:33is by the way
- 5:33this is always base to the zero base to
- 5:36the one so
- 5:37base to the one is 16 base to the 2 is
- 5:39256
- 5:41etc so what have i got well
- 5:44a5 i write a5 what are right over here
- 5:48zero there are always leading zeros we
- 5:51just ignore them right there's all
- 5:52if i said i made 12 dollars in in
- 5:54decimal 12 today
- 5:55i also made 0 12. i also made 0 0 12. we
- 5:57just don't say the zeros
- 5:59okay so a5 let's do it so a times 16.
- 6:03wait what well i have to look up so a is
- 6:0510
- 6:0610 times 16 is 160 plus 1 times
- 6:095 times 1 is 5. 160 plus 5 is 165. so if
- 6:13i made a5
- 6:14a hexadecimal hex a5 dollars i made 165
- 6:18in decimal dollars pretty cool right
- 6:22every base is base 10. here's an alien
- 6:26and here's an astronaut bail
- 6:27alien says there are 10 rocks astronaut
- 6:30says
- 6:31oh yeah you must be using base four uh
- 6:34uh see i use base ten and alien says no
- 6:37no i use base ten what's base four
- 6:40because in every base there's a ten
- 6:43and in fact every base ten is their base
- 6:48base two remember binary one zero is two
- 6:51base decimal one zero is ten hexadecimal
- 6:54one
- 6:54zero is sixteen so every base has a ten
- 6:58and every base is that base all your
- 7:01base
- 7:02are belong to us so now let's convert
- 7:04we've now converted from
- 7:06decimal converted from binary and hex to
- 7:09decimal
- 7:10let's now convert from decimal to binary
- 7:13and to hex
- 7:15so let's let's reverse it don't tell
- 7:17them this is the same number we
- 7:18converted before okay
- 7:1913. so this is an hour this is actually
- 7:22the first algorithm that's i mean that
- 7:23was an algorithm too but it was a little
- 7:24easy
- 7:24this is an algorithm that has some ifs
- 7:26then we have to make a decision here
- 7:28the way i think of this my analogy for
- 7:30this this has worked well for
- 7:31students when i've taught them to think
- 7:34of the following
- 7:34i've got these boxes to ship back to
- 7:36amazon all labeled
- 7:38in those bases i've got a box labeled
- 7:40one box labeled two a box labeled four
- 7:42box labeled eight okay
- 7:45i want to reduce my postage amazon will
- 7:48only take
- 7:49a full box back remember it's either
- 7:50full or empty i won't take a full box
- 7:52back
- 7:53so i first start by this i say i got 13
- 7:57and the first the algorithm says this is
- 7:59the column number by the way you know to
- 8:01the left of this is a 16
- 8:03this i should write this better there's
- 8:04a 16 there's a 32
- 8:07etc on the left of that so i can start
- 8:09there let's start with the 16. this just
- 8:10just to be just to go rogue for a second
- 8:13i say
- 8:14is the column number less than or equal
- 8:16the number i have left the number of
- 8:18eggs i have left to you know
- 8:19boy those chickens were productive so i
- 8:22got to ship 13
- 8:23eggs back to amazon i got a box said 16
- 8:25and i know amazon will
- 8:26take a full box so can i fit 13 in a
- 8:30box of 16 and fill it up i can't so i
- 8:33write a zero here
- 8:34boop pretty cool go to the next guy
- 8:37so let's try this then i go to the eight
- 8:39and i've actually animated this for you
- 8:41so i say can i fit and fill an
- 8:44eight box with whatever eggs i've got
- 8:46left which is thirteen i say yes
- 8:49then i ask how many of those
- 8:52boxes can i fill well here only one in
- 8:55fact in binary it's only gonna
- 8:56be one or zero but so we'll see in hex
- 8:57it's actually more we'll have to say in
- 8:59general be more than just one
- 9:01so once i filled eight how many do i
- 9:03have left cross it off i say i've got
- 9:05five left
- 9:06let's keep going i go to my next box can
- 9:09i fill a four
- 9:10yes i can how many times can i fill the
- 9:13four only once
- 9:14so therefore i fill it up i take i fill
- 9:17the four how many is left after i fill
- 9:18the four box
- 9:19one left i've got the next box we've got
- 9:22a two can i fill the two with only one
- 9:24egg
- 9:24i cannot then i can't use it so a zero
- 9:27in that one
- 9:28go to the next one can i fill a box of
- 9:30one that box labeled one with one
- 9:32egg yes i can how many times can i fill
- 9:34it only once there's one and i've seen
- 9:36now
- 9:37and i just by the way i stopped when my
- 9:38my sum is zero so once i cross it off
- 9:40and i get my zero i stop and i just put
- 9:42zeros in the rest of these guys if i
- 9:43have if i happen to let's say i had
- 9:44eight i'd put a one for eight and put
- 9:46all the zeros i could just
- 9:47you know short circuit it and go up
- 9:48right right to the end
- 9:50so converting 13 to binary
- 9:531101 is the equivalent by going down
- 9:56thinking of the amazon egg boxes you got
- 9:59it
- 9:59now let's do this in hexadecimal
- 10:04165 again don't tell the students but
- 10:06this is the same number we had before
- 10:08let's go 165 which is a
- 10:11decimal number i want to convert that to
- 10:14hexadecimal
- 10:15so start with my columns in fact i've
- 10:17added some extra ones here now
- 10:194096. so i asked the question
- 10:22can i fill the 4096 box probably 16 to
- 10:24the three is 4096
- 10:25another nice thing to memorize but i
- 10:27wouldn't worry about that one too much
- 10:30i cannot zero let's go to the next guy
- 10:33can i fill the 256
- 10:35labeled box with 165 i cannot zero
- 10:39can i fill a 16 box yes i can
- 10:43how many see this is the first time it's
- 10:45come up how many
- 10:4716s how many 16 boxes can i fill with my
- 10:50165 eggs sitting all
- 10:52on my floor 10 of them do i write
- 10:5510 no i look up in that base
- 10:58what the character the digit that
- 11:01represents that is
- 11:02and it's going to be an a i would list
- 11:03an a not a 10 i'd list an
- 11:05a so far once i have a or
- 11:0810 of these 16 boxes that's 160
- 11:13how many is left five and i keep going
- 11:16same algorithm
- 11:17this is the same algorithm for every
- 11:18base isn't that pretty cool so now you
- 11:20know
- 11:20i could actually ask you an exam to
- 11:22convert to some base you haven't heard
- 11:23of before
- 11:24and you should be able to do that same
- 11:25algorithm okay so
- 11:27five left i've got boxes with one how
- 11:29many can i fill
- 11:31uh you can can i fill a box with one yes
- 11:32i can how many of those boxes can i fill
- 11:34five i fill the five i'm down to zero
- 11:37and i'm done
- 11:38it's pretty cool this is fun right so
- 11:41now
- 11:42of the triangle here's a decimal binary
- 11:45hex
- 11:46i can now go back and forth that's
- 11:47pretty cool i can't go this way yet
- 11:49until this slide
- 11:51boom binder to hex so i've got some
- 11:54binary number
- 11:55how do i convert it to to hex here's the
- 11:57key don't make this mistake
- 12:00you have to left pad it which means add
- 12:03zeros to maybe you always add zeros to
- 12:05the left
- 12:05you have to left pad it so that it's all
- 12:07the length of that binary digit
- 12:10always is a multiple of four if you do
- 12:13this
- 12:13it'll work out trivially so if i give
- 12:15you one one one one
- 12:17four ones and a zero convert it to hex
- 12:21what have i got one one one one
- 12:24one one one one zero to hex well it's
- 12:27only five
- 12:28it's five let's add three more now i've
- 12:30got
- 12:32eight characters three zeros four ones
- 12:35and a zero you see it right here okay
- 12:37now this guy is eight
- 12:41binary digits or bits by the way i
- 12:43didn't mention binary digits
- 12:45bi and the ts from digits becomes bits
- 12:50that's why we call them bits so look
- 12:52that up now i have this
- 12:53look that up here's the table this table
- 12:56right here boom
- 12:58boom memorize this table
- 13:01it will save you in time and will make
- 13:03you feel like
- 13:04oh stand a little taller 61c i know i
- 13:07now know i'm fluent with hexadecimal
- 13:09you can ask me boom what's uh i don't
- 13:12know
- 13:12let's look at it by the way the way you
- 13:14do this then you look it up by
- 13:15fours one of the things you're going to
- 13:17notice is
- 13:19four binary digits remember this from
- 13:21before the first video
- 13:22is two to the four sixteen things
- 13:26well sixteen things is this number of
- 13:28hexadecimal
- 13:30options zero through f one accidental
- 13:32digit is
- 13:3316 things so there's my four there's my
- 13:3616 binary bits i just looked them up i
- 13:39say
- 13:40what's that i just look it up boom
- 13:43e so i write e
- 13:46what's 0 i look it up 1
- 13:50answer is 1e easy you could also
- 13:53not do the padding but start from the
- 13:54right okay if you start from the right
- 13:56it doesn't matter so i
- 13:57go here there's that 1 1 1 0. that's my
- 14:00e
- 14:01this is my one with some leading zeros
- 14:03okay here's here's an important thing
- 14:05if you go from the left you will get it
- 14:07wrong
- 14:09one one one one that would be f
- 14:12zero and i can't pad to the right
- 14:14because that changes the number
- 14:16so it is not f zero it is not f zero
- 14:19very important that you see that okay so
- 14:21start from the right or
- 14:22pad it so that's always multiple to four
- 14:24and just that doesn't matter which way
- 14:25you go
- 14:27hex to binary piece of cake let's look
- 14:29them up
- 14:30literally look them up concatenate it
- 14:32all together and then drop the leading
- 14:33zeros
- 14:34so one e so what's your e
- 14:37there's your e we did this already
- 14:40what's your one
- 14:41drop leading zeros cross those off what
- 14:43do you get
- 14:44one one one one zero okay binary hex
- 14:47that's the triangle
- 14:48huh with the thing they made the
- 14:50triangle okay
- 14:53so four bits
- 14:57okay let's start for the eight i start
- 14:58with the second bullet point here eight
- 14:59bits is called a byte
- 15:00just historically it's called a byte
- 15:03okay
- 15:04now four bits half of that as you see
- 15:06here this is this is my
- 15:08four bit column that's called a nibble
- 15:11get it a bite
- 15:12see a bite and then a a nibble
- 15:16you also you also have i've i've tried
- 15:19to advocate that two bits should be like
- 15:20a
- 15:21taste you know i don't know nobody's
- 15:23ever bought under there
- 15:24one hex digit is 16 different things
- 15:26we've seen that before
- 15:28two hex digits okay here we go two hex
- 15:30digits how many things can i do
- 15:32if you remember before zero to zero zero
- 15:35to f
- 15:36that's that's that's two hex digits well
- 15:38what's ff let's do the math
- 15:40oh it's 255. starting from zero that
- 15:43means 256 things
- 15:44okay so or n hex digits is
- 15:4816 to the n there's another way to think
- 15:49about that now
- 15:51turns out back in the days of thinking
- 15:54of making displays
- 15:55making computer displays okay they
- 15:57realized that
- 15:59we need just about eight digits eight
- 16:01digits eight bits
- 16:02to represent a single color partly
- 16:04because the human eye can't see more
- 16:05than if i had
- 16:06not by the way they now have high-end
- 16:08tvs that have more of those bits which
- 16:10is interesting because they can do
- 16:10dynamic range there's a long longer
- 16:12conversation there
- 16:13but the point of this is early tvs used
- 16:16eight bits for each of the red
- 16:18green and blue channels they concatenate
- 16:20them together to make a 24-bit
- 16:22color you need to know this for your
- 16:24first project
- 16:25let's see how that is illusion yeah
- 16:30so zero to 255 red zero 255
- 16:34green 55 blue you concatenate them
- 16:37together
- 16:38what you get is six hexadecimal digits
- 16:42and that's a color and if you've ever
- 16:44seen web programming or seen some color
- 16:47on a mac
- 16:47by the way i could go rogue and show you
- 16:49this this is probably we'll show you
- 16:50this in a second
- 16:51this is a color represented as six
- 16:55hexadecimal digits i'm gonna go rogue
- 16:57because i love doing this demo
- 16:59here we go i love this demo and the show
- 17:02keep my imitation discovery watch this
- 17:04so here we go ready apple
- 17:06system preferences desktop and screen
- 17:09saver
- 17:11desktop
- 17:15i can choose a color i can customize the
- 17:18color there's a long long way to get
- 17:20there
- 17:20i could have done this i could have done
- 17:21this by the way in powerpoint
- 17:25look at this this is rgb sliders
- 17:30that and here is the hex value so all
- 17:33white
- 17:34is all f's if i
- 17:37take these values down look at this
- 17:39watch how everything changes color
- 17:41as i move this color around watch this
- 17:45ff000 is all red
- 17:48huh with the thing if i add this color
- 17:51in
- 17:53ffff00 it's yellow
- 17:56is that cool so i can now adjust this
- 17:58and play with this and i encourage you
- 17:59to
- 18:00find one of these things one of these
- 18:01tools on the web to this
- 18:03to play with the colors to see actually
- 18:04colors here's d0367f
- 18:08uh as a color here on the bottom pretty
- 18:12cool
- 18:14which base do we use now you've got me
- 18:15six guys which one do i use well decimal
- 18:17is obviously great for humans especially
- 18:19when doing arithmetic we know how to do
- 18:20that very easily
- 18:21hexadecimal terrible for arithmetic just
- 18:24terrible
- 18:25but if you're looking at a string of
- 18:26binary digits i could either give you
- 18:28a crazy long number by digits or i could
- 18:30compact them down to a hexadecimal
- 18:32equivalent by the way would you prefer
- 18:33to give you
- 18:34that six hexadecimal digit color or 24
- 18:38bits you don't want the 24 bits right
- 18:40so i don't want that in binary
- 18:43what computers use you're going to be
- 18:45able to do multiplication division
- 18:47subtraction all the operations you think
- 18:49of in all the numbers computers always
- 18:51use binary because that's what they can
- 18:52do
- 18:52we'll talk about that a little later in
- 18:54the class transistors only know two
- 18:56states on or off
- 18:57current flowing or not flowing so
- 18:58they're going to use binary because of
- 18:59that
- 19:01regardless of how a number is written 32
- 19:04base 10
- 19:04or 32 base 1 0
- 19:07or 0x 20 or 1 0
- 19:110 0 000 base 2 or 0b1000
- 19:16they represent the same number those are
- 19:18all numerals
- 19:19representing the same number we'll talk
- 19:20about numerals in the next next video
- 19:23please do use the subscripts or the zero
- 19:25x or the zero b to precede your number
- 19:27so it's not ambiguous
- 19:29the computer knows it too this is really
- 19:32cool actually
- 19:33here's a little piece of c code we're
- 19:34gonna see in the next lecture but check
- 19:36this out i say 1 2 3 4 is my
- 19:38number right base 10 okay ten
- 19:42and i say let's just print this out so i
- 19:44print this out in percent d
- 19:45for you can see this here in percent d
- 19:48for
- 19:49decimal percent d
- 19:52percent x and percent o for octal
- 19:55base eight what does it print
- 19:58124 yeah that's good what else is a
- 20:02print
- 20:034d2 this is a problem
- 20:06it doesn't proceed with 0x or have a
- 20:08subscript 16.
- 20:10this is bad this means if it were like
- 20:12let's say let's say this number was 16
- 20:14it represents one zero and you wouldn't
- 20:15know what base it is
- 20:16so you better add the zero x yourself
- 20:18when you're printing it to percent x
- 20:20you got that good percent oh well how
- 20:22does it do percent oh it turns out that
- 20:24the way we do the great computers do
- 20:25octal is you
- 20:26lead at a leading zero it's a little
- 20:28ambiguous because you can always have a
- 20:29leading zero
- 20:30but by having a leading zero it tells
- 20:32the computer it's going to be octal
- 20:33we'll see that as we put these numbers
- 20:35in
- 20:36i can also go the other way watch this i
- 20:39can say
- 20:40pass the hexadecimal number in look at
- 20:43this i can
- 20:43hard code a hexadecimal number in by
- 20:46saying 0x
- 20:47if i say percent d that says convert it
- 20:49to decimal what does it give me
- 20:50one two three four is that cool same
- 20:53thing
- 20:54zero b some huge thing percent d
- 20:57converts it binary
- 20:58to decimal and here's my octal number
- 21:02preceded by a zero and here's percent d
- 21:04and we get one two three four for all of
- 21:06them
- 21:06isn't that cool so i just took these
- 21:08numbers here and put them there
- 21:10all right we're done with the series of
- 21:12we're done with this lecture
- 21:14the next lecture we're going to talk
- 21:15about how to represent
- 21:17negative numbers positive numbers in a
- 21:19more general way number representation
- 21:21we'll see you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 02.2 - Number Representation: Conversions by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 4,028 words across 630 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.