YouTube2Text

[CS61C FA20] Lecture 02.3 - Number Representation: Overflow, Sign and Magnitude, One's Complement — Transcript

by CS 61C Departmental · 2,232 words · 368 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's talk about
  2. 0:03number representations
  3. 0:06what can we do with representations of
  4. 0:07numbers well we can
  5. 0:09do everything you can do you understand
  6. 0:11in decimal you can add them subtract the
  7. 0:13multiply divide compare them
  8. 0:15for example and by the way this is
  9. 0:16representation in binary
  10. 0:18how could we represent
  11. 0:22how could we do some addition here's 10
  12. 0:251 0 1
  13. 0:250 is 10. so 10 10 is 10 remember that
  14. 0:28memorize that guy
  15. 0:29and let's add that together to seven
  16. 0:30seven is one one one well let's do it
  17. 0:33together let's actually do it as we did
  18. 0:35you're back in first grade zero plus one
  19. 0:38one by the way the maximum value in any
  20. 0:41of those slots is a one in binary by the
  21. 0:43way this is these are all base two
  22. 0:45okay this is this is the base two and
  23. 0:46base two zero plus one one one plus one
  24. 0:49ready it's two which means zero
  25. 0:53carry the one because it's kind of the
  26. 0:54one bubbled over two
  27. 0:56and binary is 0. 1 plus 0 plus 1 is 2
  28. 0:590 plus 1 carries 1 plus 1 there again 2
  29. 1:030 and the 1 carries to here and you get
  30. 1:061 0 0 1. so
  31. 1:0910 plus 7 is 17. and this ladies and
  32. 1:13gentlemen is 17
  33. 1:14because that's 16 plus a 1.
  34. 1:18now if i only had
  35. 1:21four bits to do this i wouldn't have had
  36. 1:23enough room to store that number we're
  37. 1:24going to see that a little later but
  38. 1:26that's an important piece of that so
  39. 1:27anyway the point is i just did a simple
  40. 1:28binary addition very very easy
  41. 1:31subtraction just as you did before and
  42. 1:34how do you tell if a number is bigger if
  43. 1:35x
  44. 1:36some number of binary numbers bigger
  45. 1:37than the other one think about that
  46. 1:40is it the total length what if they're
  47. 1:42the same length let's think about how
  48. 1:43you test
  49. 1:44if one number is bigger than the other
  50. 1:46with this representation
  51. 1:50remember binary bit patterns are simply
  52. 1:52representations of numbers
  53. 1:54numbers are a theoretical concept the
  54. 1:56value pi
  55. 1:57theoretically theoretical concept okay
  56. 2:00the
  57. 2:01numerals are the representations of that
  58. 2:04so
  59. 2:04123 is a nice example is
  60. 2:08a number it's in my head one two three
  61. 2:10the way we
  62. 2:11write it down so 123 this kind of
  63. 2:14concept of 123 things
  64. 2:16that could be tally marks that can be
  65. 2:18anything that's the number the concept
  66. 2:19is a number
  67. 2:20the representation is a numeral
  68. 2:24digits are always digits of numerals you
  69. 2:26don't have digits of numbers if you ever
  70. 2:28say that's a digit of a number that's
  71. 2:29wrong
  72. 2:30it's a digital or numeral numerals are
  73. 2:32the written representation of that
  74. 2:33theoretical concept of a number so the
  75. 2:35idea of abstraction
  76. 2:36okay concept representation of that of
  77. 2:39that beautiful idea
  78. 2:41numbers really have an infinite number
  79. 2:42of digits we know that numerals
  80. 2:45yeah i just did it that's funny right
  81. 2:46see how i did that numerals really have
  82. 2:48an infinite number of digits
  83. 2:50we always normally the beginning
  84. 2:53always is uh zeros we'll talk about how
  85. 2:55if the beginning is ones what that means
  86. 2:57and other things except for a few of the
  87. 2:58right most digits so mostly the
  88. 3:00actions in the lower end up because it's
  89. 3:01infinite so it'll be lots of lots of
  90. 3:03zeros
  91. 3:04the leading we just don't normally show
  92. 3:05the leading digits there
  93. 3:08if the result cannot be stored in the
  94. 3:11number of bits we have to allocate that
  95. 3:13we call that overflow so here's an
  96. 3:16unsigned remember that what we've seen
  97. 3:18so far is just always positive numbers
  98. 3:20non-negative numbers i'll say so if i go
  99. 3:22to zero
  100. 3:24and i'm counting i'm gonna use five bits
  101. 3:26so five bits i can think
  102. 3:27five bits is zero through 31. so
  103. 3:30the smallest number is zero the biggest
  104. 3:33number is all ones as my 31
  105. 3:35if i add one more to that it's going to
  106. 3:37wrap it and only have five bits
  107. 3:39it's going to wrap around to zero we
  108. 3:42call this
  109. 3:43overflow okay you haven't heard that
  110. 3:45before
  111. 3:46if i am subtracting numbers and i'm at
  112. 3:49two
  113. 3:50here my two i'm at one never zero and i
  114. 3:53subtract one more
  115. 3:54it'll actually wrap around this way too
  116. 3:56it'll go back to all ones
  117. 3:57that is not underflow that is
  118. 4:01also overflow people often call it
  119. 4:03negative overflow
  120. 4:04it's not underflow we'll talk about
  121. 4:06underflow is in about three lectures
  122. 4:09so that's if it's too big okay
  123. 4:13think how do you represent negative
  124. 4:15numbers though
  125. 4:17so so far all we've seen is unsigned
  126. 4:19numbers and by the way if you've ever
  127. 4:21seen
  128. 4:21in c this is called an unsigned integer
  129. 4:24or
  130. 4:25u for unsigned int this end is going to
  131. 4:28be a number 8
  132. 4:2916 32 which tells you actually the width
  133. 4:31of it because
  134. 4:32this is ambiguous how many bits is an
  135. 4:35unsigned int it's ambiguous
  136. 4:36so we started in c99 which they've
  137. 4:39introduced in subsequent versions of c
  138. 4:41now up to c18
  139. 4:43we call them you int with a number there
  140. 4:46underscore t that's the type for that so
  141. 4:49you should use that
  142. 4:49it's called int types.h we'll get that
  143. 4:51when we talk about c later
  144. 4:53so far we know unsigned numbers starts
  145. 4:55at zero and goes up
  146. 4:57well how do we do a negative number
  147. 4:59there are numbers on the number line
  148. 5:00there are numbers over here
  149. 5:02uh be nice be able to represent those at
  150. 5:04some point um of course if you owed
  151. 5:06money maybe no
  152. 5:07don't represent those at all that'd be
  153. 5:08good not to represent those because i
  154. 5:10owe money so the way one way to think
  155. 5:12about it is let's just borrow some let's
  156. 5:14actually borrow some space from it so i
  157. 5:16got
  158. 5:17five bits let's borrow a bit
  159. 5:20let me tell you my story so there's a
  160. 5:23story i need to tell you here just to
  161. 5:24put a context to this
  162. 5:27it was a rich king kingdom and he had
  163. 5:30three wise people three wise thinkers
  164. 5:34and he said to the wise thinkers i want
  165. 5:35you to teach me all there is to know
  166. 5:37about
  167. 5:38economics and they went away and
  168. 5:40scurried away and they came back a year
  169. 5:41later
  170. 5:41you know five tomes they had written
  171. 5:43this is all we understand of economics
  172. 5:45we've searched the world
  173. 5:46this is the world's knowledge of
  174. 5:47economics king says no time no time
  175. 5:50make it shorter go back and make it
  176. 5:51shorter they came back we created this
  177. 5:54single book no time sends them away
  178. 5:57another year comes back
  179. 5:58we have this pulp fiction novel to
  180. 6:00explain economics no no time to they
  181. 6:02come back
  182. 6:02next year we created this comic book
  183. 6:04graphic novel
  184. 6:0510 pages nope no time left them we
  185. 6:07cringle a single sheet of paper
  186. 6:09executive summary no time no time comes
  187. 6:12back
  188. 6:13so they get grayer and older and the
  189. 6:15beards and the hair
  190. 6:17they come back he says sire king we have
  191. 6:21come up with the theory of economics
  192. 6:24in four words and how it became english
  193. 6:28says yes go ahead i have time for that
  194. 6:31ain't no free lunch
  195. 6:34what it means is you can't get something
  196. 6:35for nothing if you want to be able to
  197. 6:37now represent negative numbers
  198. 6:38you got to give something you got to
  199. 6:40lose some numbers that you used to
  200. 6:41represent now you won't be able to
  201. 6:43represent them anymore so
  202. 6:44if we borrow a bit now we can't go as
  203. 6:46high and positive
  204. 6:48but now we can do negatives so here's
  205. 6:49the idea the leftmost bit is going to be
  206. 6:52my sign
  207. 6:53okay so you look at that leftmost bit
  208. 6:55that's positive negative zero is p by
  209. 6:57the way it'd be really nice if we in all
  210. 6:59these
  211. 6:59in all these representations all zeros
  212. 7:02were zero
  213. 7:02okay so all zero is this that way
  214. 7:06the when the first when the first bit is
  215. 7:08zero nothing changes
  216. 7:10now the biggest number i can represent
  217. 7:11is 15.
  218. 7:13b4 was 31 before but this
  219. 7:17allows me to represent the negative
  220. 7:18space this is now
  221. 7:20uh negative zero negative
  222. 7:25zero negative
  223. 7:28one etc so now i'm going to show you
  224. 7:32this way of thinking about this
  225. 7:34this is a
  226. 7:38binary odometer back in your olden days
  227. 7:41of
  228. 7:41driving an old car that had an analog
  229. 7:44odometer the mileage reading
  230. 7:47as you go from all zeros you get brand
  231. 7:48new car all zeroes and you start to
  232. 7:50drive it it's all zeros let's say it's
  233. 7:51binary then all zero's in one then all
  234. 7:53zero's one zero then all zeros one zero
  235. 7:55zero et cetera
  236. 7:56by the way i want you to be able to
  237. 7:57think about counting
  238. 7:59in binary see this spinal odometer
  239. 8:03your hand can be this binary odometer
  240. 8:05watch this
  241. 8:06here we go okay all zeros
  242. 8:09repeat after me try this at home okay
  243. 8:11hand this is zeros
  244. 8:12this is my as you see it this is the uh
  245. 8:15one spot
  246. 8:17two spot four spot eight spot sixteenth
  247. 8:19spot let's count
  248. 8:21zero one two
  249. 8:24three four i have to block it see
  250. 8:27otherwise i'm on
  251. 8:28youtube five okay that's five
  252. 8:32six seven is harder
  253. 8:36eight nine okay let's see eight
  254. 8:40eight this is harder because nine okay
  255. 8:42go it
  256. 8:43ten eleven twelve
  257. 8:47twelve thirteen fourteen
  258. 8:51fourteen 15 cool right
  259. 8:5516 17 yos 17 dude
  260. 8:59okay et cetera et cetera et cetera 31.
  261. 9:02isn't that neat
  262. 9:02so what you just saw in this binary
  263. 9:03diameter you can do with your hand and i
  264. 9:05encourage you to memorize how to do that
  265. 9:06zero one two three that's pretty cool do
  266. 9:08that as fast as you can see
  267. 9:10see how we can do it and maybe you're
  268. 9:11more dexterous than i am i can't get the
  269. 9:13eights up there
  270. 9:14so well so
  271. 9:17notice that the odometer is good it
  272. 9:18starts at the beginning and goes up
  273. 9:20continuous
  274. 9:20nice as i as the binary goes up the
  275. 9:23number gets bigger
  276. 9:23kind of makes sense however watch what
  277. 9:26happens to the binary odometer with sine
  278. 9:28magnitude
  279. 9:30okay that's good oh wait now as odometer
  280. 9:33is going up i'm counting
  281. 9:34and going away from zero the wrong way
  282. 9:36i'm going positive positive positive and
  283. 9:38then i go negative
  284. 9:40that's hard we don't like that so much
  285. 9:42passive we went always went the same way
  286. 9:43always went bigger so
  287. 9:45think about that as one of the problems
  288. 9:47with sine magnitude
  289. 9:48so another problem so that was one
  290. 9:51problem is this
  291. 9:52when a weird direction the second is to
  292. 9:55build
  293. 9:55circuitry around this is actually hard
  294. 9:57because you got to deal with the fact
  295. 9:58that it's positive or negative and what
  296. 9:59if there one's a positive one's negative
  297. 10:01hard okay annoying you got two zeros
  298. 10:04that's annoying uh does that mean for
  299. 10:07programming is it equal to one of those
  300. 10:08you have to check for two patterns of a
  301. 10:09zero now that's
  302. 10:10that's annoying i'd mention the binary
  303. 10:12odometer does this weird thing
  304. 10:13um so actually we don't usually use sine
  305. 10:15magnitude except for in rare cases often
  306. 10:17in signal processing
  307. 10:19let's try it again how about if we just
  308. 10:21flip the bits let's just try it's
  309. 10:23another
  310. 10:23attempt these are all people sitting in
  311. 10:25a you know with a white board thinking
  312. 10:27let's try flipping all the bits when
  313. 10:29it's negative so here let's try it again
  314. 10:31so seven is two zeros and three ones
  315. 10:34okay what would negative seven be flip
  316. 10:38the bits
  317. 10:39now one one zero zero zero there's
  318. 10:42minus seven i get it so in a way it
  319. 10:45looks
  320. 10:46like it actually looks like that leading
  321. 10:48zero look at this got the same kind of
  322. 10:49nice thing is
  323. 10:50positive numbers have leading zeros
  324. 10:52here's the leading zeros
  325. 10:54negatives have leading ones but it's not
  326. 10:55psi magnitude
  327. 10:58what's minus zero zero zero well all
  328. 11:01ones so i still have this problem that
  329. 11:03there's two zeros we've we see that
  330. 11:05already um
  331. 11:07how many positive numbers okay how many
  332. 11:09ask yourself how many positive numbers
  333. 11:10and how many negative numbers you might
  334. 11:11have
  335. 11:12in n bits okay
  336. 11:15so but the problem you see how the
  337. 11:17problem is let's let's watch let's
  338. 11:19look at the odometer just to see ready
  339. 11:20here's the odometer you go up
  340. 11:22and then if you notice
  341. 11:26all ones is at the far side on the left
  342. 11:29so actually as the dominant goes up
  343. 11:30after zero zero zero one one one one and
  344. 11:32it wraps to the next number
  345. 11:33it's kind of like back in the day of you
  346. 11:35know you have all zero and all nines it
  347. 11:37wraps to
  348. 11:37one in all zeros in decimal
  349. 11:40we odometer is now fixed but this is
  350. 11:43still overlap so you're thinking well
  351. 11:44couldn't we do mostly this but just take
  352. 11:47this bottom here's what you want to do
  353. 11:48right
  354. 11:49take this bottom guy and just shift it
  355. 11:51left
  356. 11:52then you wouldn't have the overlap and
  357. 11:54it would all work out and in fact
  358. 11:56that's what we do that's the so the
  359. 11:58shortcoming
  360. 12:00arithmetic is complicated still got the
  361. 12:02two zeros although we got the nice
  362. 12:03odometer to do the right thing okay so
  363. 12:05in historically it was used for a while
  364. 12:07and then eventually abandoned because
  365. 12:09this next one is better
  366. 12:12we'll teach you what the next one is on
  367. 12:13the next lecture
  368. 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.