YouTube2Text

[CS61C FA20] Lecture 02.4 - Number Representation: Two's Complement, Bias, and Summary — Transcript

by CS 61C Departmental · 2,525 words · 411 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's see if we can
  2. 0:03figure out
  3. 0:04what we can do to fix one's compliment
  4. 0:07is there anything we can do
  5. 0:08and guess what it's called two's
  6. 0:10complement we'll also talk about
  7. 0:11something called biased encoding
  8. 0:14so here's the idea the problem is the
  9. 0:16negative numbers overlap with the
  10. 0:17positive numbers we saw that we don't
  11. 0:19want that
  12. 0:19we want to shift the bottom over one so
  13. 0:22here's what we do just like before
  14. 0:26all the zeros are on the top so like in
  15. 0:28the five bits before
  16. 0:29all zeros up to zero one one one one
  17. 0:32that's all on the top that's zero
  18. 0:34through fifteen that's the same so in
  19. 0:35terms of the top never really changes
  20. 0:36doesn't change actually zero to all ones
  21. 0:39doesn't change in
  22. 0:40unsigned sine magnitude one's complement
  23. 0:43or this two's complement so that's kind
  24. 0:44of the top stays the same
  25. 0:46it's what happens on the bottom except
  26. 0:49all ones we don't want to be zero we
  27. 0:51want all ones to be
  28. 0:52negative one and in fact i want you to
  29. 0:54lock that in all ones is always negative
  30. 0:56ones
  31. 0:57in this two's complement number okay
  32. 1:00it's called two's complement the
  33. 1:01hardware is now
  34. 1:01really really really easy we went from
  35. 1:03being complicated because these two
  36. 1:05zeros
  37. 1:05to really easy actually um by the way if
  38. 1:08you're a c programmer we'll teach you
  39. 1:10this next lecture
  40. 1:11you're going to see that in c it's
  41. 1:12called an int again we don't have how
  42. 1:14widened
  43. 1:15ins are so we have this thing called int
  44. 1:17types we'll tell you into int
  45. 1:19i for integer int with a number 8 16 or
  46. 1:2264
  47. 1:238 16 32 64 etc is a sign integer
  48. 1:26and this is two's complement so if you
  49. 1:28think of a signed integer
  50. 1:30the sign means we're doing this choose
  51. 1:32complement signing to be able to handle
  52. 1:33negative numbers okay unsigned means
  53. 1:35only positive numbers or
  54. 1:36only non-negatives zero or positive
  55. 1:38numbers
  56. 1:40okay here's the formula here's the way
  57. 1:42to think of it okay
  58. 1:43so for 32 bits the same as before
  59. 1:46all the lower bits are the same right
  60. 1:49lowest
  61. 1:50the lowest um the lowest coefficient
  62. 1:53times one the second lowest second
  63. 1:55lowest bit
  64. 1:56times two the third lowest bit times
  65. 1:59four etc
  66. 2:00except that you take this big guy
  67. 2:04and you say it's multiplied the
  68. 2:06coefficient but if the high a bit is
  69. 2:08it's that kind of that sign bit really
  70. 2:09it's not really a sign because it's not
  71. 2:10side magnitude but we think about the
  72. 2:12sign bit because
  73. 2:12the negative numbers all have a 1 there
  74. 2:14so we'll think about the sign bit for
  75. 2:16this case even though it's not sine
  76. 2:17magnitude
  77. 2:18times minus 2 to the 31
  78. 2:22minus 2 to the biggest biggest term so
  79. 2:24let's do this simple let's go back to a
  80. 2:25nibble now not five bits go back to a
  81. 2:26nibble four bits
  82. 2:28one one oh one well one one on one in
  83. 2:31unsigned
  84. 2:32well in hex1101 is d memorize that guy
  85. 2:35okay
  86. 2:36d that was going to be 13. what is it if
  87. 2:39we're talking about
  88. 2:40choose complement or sign assigned
  89. 2:43number two's complement
  90. 2:44let's do it again well this is 101 so
  91. 2:47this is the
  92. 2:48that's two no's one four no two
  93. 2:51one one and this is negative eight
  94. 2:55so watch this here's your five remember
  95. 2:58101 is five
  96. 3:00minus eight there's minus three okay
  97. 3:04that's one way to think about it minus
  98. 3:05three
  99. 3:08another way to think about it is flip
  100. 3:10the bits and add one
  101. 3:12and that's the equivalent number
  102. 3:14positively so let's do this
  103. 3:16let's flip the bits and add one so
  104. 3:19here we go 1 101 flip the bits
  105. 3:24001 0. add 1 001
  106. 3:28that's the negative of it so if that's a
  107. 3:30positive number now it's 3 that means 1
  108. 3:31101 must have been negative 3.
  109. 3:34let's do that let's reverse it now flip
  110. 3:36the bits again
  111. 3:381 100 add 1 1 101 and that's negative 3.
  112. 3:41so i went from negative 3
  113. 3:43to 3 to minus 3. by the way this picture
  114. 3:47is an optical illusion some people see a
  115. 3:49vas some people see a face
  116. 3:51sometimes it flops between a vase and a
  117. 3:52face the point of that showing you this
  118. 3:54is
  119. 3:54i can think of this the way i do it the
  120. 3:56way as a pro tip
  121. 3:58the way that i think about negative
  122. 4:00numbers and how to do them fast
  123. 4:01is i actually see when i say okay this
  124. 4:04leading one now
  125. 4:06i immediately see this and i flip i
  126. 4:08treat everyone as a zero into zero one
  127. 4:10which means i basically do this in my
  128. 4:12head
  129. 4:12okay and then i just quickly add one
  130. 4:14that s has to be negative three
  131. 4:16so i say to myself oh here's this now if
  132. 4:19this is the only one
  133. 4:20and i add one it's minus three see that
  134. 4:23because i
  135. 4:24quickly flipped it thinking that's the
  136. 4:25only one and if i add one to that
  137. 4:27i get all one one and i go back and i
  138. 4:29say what's that in decimal
  139. 4:30it's three so i'm able to really fast
  140. 4:33think about how to do that
  141. 4:34fast for yourself so
  142. 4:37flip the bits and add one
  143. 4:40so here we got a number line this
  144. 4:42beautiful number line as i
  145. 4:45count around as i'm counting around here
  146. 4:48in my binary odometer i've taken my
  147. 4:49odometer and kind of made it round
  148. 4:51here's the numbers that are being
  149. 4:51represented as i do this
  150. 4:540 1 2 3 up to 15. this is the same this
  151. 4:56is always the same
  152. 4:57just like the positive numbers are
  153. 4:59always the same 0 through 15. that's
  154. 5:01been that way for every
  155. 5:02representation we've seen so far all
  156. 5:03zeros through zero and all ones
  157. 5:05is zero through here in case 15 for five
  158. 5:07bits
  159. 5:09then it jumps to minus 16.
  160. 5:14jumps to minus 16. and you might
  161. 5:16remember this last slide watch this
  162. 5:18slide here
  163. 5:18remember this this is the negative
  164. 5:20number
  165. 5:22that's this minus 8 that pulls it back
  166. 5:24by doing minus 50 minus 16
  167. 5:28it took this this representation which
  168. 5:31used to be in one's complement minus 15
  169. 5:33and then all the ones was here we didn't
  170. 5:35want that we wanted to bring it back one
  171. 5:37more
  172. 5:37so that's why
  173. 5:40that term that top level term minus 16
  174. 5:43pulls that
  175. 5:44down so that this is the minor 16
  176. 5:46pulling that value down to minus 16. if
  177. 5:47i then stretch
  178. 5:4816 more guys or 15 16 more guys there
  179. 5:51this will end up at minus one
  180. 5:53right minus 16 to minus one is 16 things
  181. 5:56so that's why that works that cool
  182. 5:58that cool top level term this cool top
  183. 6:00level term
  184. 6:01it's because you grab that negative guy
  185. 6:03that smallest native number and
  186. 6:05pulled it over by exactly one and one's
  187. 6:07complement minus 15
  188. 6:09and now it's minus 16 which is why it
  189. 6:11works
  190. 6:12so let's look at our odometer now and
  191. 6:14let's see if our we have
  192. 6:15let's see if i overlap here ready there
  193. 6:18we go
  194. 6:18there's 0 to 15 and no overlap
  195. 6:22and as i said before all ones is
  196. 6:25negative one
  197. 6:26huh with the thing and then there's all
  198. 6:28ones negative one
  199. 6:30love it okay two to the n minus one
  200. 6:33non-negatives two to the n minus one
  201. 6:35negatives
  202. 6:36one zero okay how many positives ask
  203. 6:39yourself that
  204. 6:40and then we'll see this next encoding
  205. 6:42which is called
  206. 6:44the biased encoding now
  207. 6:48one of the things that's interesting is
  208. 6:50none of these guys
  209. 6:51had negative numbers they could
  210. 6:54represent
  211. 6:54but also all zeros being the smallest
  212. 6:57value
  213. 6:58here's here's another here's an analogy
  214. 6:59for you i'm recording some electrical
  215. 7:02signal okay electrical i'm recording
  216. 7:03some electrical signal
  217. 7:04and it's wavering from zero volts which
  218. 7:06is you know no
  219. 7:07no no let's say it's a dc dc voltage to
  220. 7:1031 volts okay
  221. 7:12and i'm going between that so you know
  222. 7:13nothing there and now goes to 31 and
  223. 7:15goes down to there
  224. 7:16and i want to have 31 different
  225. 7:17representations in there well
  226. 7:20let's say it's wiggling like this i if
  227. 7:22you think about graphing this would be a
  228. 7:24graph it's always a positive thing and i
  229. 7:25said to myself
  230. 7:26wouldn't it be cool to kind of grab this
  231. 7:27graph and bring it down so it wiggles
  232. 7:29around zero
  233. 7:30that's what this does it takes these
  234. 7:32values and
  235. 7:34pulls them down by some bias that to add
  236. 7:37an offset or we call the dc bias it will
  237. 7:40if i add negative 15 volts then it now
  238. 7:42sudden my signal is wiggling down here
  239. 7:44across zero so this bias brings things
  240. 7:47down to so it's kind of balanced or
  241. 7:48balanced across zero but now you see the
  242. 7:50problem is zero is one guy
  243. 7:53you're never going to have an equal
  244. 7:54number of positives and negatives
  245. 7:55because that's a total of
  246. 7:57like zero is one thing either you have
  247. 7:59two zeros and then an equal number of
  248. 8:00positive negatives or
  249. 8:01you have one zero which is what we want
  250. 8:03and then an unequal number positive
  251. 8:04negatives and if you recall
  252. 8:06i have one more negative than
  253. 8:09positive in a two's complement
  254. 8:11representation right i only go to 15
  255. 8:13but i go to minus 16 as my biggest
  256. 8:15negative okay
  257. 8:18in bias we are going to take my numbers
  258. 8:21my unsigned numbers 0 through 31
  259. 8:24and then add this bias on top of it
  260. 8:28pulls it down and what distinguishes it
  261. 8:31from that
  262. 8:32i could either add minus 16 to it so
  263. 8:34they're almost exactly the same as the
  264. 8:35two's complement that's not what we do
  265. 8:36what we typically do
  266. 8:38so by default we're going to have a bias
  267. 8:41chosen as minus 2 to the n minus 1 minus
  268. 8:441 meaning i got 5 bits here make it 4
  269. 8:46bits
  270. 8:462 to the 4 is 16 subtract by 1 is 15
  271. 8:49minus 15.
  272. 8:50that's my bias here okay so if i
  273. 8:54grab z all zero here's like here's the
  274. 8:56unsigned remember look unsigned pretend
  275. 8:58this is unsigned okay unsigned all
  276. 8:59zeroes to all one in that order
  277. 9:02if i grab it and shift it back by minus
  278. 9:0415
  279. 9:06what i get is all zeros is now the
  280. 9:09smallest negative number
  281. 9:11and it just keeps counting up one one
  282. 9:13one one one it's actually beautiful this
  283. 9:14way
  284. 9:15here's my odometer it just does the
  285. 9:18right thing
  286. 9:19so i start with the smallest negative
  287. 9:21number which again is this
  288. 9:22this value here and i just keep going up
  289. 9:25by one
  290. 9:26and nothing strange happens around zero
  291. 9:29i just keep incrementing minus one one
  292. 9:31nothing strange happens there
  293. 9:34unlike before it would snap around it
  294. 9:36would wrap and flip no it just continues
  295. 9:38to count up
  296. 9:39it just just works um you can think
  297. 9:42about by the way zero
  298. 9:44this is actually the equivalent of the
  299. 9:46bias when -15
  300. 9:48adds to the unsigned value 15 i get zero
  301. 9:51because the number is the unsigned value
  302. 9:54plus the bias
  303. 9:55okay it'll take some practice but we
  304. 9:58really like biased encoding
  305. 10:00for some applications and we'll see that
  306. 10:01actually later in this course
  307. 10:04and in summary we're at the finish line
  308. 10:06woo
  309. 10:09we represent things in computers as bit
  310. 10:12patterns
  311. 10:13n bits is two to the n things
  312. 10:16we've talked about five integer
  313. 10:18encodings each of them have com each of
  314. 10:20them have benefits and each of them have
  315. 10:22shortcomings one's complement and size
  316. 10:25magnitude have the most problems so we
  317. 10:26don't use those what we typically use
  318. 10:28is the remaining three we use unsigned
  319. 10:32choose complement and bias we're going
  320. 10:33to see those guys
  321. 10:35unsigned comes for free in a programming
  322. 10:37language here we go
  323. 10:38unsigned numbers are you int in c choose
  324. 10:41complement come as ins
  325. 10:43or int n underscore t
  326. 10:46biased there is no way to do that you
  327. 10:48have to almost store the bias together
  328. 10:50or remember that and if i'm shipping you
  329. 10:52some bits i'll ship you an unsigned
  330. 10:54and then i'll encode it for example if
  331. 10:56i'm going to store
  332. 10:570-31 let's give it a short some numbers
  333. 10:59like let's say minus
  334. 11:01see oh here we go minus 16 minus
  335. 11:0416 minus right here let's go back
  336. 11:07my minus 15 to 16. okay i want to store
  337. 11:10that number
  338. 11:11i'll store this how can i give this to
  339. 11:14you
  340. 11:14if you and i are agreeing on this
  341. 11:16there's no way in the computer do this
  342. 11:17but you aren't agreeing on it
  343. 11:18what we do is i send your mail saying ps
  344. 11:21the bias is going to be -15 and then i
  345. 11:23hand you the unsigned value
  346. 11:26so i take my number from minus 15 to 16.
  347. 11:29okay i add back the bias to find out
  348. 11:32what the bit pattern is
  349. 11:34okay all right finish it so so now i
  350. 11:36have
  351. 11:370 to 31. i encode that as a number 031 i
  352. 11:41ship it to you
  353. 11:42you apply the bias and now it goes back
  354. 11:44to the range that i had so i had a
  355. 11:45number from -15 to 16
  356. 11:46i converted to the unsigned equivalent
  357. 11:48of it give it to you
  358. 11:50and then you decode that that's the way
  359. 11:51you have to do biased
  360. 11:53overflow is when it wraps and the bits
  361. 11:56aren't enough to store the
  362. 11:57the actual number that you want because
  363. 11:58numbers are infinite but the the the
  364. 12:00numerals and the the space we have is a
  365. 12:02finite
  366. 12:03value you saw this ain't no free lunch
  367. 12:05to get
  368. 12:06negative numbers we had to steal
  369. 12:08something from the positive space we now
  370. 12:10couldn't go to 31 we only go to 15 and
  371. 12:12we saw all of them
  372. 12:13actually able to go to 15 except for
  373. 12:15except for
  374. 12:16uh bias which goes to 16.
  375. 12:20meta take away from the big big big big
  376. 12:22big big picture is we often make
  377. 12:25design decisions to make the hardware
  378. 12:26simple and one of the reasons we threw
  379. 12:28out sine magnitude and one's complement
  380. 12:30is that the hardware to build that would
  381. 12:31have been really hard but the hardware
  382. 12:33to build
  383. 12:34unsigned and side magnitude is really
  384. 12:36easy and in fact
  385. 12:38i give you a secret as we start this is
  386. 12:40like i'm foreshadowing probably 10 weeks
  387. 12:42ahead
  388. 12:42it's the same hardware it's the same
  389. 12:45hardware to do mathematics
  390. 12:47on unsigned and two's complement
  391. 12:51numbers the only thing you have to worry
  392. 12:53about is overflow because different
  393. 12:54things overflow because they're
  394. 12:55different the ranges are different right
  395. 12:56the ranges
  396. 12:57this is 31 and this is you could you can
  397. 12:59do something weird you could overflow
  398. 13:00negative there it's a little different
  399. 13:01for the overflow calculation but the
  400. 13:02rest of it's exactly the same
  401. 13:04so it's really cool you can use the same
  402. 13:05hardware to add two two's complement
  403. 13:07numbers
  404. 13:07as you could to add two unsigned numbers
  405. 13:10if you don't care about overflow
  406. 13:11that's pretty cool that's the end of
  407. 13:14this lecture on number representation
  408. 13:15folks it was really fun
  409. 13:17the next series of the next module will
  410. 13:19be a series of lectures
  411. 13:20on c programming i'll see you there

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 02.4 - Number Representation: Two's Complement, Bias, and Summary by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,525 words across 411 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.