1 00:00:01,984 --> 00:00:07,450 [ELECTRONIC STATIC SOUNDS] 2 00:00:07,450 --> 00:00:11,320 ROB: Okay, so what we just brought you to is CS50 Spaces. 3 00:00:11,320 --> 00:00:12,325 ALI: Wait, wait back up. 4 00:00:12,325 --> 00:00:13,175 Welcome to section! 5 00:00:13,175 --> 00:00:13,860 ROB: Welcome to section! 6 00:00:13,860 --> 00:00:14,742 ALI: Yay! 7 00:00:14,742 --> 00:00:16,840 ROB: Supersection! 8 00:00:16,840 --> 00:00:18,610 ALI: I'm Ali and this is Rob. 9 00:00:18,610 --> 00:00:20,680 Okay, now we'll explain Spaces. 10 00:00:20,680 --> 00:00:23,650 ROB: So what we just brought you to is CS50 Spaces. 11 00:00:23,650 --> 00:00:29,260 You'll be using this a lot in your sections this semester. 12 00:00:29,260 --> 00:00:32,110 Basically, we've already had you download the appliance. 13 00:00:32,110 --> 00:00:38,630 And you can think of CS50 Spaces as a web interface talking to an appliance 14 00:00:38,630 --> 00:00:40,670 that we have on some server somewhere. 15 00:00:40,670 --> 00:00:42,990 So you can run your code in this interface, and we'll 16 00:00:42,990 --> 00:00:44,180 see how to do things. 17 00:00:44,180 --> 00:00:48,200 You can also look at the code of other people in the section, and-- 18 00:00:48,200 --> 00:00:51,920 ALI: And as some people have found out, you can chat people on the side. 19 00:00:51,920 --> 00:00:54,230 And we'll all see it, too, so that's exciting. 20 00:00:54,230 --> 00:00:55,655 Come on in, come and sit. 21 00:00:55,655 --> 00:00:57,060 Take a seat. 22 00:00:57,060 --> 00:00:57,840 >> ROB: So, sections are-- 23 00:00:57,840 --> 00:00:59,380 ALI: No, no, you guys can come. 24 00:00:59,380 --> 00:01:03,356 ROB: Sections are going to be much more interactive this semester. 25 00:01:03,356 --> 00:01:04,180 ALI: Oh, he's like-- 26 00:01:04,180 --> 00:01:05,840 Oh. 27 00:01:05,840 --> 00:01:07,530 Okay, cool. 28 00:01:07,530 --> 00:01:15,630 So if you guys are just coming in, you can go to this link, if I can get 29 00:01:15,630 --> 00:01:20,190 there, right there. 30 00:01:20,190 --> 00:01:22,780 We can't really write it, because there isn't board space, but this link 31 00:01:22,780 --> 00:01:27,160 right here, go to that on your computers, and you'll enter CS50 32 00:01:27,160 --> 00:01:29,810 Spaces, which is a pretty cool thing. 33 00:01:32,460 --> 00:01:33,830 Okay. 34 00:01:33,830 --> 00:01:35,370 Do you have a problem? 35 00:01:35,370 --> 00:01:39,930 ROB: So you can find the problems-- 36 00:01:39,930 --> 00:01:42,720 we need to be able to write this somewhere. 37 00:01:42,720 --> 00:01:50,040 So if you go to cs50.net/psets and this is the hacker supersection-- 38 00:01:50,040 --> 00:01:55,870 and you go into the Hacker Edition of pset one, look at the specification 39 00:01:55,870 --> 00:01:57,620 somewhere down on page-- 40 00:02:01,940 --> 00:02:04,280 >> ALI: So basically, while Rob's looking, what we're going to do in 41 00:02:04,280 --> 00:02:06,650 section today is there is a section of problems-- 42 00:02:06,650 --> 00:02:07,600 ROB: --on page eight. 43 00:02:07,600 --> 00:02:09,300 ALI: --called a section of problems-- 44 00:02:09,300 --> 00:02:10,280 ROB: Section of questions. 45 00:02:10,280 --> 00:02:11,630 ALI: Sorry, a section of questions. 46 00:02:11,630 --> 00:02:14,450 And those are what we're going to go over-- 47 00:02:14,450 --> 00:02:16,370 those are what we're going to go over today in section. 48 00:02:16,370 --> 00:02:20,010 And we're going to code it on CS50 Spaces, and hopefully that will work. 49 00:02:20,010 --> 00:02:22,210 And we can just talk through them. 50 00:02:22,210 --> 00:02:24,510 And you guys can ask questions whenever you feel-- 51 00:02:24,510 --> 00:02:27,890 >> ROB: So this is getting to be a common thing with most psets. 52 00:02:27,890 --> 00:02:31,000 I think with this one, it says you don't have to hand these questions in. 53 00:02:31,000 --> 00:02:36,780 But the idea is that these questions are put on the pset, and you can come 54 00:02:36,780 --> 00:02:39,790 to section to have those questions answered. 55 00:02:39,790 --> 00:02:42,730 Or if you don't come to section, you can answer them on your own or get 56 00:02:42,730 --> 00:02:44,250 help at office hours or something. 57 00:02:44,250 --> 00:02:47,540 But these are questions that are supposed to prep you for the problem set. 58 00:02:47,540 --> 00:02:50,910 And on the hacker edition, a lot of the questions might just be about 59 00:02:50,910 --> 00:02:54,170 expanding your current CS knowledge. 60 00:02:54,170 --> 00:02:55,370 ALI: Yep, pretty much. 61 00:02:55,370 --> 00:02:57,950 >> Okay, so, is everyone on CS50 Spaces? 62 00:02:57,950 --> 00:02:59,457 Hi, come in. 63 00:02:59,457 --> 00:03:00,828 SPEAKER 1: Can we see the URL one more time? 64 00:03:04,030 --> 00:03:07,000 ROB: Yeah, this will be easier once you are actually in your sections, and 65 00:03:07,000 --> 00:03:10,760 your TF can just email you the URL beforehand. 66 00:03:10,760 --> 00:03:11,870 ALI: Yay, take a seat. 67 00:03:11,870 --> 00:03:13,170 There are seats here. 68 00:03:13,170 --> 00:03:14,420 The front isn't that bad. 69 00:03:17,112 --> 00:03:18,362 What? 70 00:03:20,650 --> 00:03:23,590 So, CS50 Spaces, we're getting there. 71 00:03:23,590 --> 00:03:26,760 Does anyone have any questions before we get into-- what? 72 00:03:26,760 --> 00:03:28,060 ROB: There's not even any chalk. 73 00:03:28,060 --> 00:03:29,270 [INAUDIBLE] 74 00:03:29,270 --> 00:03:31,540 ALI: Sorry. 75 00:03:31,540 --> 00:03:32,950 You could use your fingernails? 76 00:03:32,950 --> 00:03:38,210 We're in a very low-tech room for a very high-tech class. 77 00:03:38,210 --> 00:03:40,795 Okay, is everyone kind of good? 78 00:03:40,795 --> 00:03:44,310 Does anyone have still questions about it? 79 00:03:44,310 --> 00:03:48,540 As I said before, we're going to go through the section of questions in 80 00:03:48,540 --> 00:03:51,340 the pset specifications. 81 00:03:51,340 --> 00:03:53,490 So there's just a couple of problems that we're going to 82 00:03:53,490 --> 00:03:57,490 code up in CS50 Spaces. 83 00:03:57,490 --> 00:03:58,740 Is everyone good? 84 00:04:01,010 --> 00:04:01,360 Okay. 85 00:04:01,360 --> 00:04:02,312 Good? 86 00:04:02,312 --> 00:04:04,260 ROB: Do you have a laptop? 87 00:04:04,260 --> 00:04:05,150 ALI: You can hang out with Lucas. 88 00:04:05,150 --> 00:04:06,960 He's sitting right next to you. 89 00:04:06,960 --> 00:04:10,030 Bonding time. 90 00:04:10,030 --> 00:04:10,990 >> ROB: So, start with the first problem? 91 00:04:10,990 --> 00:04:11,260 ALI: Yeah. 92 00:04:11,260 --> 00:04:11,820 We can start. 93 00:04:11,820 --> 00:04:12,390 Do you want me-- 94 00:04:12,390 --> 00:04:13,700 I can go. 95 00:04:13,700 --> 00:04:16,666 So, we'll go to the pset specs. 96 00:04:16,666 --> 00:04:17,640 Oh, why is it-- 97 00:04:17,640 --> 00:04:18,890 ROB: Control. 98 00:04:21,769 --> 00:04:25,770 ALI: Okay, so are we going into answer these things as well? 99 00:04:25,770 --> 00:04:26,190 ROB: Oh, yeah. 100 00:04:26,190 --> 00:04:27,280 So-- 101 00:04:27,280 --> 00:04:29,675 ALI: Oh, did everyone watch Rob's starring shorts? 102 00:04:32,430 --> 00:04:33,360 Okay, cool. 103 00:04:33,360 --> 00:04:36,870 ROB: Yeah, I don't think we necessarily expected you to have 104 00:04:36,870 --> 00:04:40,650 watched it before coming to this section, but we can discuss those 105 00:04:40,650 --> 00:04:43,870 problems beforehand, since they are under the section of questions things. 106 00:04:43,870 --> 00:04:46,480 So if you haven't watched it, don't worry. 107 00:04:46,480 --> 00:04:48,630 Those who have can try and answer. 108 00:04:48,630 --> 00:04:53,875 >> So, the first question, what is a pre-processor, how does #include 109 00:04:53,875 --> 00:04:56,080 relate? 110 00:04:56,080 --> 00:04:58,440 So does anyone have answer for that? 111 00:04:58,440 --> 00:04:59,320 ALI: You guys can talk it out. 112 00:04:59,320 --> 00:05:00,308 Sure, go ahead. 113 00:05:00,308 --> 00:05:03,766 SPEAKER 2: #include is some pre-written code, and instead of 114 00:05:03,766 --> 00:05:08,706 copying it and pasting it in your program, by just saying include it, 115 00:05:08,706 --> 00:05:14,140 the pre-processor knows that it's there and that it should add it in 116 00:05:14,140 --> 00:05:17,520 later or before anything else happens. 117 00:05:17,520 --> 00:05:18,466 ALI: Yeah, awesome. 118 00:05:18,466 --> 00:05:19,440 Cool. 119 00:05:19,440 --> 00:05:23,820 ROB: So when you're actually compiling a program, the pre-processor is this 120 00:05:23,820 --> 00:05:25,230 first step. 121 00:05:25,230 --> 00:05:27,620 Compilation happens in four big steps. 122 00:05:27,620 --> 00:05:31,120 So the pre-processor is the first big one, and it's this guy that goes 123 00:05:31,120 --> 00:05:33,510 through and looks for all of these hash symbols. 124 00:05:33,510 --> 00:05:36,610 And any line that begins with a hash, the pre-processor looks at it and sees 125 00:05:36,610 --> 00:05:37,880 if it can process it. 126 00:05:37,880 --> 00:05:43,800 So #include tells the pre-processor to look for some cs50.h 127 00:05:43,800 --> 00:05:47,830 file and just copy and paste its contents into this file. 128 00:05:47,830 --> 00:05:50,970 So you can really #include anything you want, but it's mostly going to be 129 00:05:50,970 --> 00:05:53,700 .h files. 130 00:05:53,700 --> 00:05:55,820 We have not gotten to #define yet, so that doesn't matter. 131 00:05:55,820 --> 00:05:57,620 ALI: Yeah, we're good on that one. 132 00:05:57,620 --> 00:05:59,450 Does anyone have any questions about that? 133 00:05:59,450 --> 00:06:01,090 Are we good? 134 00:06:01,090 --> 00:06:02,430 Next question. 135 00:06:02,430 --> 00:06:07,096 >> SPEAKER 3: In the short, there was something about .c as well? 136 00:06:07,096 --> 00:06:09,110 Is that relevant or is it irrelevant? 137 00:06:09,110 --> 00:06:11,210 ROB: About .c with the pre-processor? 138 00:06:11,210 --> 00:06:13,990 SPEAKER 3: Yeah, or did I make something up? 139 00:06:13,990 --> 00:06:18,290 ROB: So the short might have been showing pre-processing this file and 140 00:06:18,290 --> 00:06:24,240 outputting it to another .c file, so you can pre-process this file-- 141 00:06:24,240 --> 00:06:30,000 when you just run like, make hello or clang hello.c, you are doing all 142 00:06:30,000 --> 00:06:31,730 the compilation in one big step. 143 00:06:31,730 --> 00:06:34,780 But you can explicitly do it into its separate steps. 144 00:06:34,780 --> 00:06:36,950 So first you can pre-process it. 145 00:06:36,950 --> 00:06:42,260 Then you can compile, then you can assemble, and then you can link. 146 00:06:42,260 --> 00:06:43,620 We'll get to those other ones. 147 00:06:43,620 --> 00:06:47,410 But pre-processing, you kind of pre-process it, and then it goes into 148 00:06:47,410 --> 00:06:49,270 another .c file. 149 00:06:49,270 --> 00:06:51,430 Because pre-processing doesn't really change anything. 150 00:06:51,430 --> 00:06:52,800 It's just a bunch of copy and pasting. 151 00:06:52,800 --> 00:06:56,760 You could manually copy and paste it yourself. 152 00:06:56,760 --> 00:06:59,630 ALI: And to be clear, the .c file is a C file, so it's 153 00:06:59,630 --> 00:07:00,930 written in C code. 154 00:07:00,930 --> 00:07:03,300 So it's going from C code to C code. 155 00:07:03,300 --> 00:07:05,210 You're just adding more code with the pound. 156 00:07:05,210 --> 00:07:08,575 ROB: When you pre-processed it, it's still a valid C file. 157 00:07:08,575 --> 00:07:09,940 SPEAKER 3: Okay. 158 00:07:09,940 --> 00:07:10,540 ALI: Good question. 159 00:07:10,540 --> 00:07:11,370 I like that. 160 00:07:11,370 --> 00:07:12,755 Okay, next question. 161 00:07:17,870 --> 00:07:19,660 >> What's the compiler? 162 00:07:19,660 --> 00:07:20,824 Anyone? 163 00:07:20,824 --> 00:07:21,286 Yes. 164 00:07:21,286 --> 00:07:24,650 SPEAKER 4: It changes the pre-processed code into assembly. 165 00:07:24,650 --> 00:07:25,110 ALI: Perfect. 166 00:07:25,110 --> 00:07:25,390 ROB: Yes. 167 00:07:25,390 --> 00:07:25,720 ALI: Cool. 168 00:07:25,720 --> 00:07:28,270 ROB: So that's what the compiler is doing specifically 169 00:07:28,270 --> 00:07:30,740 when we're using Clang. 170 00:07:30,740 --> 00:07:35,100 In a very general sense, a compiler is just like taking code in one language 171 00:07:35,100 --> 00:07:37,070 and turning it into another language. 172 00:07:37,070 --> 00:07:38,370 So in C-- 173 00:07:38,370 --> 00:07:43,750 or, well, Clang is taking code that's in C and transforming it to assembly. 174 00:07:43,750 --> 00:07:46,560 And you don't have to be able to understand the assembly at all. 175 00:07:46,560 --> 00:07:49,830 But that's the language that it's turning it into. 176 00:07:49,830 --> 00:07:50,230 >> ALI: Okay. 177 00:07:50,230 --> 00:07:52,920 And then, what's an assembler? 178 00:07:52,920 --> 00:07:54,270 Anyone? 179 00:07:54,270 --> 00:07:56,810 SPEAKER 5: [INAUDIBLE] to binary? 180 00:07:56,810 --> 00:07:57,260 ALI: Sorry, what? 181 00:07:57,260 --> 00:07:58,760 SPEAKER 5: [INAUDIBLE] to binary? 182 00:07:58,760 --> 00:07:59,260 ROB: Yep. 183 00:07:59,260 --> 00:07:59,880 ALI: Yes. 184 00:07:59,880 --> 00:08:01,960 ROB: So the assembler-- 185 00:08:01,960 --> 00:08:05,830 assembly code is really, really close to what your computer can understand, 186 00:08:05,830 --> 00:08:09,390 but remember that it doesn't quite understand the text 187 00:08:09,390 --> 00:08:10,630 that is assembly code. 188 00:08:10,630 --> 00:08:14,570 You need to convert it into straight 1s and 0s. 189 00:08:14,570 --> 00:08:17,000 It's like a direct translation process. 190 00:08:17,000 --> 00:08:19,450 We could just give you a table that maps what each 191 00:08:19,450 --> 00:08:21,880 assembly instruction means. 192 00:08:21,880 --> 00:08:24,340 But the assembler is just doing this translation for you. 193 00:08:24,340 --> 00:08:28,790 It's transforming the assembly code into 1s and 0s. 194 00:08:28,790 --> 00:08:30,870 >> ALI: And then, for the last one, what's a linker? 195 00:08:30,870 --> 00:08:35,070 And how does -lcs50 relate? 196 00:08:35,070 --> 00:08:37,010 Anyone? 197 00:08:37,010 --> 00:08:41,440 ROB: This is the hardest and least explained one in the compiler video. 198 00:08:41,440 --> 00:08:48,980 ALI: Does anyone remember seeing -lcs50 in a command? 199 00:08:48,980 --> 00:08:50,690 SPEAKER 6: When you went to the make. 200 00:08:50,690 --> 00:08:51,980 ALI: It's in the make. 201 00:08:51,980 --> 00:08:55,780 Yeah, cool, okay. 202 00:08:55,780 --> 00:09:02,024 Is there any guesses or vague--yes, go. 203 00:09:02,024 --> 00:09:06,370 SPEAKER 7: I'm not sure, because your short was really [INAUDIBLE], but was it 204 00:09:06,370 --> 00:09:12,244 something about doing separate files with each library and then putting 205 00:09:12,244 --> 00:09:13,620 them all together in one library? 206 00:09:13,620 --> 00:09:14,400 ROB: Yes. 207 00:09:14,400 --> 00:09:23,000 So when you have hello.c, it's a really simple program. 208 00:09:23,000 --> 00:09:25,570 You're really only doing one thing in it. 209 00:09:25,570 --> 00:09:27,400 But when you get into other programs-- 210 00:09:27,400 --> 00:09:31,700 and I guess even when you GetString in your programs, you need to start 211 00:09:31,700 --> 00:09:33,830 including other files. 212 00:09:33,830 --> 00:09:38,000 So the CS50 library is one such file in which we have the implementations 213 00:09:38,000 --> 00:09:40,750 of GetString and GetInt and all those things. 214 00:09:40,750 --> 00:09:45,540 So what the linker does is it goes around looking through all of these 215 00:09:45,540 --> 00:09:49,400 files, seeing where functions are defined, making sure that when I call 216 00:09:49,400 --> 00:09:52,440 GetString somewhere, it knows that GetString means that it's the 217 00:09:52,440 --> 00:09:54,910 GetString over in the CS50 library. 218 00:09:54,910 --> 00:09:58,895 So the linker just takes all these files, smashes them together, and now 219 00:09:58,895 --> 00:10:01,020 you have an executable. 220 00:10:01,020 --> 00:10:05,600 So keep in mind, if you're using the CS50 library, you need both the 221 00:10:05,600 --> 00:10:10,370 #include at the top, and then also in your make command, as you 222 00:10:10,370 --> 00:10:12,630 said, you have to have the -lcs50. 223 00:10:12,630 --> 00:10:15,720 So you can't just have one. 224 00:10:15,720 --> 00:10:18,294 So that's just something to keep in mind for future reference. 225 00:10:18,294 --> 00:10:22,550 >> SPEAKER 8: So, let's say there was another library, like cs51.h, would we 226 00:10:22,550 --> 00:10:26,780 also have to add -lcs51? 227 00:10:26,780 --> 00:10:32,390 ROB: The only reason this -lcs50 works perfectly is because we have that set 228 00:10:32,390 --> 00:10:35,800 up in a specific place in your appliances so that it knows what 229 00:10:35,800 --> 00:10:38,550 -lcs50 means. 230 00:10:38,550 --> 00:10:43,240 We could just give you a cs50.c file, and then you could compile it, saying 231 00:10:43,240 --> 00:10:49,050 Clang hello.c cs50.c, and then it would make your executable by putting 232 00:10:49,050 --> 00:10:50,350 those two files together. 233 00:10:50,350 --> 00:10:56,520 It knows that -lcs50 means cs50.c over in some directory that we specified in 234 00:10:56,520 --> 00:10:58,350 your appliances. 235 00:10:58,350 --> 00:11:04,290 So if you wanted a cs51.c to be specifiable by just saying -lcs51, 236 00:11:04,290 --> 00:11:06,760 then we would also need to put it in that directory so it knows where to 237 00:11:06,760 --> 00:11:08,636 look for it. 238 00:11:08,636 --> 00:11:10,510 ALI: What was your question? 239 00:11:10,510 --> 00:11:13,975 >> SPEAKER 7: Why do you have to link the CS50 240 00:11:13,975 --> 00:11:18,940 if--wasn't it copy/pasted at the first step when you pre-processed it? 241 00:11:18,940 --> 00:11:19,700 ALI: Do you want to take it? 242 00:11:19,700 --> 00:11:20,880 ROB: Sure. 243 00:11:20,880 --> 00:11:28,028 So the cs50.h file is separate from the cs50.c C file. 244 00:11:28,028 --> 00:11:33,652 Have you gotten to function prototypes in class? 245 00:11:33,652 --> 00:11:34,350 Okay. 246 00:11:34,350 --> 00:11:40,255 So basically, the cs50.h file is just going to copy and paste-- 247 00:11:40,255 --> 00:11:44,040 ALI: Do you guys know what signatures are? 248 00:11:44,040 --> 00:11:46,580 Okay, so pretty much, if you look at-- 249 00:11:46,580 --> 00:11:50,530 ROB: Let's pretend cs50.h is not there. 250 00:11:50,530 --> 00:11:52,960 Now this file-- 251 00:11:52,960 --> 00:11:56,090 you're doing string s = GetString. 252 00:11:56,090 --> 00:11:59,350 But when we've gotten to this point in the code, it has no idea what 253 00:11:59,350 --> 00:12:00,230 GetString is. 254 00:12:00,230 --> 00:12:02,600 It knows it's a function, since you're calling it. 255 00:12:02,600 --> 00:12:06,500 But it doesn't know that it's actually supposed to return a string. 256 00:12:06,500 --> 00:12:14,530 So like I said, string s = GetInt, this doesn't make any sense, since 257 00:12:14,530 --> 00:12:17,530 you're signing an integer to a string. 258 00:12:17,530 --> 00:12:22,270 But it doesn't know that GetString does make sense, because it doesn't 259 00:12:22,270 --> 00:12:24,380 know that GetString returns a string. 260 00:12:24,380 --> 00:12:28,220 So what cs50.h says is something like this. 261 00:12:33,070 --> 00:12:37,750 ALI: So what Rob is typing is, it's a promise in a way that it's going to 262 00:12:37,750 --> 00:12:41,280 have this function called GetString. 263 00:12:41,280 --> 00:12:45,250 ROB: This is all that cs50.h is copying and pasting in here, as well 264 00:12:45,250 --> 00:12:47,620 as GetInt and all those things. 265 00:12:47,620 --> 00:12:51,520 And this just says that GetString does return a string. 266 00:12:51,520 --> 00:12:54,830 You don't know how it's implemented yet, but when we get to this line, now 267 00:12:54,830 --> 00:12:57,760 it knows that it correctly returns a string. 268 00:12:57,760 --> 00:13:03,022 >> SPEAKER 9: So if we didn't bother with the #include thing and instead 269 00:13:03,022 --> 00:13:07,130 just wrote the prototype for the ones that we are actually going to use? 270 00:13:07,130 --> 00:13:07,330 ROB: Yeah. 271 00:13:07,330 --> 00:13:08,870 So let's run this. 272 00:13:08,870 --> 00:13:09,110 SPEAKER 9: Is it? 273 00:13:09,110 --> 00:13:13,670 So, by default, it would do the -l to CS50 just because--. 274 00:13:13,670 --> 00:13:14,740 ROB: --expression. 275 00:13:14,740 --> 00:13:19,370 ALI: Yes, because as you said, it's in the make file. 276 00:13:19,370 --> 00:13:24,040 ROB: cs50.h also happens to be typedef char * to string. 277 00:13:24,040 --> 00:13:26,950 You can completely ignore what that means for now. 278 00:13:26,950 --> 00:13:31,430 But that is also something included in cs50.h. 279 00:13:31,430 --> 00:13:34,230 So now things worked perfectly fine. 280 00:13:34,230 --> 00:13:40,770 It runs exactly the same as it was when we just had 281 00:13:40,770 --> 00:13:45,120 the #include. 282 00:13:45,120 --> 00:13:49,200 And so including cs50.h happens to over-include things you need-- 283 00:13:49,200 --> 00:13:53,080 like, you don't use the GetInt in this program, but it doesn't matter. 284 00:13:53,080 --> 00:13:55,010 It'll just ignore it. 285 00:13:55,010 --> 00:14:02,400 ALI: And then when you do the -lcs50 over here and right here, what's going 286 00:14:02,400 --> 00:14:05,290 on is that it's having the actual implementable code. 287 00:14:05,290 --> 00:14:07,530 So that's where it actually is, doing the-- 288 00:14:07,530 --> 00:14:09,090 writing the code for GetString. 289 00:14:09,090 --> 00:14:11,180 So it's not just a promise anymore. 290 00:14:11,180 --> 00:14:16,520 It is actually going through and taking the string and all that stuff. 291 00:14:16,520 --> 00:14:17,600 Make sense? 292 00:14:17,600 --> 00:14:22,640 Questions? All right. 293 00:14:22,640 --> 00:14:24,466 Cool beans. 294 00:14:24,466 --> 00:14:27,630 >> ROB: Now we can move on to actual code. 295 00:14:27,630 --> 00:14:28,780 ALI: Okay. 296 00:14:28,780 --> 00:14:33,340 So here is the first problem. 297 00:14:33,340 --> 00:14:36,910 So it says write a program that prompts the user for a lowercase 298 00:14:36,910 --> 00:14:40,540 letter and then converts it to uppercase without using bitwise 299 00:14:40,540 --> 00:14:43,590 operations, as per the sample output below. 300 00:14:43,590 --> 00:14:48,780 So you can see, you run the program with the ./a.out, and then you're 301 00:14:48,780 --> 00:14:53,320 going to put in lowercase a, and then your program should give out capital 302 00:14:53,320 --> 00:15:00,590 A. So why don't you guys all give it a shot on CS50 Spaces? 303 00:15:00,590 --> 00:15:03,360 So you guys can all go here. 304 00:15:03,360 --> 00:15:06,650 And you can delete all the code in here. 305 00:15:06,650 --> 00:15:12,140 And then you can go ahead and start coding the thing. 306 00:15:12,140 --> 00:15:14,590 ROB: You'll probably want to use GetChar, that's 307 00:15:14,590 --> 00:15:16,190 included in the CS50 library. 308 00:15:16,190 --> 00:15:18,770 ALI: I guess it might be better if you save the top part. 309 00:15:21,452 --> 00:15:23,805 So you might want to just change the stuff in the middle. 310 00:15:28,710 --> 00:15:29,070 Go ahead and work. 311 00:15:29,070 --> 00:15:30,530 Feel free to work with each other. 312 00:15:30,530 --> 00:15:32,360 ROB: And you can ask questions if you have-- 313 00:15:32,360 --> 00:15:33,450 >> SPEAKER 10: What is a bitwise operation? 314 00:15:33,450 --> 00:15:35,100 ROB: So, ignore that for this problem. 315 00:15:35,100 --> 00:15:36,330 ALI: If you don't know it, that's good. 316 00:15:36,330 --> 00:15:38,300 ROB: We're going to use it in the next problem. 317 00:15:38,300 --> 00:15:40,750 But if you don't know what a bitwise operator is, that's fine. 318 00:15:40,750 --> 00:15:43,575 SPEAKER 10: Is that like turning it to ASCII code? 319 00:15:43,575 --> 00:15:44,050 ALI: No. 320 00:15:44,050 --> 00:15:44,750 ROB: No. 321 00:15:44,750 --> 00:15:46,220 You can do that for this problem. 322 00:15:46,220 --> 00:15:48,280 SPEAKER 10: How do you do that? 323 00:15:48,280 --> 00:15:52,460 So if only I had somewhere to write-- 324 00:15:52,460 --> 00:15:54,670 ALI: Or something to write with. 325 00:15:54,670 --> 00:15:56,780 ROB: I could just kind of type into this. 326 00:15:56,780 --> 00:15:59,045 ALI: Type at the top. 327 00:15:59,045 --> 00:16:01,390 ROB: Oh, I'm sitting on a mic. 328 00:16:01,390 --> 00:16:10,860 >> So, we saw in lecture that the ASCII value for capital A is 65. 329 00:16:10,860 --> 00:16:13,670 And capital B will be 66, and so on. 330 00:16:13,670 --> 00:16:20,860 So you can literally use 'A' to mean the number 65. 331 00:16:20,860 --> 00:16:24,600 Like, this is the value 65. 332 00:16:24,600 --> 00:16:30,950 I can do something like int x = 100 - 'A'. And now x will have the value 333 00:16:30,950 --> 00:16:33,810 100 - 65. 334 00:16:33,810 --> 00:16:36,708 ALI: You can run that and show that. 335 00:16:36,708 --> 00:16:38,420 Oh, maybe not. 336 00:16:38,420 --> 00:16:38,600 Never mind. 337 00:16:38,600 --> 00:16:40,970 ROB: I had to print it. 338 00:16:40,970 --> 00:16:43,730 SPEAKER 10: How do you turn it back to character? 339 00:16:43,730 --> 00:16:46,800 ALI: So if you don't-- 340 00:16:46,800 --> 00:16:51,450 you just force it to be char A, equals. 341 00:16:51,450 --> 00:16:53,630 ROB: So there is a-- 342 00:16:53,630 --> 00:16:58,440 the distinction between a char and an integer is just in terms of size. 343 00:16:58,440 --> 00:17:01,230 So an integer happens to be able to represent things up 344 00:17:01,230 --> 00:17:02,600 to, like, four billion. 345 00:17:02,600 --> 00:17:05,839 A char is only able to represent things up to 255. 346 00:17:05,839 --> 00:17:08,390 But there is no difference between them other than this. 347 00:17:08,390 --> 00:17:12,339 So you can say char c = 65. 348 00:17:12,339 --> 00:17:24,204 That's equivalent to saying char c equals 'A'. 349 00:17:24,204 --> 00:17:25,210 ALI: Oh, no. 350 00:17:25,210 --> 00:17:26,710 It doesn't actually save things. 351 00:17:26,710 --> 00:17:28,130 ROB: No, you can't do that. 352 00:17:28,130 --> 00:17:31,610 ALI: That's just-- 353 00:17:31,610 --> 00:17:34,920 [GROANS] 354 00:17:34,920 --> 00:17:40,700 >> ROB: Okay, so what Ali just wrote was a program which first prints 100 - 355 00:17:40,700 --> 00:17:46,290 'A' as an integer, which was 35, as we expect, because 100 minus the ASCII 356 00:17:46,290 --> 00:17:48,580 value of A is 65. 357 00:17:48,580 --> 00:17:54,100 Then she printed out using %c, which means interpret it as a char. 358 00:17:54,100 --> 00:17:57,050 So 100 - a is 35. 359 00:17:57,050 --> 00:18:00,990 Interpreting that as a character happens to be the hash symbol. 360 00:18:00,990 --> 00:18:06,160 If you look at asciitable.com or whatever, you'll see that 35 is the 361 00:18:06,160 --> 00:18:07,410 hash symbol. 362 00:18:09,940 --> 00:18:12,700 ALI: Okay, any other clarifications about the problem? 363 00:18:16,000 --> 00:18:18,180 Okay, you guys can go ahead and do it then. 364 00:18:18,180 --> 00:18:20,330 Feel free to ask questions or talk to each other. 365 00:18:20,330 --> 00:18:24,390 Or if you're already done, you can relax. 366 00:18:24,390 --> 00:18:29,640 >> LUCAS: Are they the same class, all the lowercase letters in order and 367 00:18:29,640 --> 00:18:32,923 also the uppercase are also in order, because that's something useful for 368 00:18:32,923 --> 00:18:33,990 the problem. 369 00:18:33,990 --> 00:18:35,620 ALI: Good point, Lucas. 370 00:18:35,620 --> 00:18:37,430 So, did you guys all get that? 371 00:18:37,430 --> 00:18:37,640 SPEAKER 11: Yes. 372 00:18:37,640 --> 00:18:39,535 That's how you do it, right? 373 00:18:39,535 --> 00:18:39,960 ALI: Right. 374 00:18:39,960 --> 00:18:40,415 ROB: Yeah. 375 00:18:40,415 --> 00:18:47,270 SPEAKER 11: [INAUDIBLE] 376 00:18:47,270 --> 00:18:48,330 ALI: All right. 377 00:18:48,330 --> 00:18:49,350 ROB: What is the question asking? 378 00:18:49,350 --> 00:18:50,590 Just to convert-- 379 00:18:50,590 --> 00:18:52,560 ALI: Convert the lowercase to the uppercase. 380 00:18:52,560 --> 00:18:53,100 That's it. 381 00:18:53,100 --> 00:18:54,700 ROB: Okay. 382 00:18:54,700 --> 00:18:56,180 ALI: Should we write it? 383 00:18:56,180 --> 00:18:59,340 I guess we'll just look at someone else's. 384 00:18:59,340 --> 00:19:07,140 ROB: So for those who might be stuck, a way-- 385 00:19:07,140 --> 00:19:15,160 if I have some char c, and let's say it happens to be the letter D. 386 00:19:15,160 --> 00:19:20,760 So now how can I figure out what letter of the alphabet C is? 387 00:19:20,760 --> 00:19:25,620 Not D, but I mean, D happens to be the fourth letter of the alphabet. 388 00:19:25,620 --> 00:19:28,910 And if we start counting from 0, then it's the third letter of the alphabet. 389 00:19:28,910 --> 00:19:36,240 So if A is 0, B is 1, C is 2, D is 3, how can I figure out int position-- 390 00:19:36,240 --> 00:19:40,780 what position of the alphabet C is in? 391 00:19:40,780 --> 00:19:42,333 Does anyone have any ideas? 392 00:19:42,333 --> 00:19:45,440 ALI: I think they're all coding. 393 00:19:45,440 --> 00:19:46,440 ROB: What about the first one? 394 00:19:46,440 --> 00:19:48,608 SPEAKER 12: So whatever A is, subtract the first one? 395 00:19:48,608 --> 00:19:49,030 ROB: Yeah. 396 00:19:49,030 --> 00:19:49,950 ALI: Yeah, awesome. 397 00:19:49,950 --> 00:19:51,765 So you could do capital D-- 398 00:19:51,765 --> 00:19:52,840 oh, sorry. 399 00:19:52,840 --> 00:19:55,620 You take the character and you subtract the first one, as you said. 400 00:19:55,620 --> 00:20:03,940 ROB: So if D is something like 68, and we subtract A, which is 65, then we get 401 00:20:03,940 --> 00:20:07,130 3, telling us that D is the third letter of the alphabet 402 00:20:07,130 --> 00:20:09,290 starting from 0. 403 00:20:09,290 --> 00:20:11,310 So you can use that. 404 00:20:11,310 --> 00:20:13,830 Now we know what letter of the alphabet, in terms of 405 00:20:13,830 --> 00:20:16,060 capital letters, or-- 406 00:20:16,060 --> 00:20:18,330 we could do the same thing for lowercase letters to figure out what 407 00:20:18,330 --> 00:20:20,170 lowercase position we're in. 408 00:20:20,170 --> 00:20:25,690 And we can use that to then convert that to uppercase using a 409 00:20:25,690 --> 00:20:26,970 very similar idea. 410 00:20:31,470 --> 00:20:32,670 Ask for suggestions? 411 00:20:32,670 --> 00:20:35,810 >> ALI: Do you guys--wait, I don't know how far you guys are. 412 00:20:35,810 --> 00:20:40,060 Are most of you done, are you still working, are you stuck? 413 00:20:40,060 --> 00:20:42,020 You guys can shout out-- 414 00:20:42,020 --> 00:20:42,330 stuck. 415 00:20:42,330 --> 00:20:43,210 One person's stuck. 416 00:20:43,210 --> 00:20:43,560 Cool. 417 00:20:43,560 --> 00:20:44,700 I tend to be stuck, too. 418 00:20:44,700 --> 00:20:45,415 SPEAKER 13: I'm done. 419 00:20:45,415 --> 00:20:46,410 ALI: You're done? 420 00:20:46,410 --> 00:20:47,480 Okay. 421 00:20:47,480 --> 00:20:47,935 Done. 422 00:20:47,935 --> 00:20:49,300 SPEAKER 13: [INAUDIBLE] 423 00:20:49,300 --> 00:20:50,600 ALI: Yeah, cool. 424 00:20:50,600 --> 00:20:53,720 Are you checking that it's in lowercase form? 425 00:20:53,720 --> 00:20:56,730 Okay, cool. 426 00:20:56,730 --> 00:20:59,882 Where are other people? 427 00:20:59,882 --> 00:21:06,140 Did this hint help you, as for un-sticking yourself? 428 00:21:06,140 --> 00:21:09,778 SPEAKER 14: Not really, but just because, I don't know, I'm not 429 00:21:09,778 --> 00:21:11,590 digesting it yet. 430 00:21:11,590 --> 00:21:12,640 ALI: Okay, cool. 431 00:21:12,640 --> 00:21:13,431 Do you want to try-- 432 00:21:13,431 --> 00:21:16,140 or do you want to go and talk? 433 00:21:16,140 --> 00:21:18,590 >> ROB: What I was going to say is-- 434 00:21:18,590 --> 00:21:19,890 so using this. 435 00:21:19,890 --> 00:21:22,650 Do you understand how we got what position of the alphabet 436 00:21:22,650 --> 00:21:24,820 the letter is in? 437 00:21:24,820 --> 00:21:30,616 SPEAKER 14: Okay, so when you put things in single quotation, 438 00:21:30,616 --> 00:21:35,030 that returns a number? 439 00:21:35,030 --> 00:21:37,100 ROB: Yes. 440 00:21:37,100 --> 00:21:40,840 It will be translated to the ASCII value it represents. 441 00:21:40,840 --> 00:21:43,350 So do you want to go to ASCII table or whatever it is? 442 00:21:46,456 --> 00:21:47,840 ALI: Just one of these? 443 00:21:47,840 --> 00:21:49,970 ROB: Yep. 444 00:21:49,970 --> 00:21:55,270 So when you put any of these symbols, ignoring a lot of the-- 445 00:21:55,270 --> 00:21:56,450 ALI: [INAUDIBLE] 446 00:21:56,450 --> 00:21:57,620 ROB: Oh, using the cursor. 447 00:21:57,620 --> 00:21:58,380 ALI: Yeah. 448 00:21:58,380 --> 00:21:59,690 That was exciting. 449 00:21:59,690 --> 00:22:04,050 ROB: So, ignoring these ones on the left, which are special symbols-- 450 00:22:04,050 --> 00:22:08,520 if you put any of these symbols in single quotes, then it will be 451 00:22:08,520 --> 00:22:11,620 translated to this value on the left. 452 00:22:11,620 --> 00:22:13,660 ALI: This is the decimal number for it. 453 00:22:13,660 --> 00:22:19,710 It's like A matches with 65, B to 66, and you'll notice that they're all in 454 00:22:19,710 --> 00:22:21,540 alphabetical order, which makes a difference. 455 00:22:21,540 --> 00:22:27,110 So, as Rob says, before, in that code, we were calculating the distance from 456 00:22:27,110 --> 00:22:29,610 the first letter, per se. 457 00:22:29,610 --> 00:22:32,820 And that'll be the same, whether it's uppercase or lowercase. 458 00:22:32,820 --> 00:22:38,390 ROB: So when we did D, 68 minus A, 65, we get 3. 459 00:22:38,390 --> 00:22:42,370 Because D is three positions into the alphabet. 460 00:22:42,370 --> 00:22:46,680 ALI: So then how would you translate that over to find the little d? 461 00:22:46,680 --> 00:22:47,240 ROB: Yeah. 462 00:22:47,240 --> 00:22:52,230 So if I have 3 now, I know I want to go three letters into the-- 463 00:22:52,230 --> 00:22:54,830 we happen to be going lowercase now, but let's say I want to go three 464 00:22:54,830 --> 00:22:58,610 positions into the lowercase side of things. 465 00:22:58,610 --> 00:23:01,400 So how can I do that? 466 00:23:01,400 --> 00:23:04,240 I know lowercase a is 97. 467 00:23:04,240 --> 00:23:07,535 So how do I find three positions into the lowercase letters? 468 00:23:10,461 --> 00:23:11,934 >> SPEAKER 15: I have one question actually. 469 00:23:11,934 --> 00:23:12,425 ALI: Yeah, go ahead. 470 00:23:12,425 --> 00:23:14,880 SPEAKER 15: So for this, it doesn't really matter if I know this position, 471 00:23:14,880 --> 00:23:16,360 like, I don't need this table. 472 00:23:16,360 --> 00:23:16,840 ROB: Nope. 473 00:23:16,840 --> 00:23:19,380 You will never need to use any of these numbers. 474 00:23:19,380 --> 00:23:23,670 And this is an important point in your programs that you should never hard 475 00:23:23,670 --> 00:23:25,500 code any of these constants. 476 00:23:25,500 --> 00:23:31,190 Use 'A'. never use 65 or 97. 477 00:23:31,190 --> 00:23:34,370 ALI: Those are called magic numbers, and they're really confusing. 478 00:23:34,370 --> 00:23:37,080 Like, when you're debugging a code, you might not remember what 479 00:23:37,080 --> 00:23:38,020 you used them for. 480 00:23:38,020 --> 00:23:40,770 And for us grading your code, we won't really know what 481 00:23:40,770 --> 00:23:41,750 you're using them for. 482 00:23:41,750 --> 00:23:44,815 So it's better if you actually use the characters so it makes 483 00:23:44,815 --> 00:23:46,065 more sense to people. 484 00:23:49,460 --> 00:23:51,160 Okay, any other questions? 485 00:23:51,160 --> 00:23:52,975 Are more people done, or-- 486 00:23:56,243 --> 00:23:57,493 I guess we can check. 487 00:23:59,870 --> 00:24:02,410 It's really creepy that you can see people's code. 488 00:24:02,410 --> 00:24:04,970 ROB: Yeah. 489 00:24:04,970 --> 00:24:06,410 We don't have to do that here. 490 00:24:06,410 --> 00:24:08,380 We don't know people's names, either. 491 00:24:08,380 --> 00:24:10,940 ALI: Oh yeah, well that makes it better, so we'll be even more unbiased 492 00:24:10,940 --> 00:24:14,820 instead of just randomly picking someone. 493 00:24:14,820 --> 00:24:15,360 Don't worry. 494 00:24:15,360 --> 00:24:16,265 I won't do it. 495 00:24:16,265 --> 00:24:20,396 If you have random things about-- never mind. 496 00:24:20,396 --> 00:24:20,870 Okay. 497 00:24:20,870 --> 00:24:22,556 How are people doing? 498 00:24:22,556 --> 00:24:26,950 >> SPEAKER 17: So the ninth line should print out a character? 499 00:24:26,950 --> 00:24:27,430 ROB: Yes. 500 00:24:27,430 --> 00:24:27,910 ALI: Yeah. 501 00:24:27,910 --> 00:24:30,770 So if you go down-- 502 00:24:30,770 --> 00:24:32,200 ROB: Oh, can't do that. 503 00:24:32,200 --> 00:24:35,400 ALI: You'll see that it printed the hash symbol. 504 00:24:35,400 --> 00:24:37,180 SPEAKER 17: Oh, okay. 505 00:24:37,180 --> 00:24:41,290 ROB: I guess another way you can look at things is we're printing two 506 00:24:41,290 --> 00:24:42,540 characters. 507 00:24:44,500 --> 00:24:47,740 First one we're printing is the letter A. 508 00:24:47,740 --> 00:24:51,610 The next one we're printing is just 65. 509 00:24:51,610 --> 00:24:54,450 It'll probably yell at me for these. 510 00:24:54,450 --> 00:25:00,060 So if we just run this, you'll notice that it prints A both times. 511 00:25:00,060 --> 00:25:02,070 Because we're asking it the same thing. 512 00:25:02,070 --> 00:25:07,840 We're asking it to print the letter A. And then we're asking it to print the 513 00:25:07,840 --> 00:25:13,820 number 65 interpreted as a character, which is the same thing. 514 00:25:13,820 --> 00:25:15,100 ALI: Do you have something to say? 515 00:25:15,100 --> 00:25:18,150 Oh, just kidding, sorry. 516 00:25:18,150 --> 00:25:20,640 All right, how are people-- 517 00:25:20,640 --> 00:25:22,280 ROB: We can just walk through it. 518 00:25:22,280 --> 00:25:25,250 >> ALI: Okay, so how do you start? 519 00:25:25,250 --> 00:25:26,500 Anyone? 520 00:25:28,030 --> 00:25:34,486 As a hint, we have to get something from the people, from the typers. 521 00:25:34,486 --> 00:25:36,850 SPEAKER 18: [INAUDIBLE] 522 00:25:36,850 --> 00:25:38,290 ALI: Oh yeah, a prompt, perfect. 523 00:25:38,290 --> 00:25:40,660 So we'll type-- 524 00:25:40,660 --> 00:25:41,670 what do we type? 525 00:25:41,670 --> 00:25:43,660 Anyone? 526 00:25:43,660 --> 00:25:46,480 Or should I just type it? 527 00:25:46,480 --> 00:25:47,530 Are we actually typing the-- 528 00:25:47,530 --> 00:25:48,430 ROB: Sure. 529 00:25:48,430 --> 00:25:50,990 ALI: So we'll type a printf to prompt it, so we can be 530 00:25:50,990 --> 00:25:54,810 like, give me a character. 531 00:25:54,810 --> 00:25:55,710 Okay, and then what? 532 00:25:55,710 --> 00:25:56,664 Why is it doing that? 533 00:25:56,664 --> 00:25:57,620 ROB: I don't know. 534 00:25:57,620 --> 00:26:00,070 ALI: Okay. 535 00:26:00,070 --> 00:26:03,680 So now, we're telling them to give us a character. 536 00:26:03,680 --> 00:26:07,064 But then how do you actually get that character? 537 00:26:07,064 --> 00:26:10,060 SPEAKER 19: Use GetString. 538 00:26:10,060 --> 00:26:12,040 ALI: GetString? 539 00:26:12,040 --> 00:26:12,850 GetChar? 540 00:26:12,850 --> 00:26:17,038 Okay, so what's the difference between a string and a char? 541 00:26:17,038 --> 00:26:20,020 SPEAKER 19: Strings are a series, like an array of characters. 542 00:26:20,020 --> 00:26:21,910 >> ALI: Cool, yeah. 543 00:26:21,910 --> 00:26:25,550 So in this problem, we only need to consider one character at a time, so 544 00:26:25,550 --> 00:26:28,400 we're only going to do GetChar for that instance. 545 00:26:28,400 --> 00:26:32,400 ROB: We could implement a function if we wanted that took an entire string 546 00:26:32,400 --> 00:26:35,750 and went over the string and changed all lowercase to uppercase and all 547 00:26:35,750 --> 00:26:37,380 uppercase to lowercase. 548 00:26:37,380 --> 00:26:39,170 But here, we're just asking you for one character. 549 00:26:39,170 --> 00:26:42,800 ALI: So now we have the character here, but then we need to save it. 550 00:26:42,800 --> 00:26:45,070 So then we'll add char c--what? 551 00:26:45,070 --> 00:26:46,070 SPEAKER 20: Define the variable. 552 00:26:46,070 --> 00:26:48,100 ALI: Yeah, exactly. 553 00:26:48,100 --> 00:26:49,585 So we have our character. 554 00:26:49,585 --> 00:26:51,766 ROB: I think you might be-- 555 00:26:51,766 --> 00:26:55,630 you're only three spaces in, which is why it's yelling at you. 556 00:26:55,630 --> 00:27:01,300 >> ALI: Okay, cool, now that we have tabbing set, what happens next? 557 00:27:01,300 --> 00:27:02,550 What's the next step? 558 00:27:05,590 --> 00:27:08,870 ROB: What our program should do is change a lowercase letter to an 559 00:27:08,870 --> 00:27:10,130 uppercase letter. 560 00:27:10,130 --> 00:27:15,478 What if I happen to enter the hash symbol? 561 00:27:15,478 --> 00:27:16,314 Is that-- 562 00:27:16,314 --> 00:27:16,732 ALI: It's a good symbol. 563 00:27:16,732 --> 00:27:18,270 We use it a lot. 564 00:27:18,270 --> 00:27:21,937 ROB: Is that a valid thing I can convert to an uppercase form? 565 00:27:21,937 --> 00:27:23,070 SPEAKER 21: No. 566 00:27:23,070 --> 00:27:24,070 ALI: No. 567 00:27:24,070 --> 00:27:24,900 We should check that. 568 00:27:24,900 --> 00:27:34,950 So we can have an if statement checking so if the c is greater than 569 00:27:34,950 --> 00:27:37,410 or equal to the lowercase 'a'-- 570 00:27:37,410 --> 00:27:45,490 so if we look at the chart, you'll notice that it has to be between here, 571 00:27:45,490 --> 00:27:47,670 at 97, and the lowercase a. 572 00:27:47,670 --> 00:27:52,110 And it can be any of these, and in increments all the way down. 573 00:27:52,110 --> 00:27:55,200 And then there's z at 122. 574 00:27:55,200 --> 00:27:58,215 And it has to fall between these two values. 575 00:27:58,215 --> 00:27:59,700 Does that make sense? 576 00:27:59,700 --> 00:28:04,130 ROB: So if c is not between 97 and 122-- 577 00:28:04,130 --> 00:28:09,960 or you should never need to use those numbers--if c is not between 'a' and 578 00:28:09,960 --> 00:28:15,110 'z,' then it wasn't a valid character for us to uppercase. 579 00:28:15,110 --> 00:28:18,590 ALI: So in code form, we say that if c is greater than or equal to single 580 00:28:18,590 --> 00:28:19,640 case lowercase-- 581 00:28:19,640 --> 00:28:21,270 wow, how did you say that? 582 00:28:21,270 --> 00:28:26,900 Okay, is greater than or equal to the lowercase 'a', and it has to be above 583 00:28:26,900 --> 00:28:32,250 the lowercase 'z.' So it has to be less than or equal to lowercase 'z'. 584 00:28:32,250 --> 00:28:35,300 We're making sure it's between the two. 585 00:28:35,300 --> 00:28:38,540 Then we can continue on with our happy code. 586 00:28:41,992 --> 00:28:43,360 What? 587 00:28:43,360 --> 00:28:46,860 ROB: So I figured we would just keep asking, if we happen to not have a 588 00:28:46,860 --> 00:28:48,250 lowercase letter. 589 00:28:48,250 --> 00:28:51,130 ALI: Oh, I wasn't aware of that. 590 00:28:51,130 --> 00:28:52,820 Sorry. 591 00:28:52,820 --> 00:28:58,100 >> Okay, so if we're going to do as Rob says, and make sure that-- 592 00:28:58,100 --> 00:29:02,068 we can keep asking them, then what should we do? 593 00:29:02,068 --> 00:29:02,994 SPEAKER 22: [INAUDIBLE] 594 00:29:02,994 --> 00:29:03,530 ROB: Yeah. 595 00:29:03,530 --> 00:29:05,630 We should use some sort of loop. 596 00:29:05,630 --> 00:29:12,900 Because the user can enter something invalid an untold amount of times. 597 00:29:12,900 --> 00:29:14,990 So you can use a while loop. 598 00:29:14,990 --> 00:29:18,870 The purpose of a do-while loop-- 599 00:29:18,870 --> 00:29:21,340 literally, the only time in your entire lives you will ever use do- 600 00:29:21,340 --> 00:29:24,780 while loops is when you're asking for user input. 601 00:29:24,780 --> 00:29:28,260 So the fact we're asking for user input here is a hint that we should 602 00:29:28,260 --> 00:29:29,660 use a do-while loop. 603 00:29:29,660 --> 00:29:30,780 And why is that? 604 00:29:30,780 --> 00:29:34,270 Because do-while loop always happens at least once. 605 00:29:34,270 --> 00:29:40,260 So when you're asking for user input, you want that to happen at least once. 606 00:29:40,260 --> 00:29:42,750 And then if things were successful, you can keep going. 607 00:29:42,750 --> 00:29:45,130 If not, go back and ask again. 608 00:29:45,130 --> 00:29:48,950 >> ALI: So in other words, we have to do section. 609 00:29:48,950 --> 00:29:51,130 And so this is telling it to do something. 610 00:29:51,130 --> 00:29:53,890 So printf-- 611 00:29:53,890 --> 00:29:57,490 print the statement, the prompt, and also get the character, or attempt to 612 00:29:57,490 --> 00:29:58,780 get a character. 613 00:29:58,780 --> 00:30:03,410 And then we have to check if it actually did it correctly. 614 00:30:03,410 --> 00:30:07,730 So then we add the conditions, then we say while, and then we have the 615 00:30:07,730 --> 00:30:08,980 conditional statement. 616 00:30:11,600 --> 00:30:14,730 ROB: But now we have reversed the train of thought. 617 00:30:14,730 --> 00:30:18,940 Now we originally were saying if c is in this range, it is valid. 618 00:30:18,940 --> 00:30:23,340 Now we want to reverse that and say if c is not in this range, then we 619 00:30:23,340 --> 00:30:25,020 need to back to do things again. 620 00:30:25,020 --> 00:30:25,900 ALI: Oh, yeah. 621 00:30:25,900 --> 00:30:31,720 ROB: So while this is not true, we want to go back and 622 00:30:31,720 --> 00:30:34,470 ask for another character. 623 00:30:34,470 --> 00:30:36,880 Does everyone see that? 624 00:30:36,880 --> 00:30:38,830 Questions on this? 625 00:30:38,830 --> 00:30:39,400 Okay. 626 00:30:39,400 --> 00:30:43,480 >> So now we have a valid character that we can uppercase. 627 00:30:43,480 --> 00:30:47,150 ALI: So then what the next step for uppercasing it? 628 00:30:49,994 --> 00:30:51,890 SPEAKER 23: Add 32. 629 00:30:51,890 --> 00:30:52,600 ALI: You add-- 630 00:30:52,600 --> 00:30:54,150 sorry, what? 631 00:30:54,150 --> 00:30:56,580 SPEAKER 23: Can't you just add 32? 632 00:30:56,580 --> 00:31:02,360 ROB: So yes, not in terms of magic numbers. 633 00:31:02,360 --> 00:31:04,610 You should only be using single-quote stuff. 634 00:31:04,610 --> 00:31:06,450 SPEAKER 23: Okay, well-- 635 00:31:06,450 --> 00:31:07,250 ROB: But you can get 32-- 636 00:31:07,250 --> 00:31:11,870 ALI: How did you get the number 32, I guess, is what we're saying. 637 00:31:11,870 --> 00:31:18,060 SPEAKER 23: [INAUDIBLE] 638 00:31:18,060 --> 00:31:22,158 ROB: So we can come up with the number 32 together-- 639 00:31:22,158 --> 00:31:24,468 SPEAKER 23: [INAUDIBLE]? 640 00:31:24,468 --> 00:31:24,930 ROB: Yeah. 641 00:31:24,930 --> 00:31:28,540 But if we're going to do it in the two steps we did before with 642 00:31:28,540 --> 00:31:29,950 the position stuff. 643 00:31:29,950 --> 00:31:36,910 So the position in the alphabet that c happens to be is c minus lowercase 644 00:31:36,910 --> 00:31:42,360 'a'. So if we entered a lowercase 'd', position is going to be 3. 645 00:31:42,360 --> 00:31:46,490 And now we want to take things to the uppercase range of things. 646 00:31:46,490 --> 00:31:55,376 So now our new c is going to be the capital 'A' plus position. 647 00:31:55,376 --> 00:32:00,090 So do people see how that brings us-- 648 00:32:00,090 --> 00:32:04,140 we're moving from the lowercase range, the exact depth we were into that 649 00:32:04,140 --> 00:32:07,650 range, and going down into the uppercase range, and going that far 650 00:32:07,650 --> 00:32:10,610 into it again, which is going to be the same character, but now 651 00:32:10,610 --> 00:32:12,550 uppercased. 652 00:32:12,550 --> 00:32:16,490 ALI: And for the people that were confused by how he just said 32, 653 00:32:16,490 --> 00:32:20,390 basically he combined both of these into one statement. 654 00:32:20,390 --> 00:32:24,550 So an equivalent way of writing this to say you can just kind of 655 00:32:24,550 --> 00:32:28,190 substitute position, the c minus 'a,' and put it right here. 656 00:32:28,190 --> 00:32:31,330 So what he did, I don't-- 657 00:32:31,330 --> 00:32:44,510 char newc = uppercase 'A' plus c minus lowercase 'a'. And because of algebra, 658 00:32:44,510 --> 00:32:47,090 you can move them around. 659 00:32:47,090 --> 00:32:52,960 You can also say that you can move it around so char newc = uppercase 'A' 660 00:32:52,960 --> 00:32:55,620 minus lowercase 'a' plus c. 661 00:32:55,620 --> 00:32:58,650 And this, you'll notice if we go back to the chart. 662 00:33:01,160 --> 00:33:06,300 We have uppercase A is 65 and lowercase a is 97. 663 00:33:06,300 --> 00:33:09,800 So 65 - 97 is -32. 664 00:33:09,800 --> 00:33:10,020 >> Hi. 665 00:33:10,020 --> 00:33:10,930 Take a seat. 666 00:33:10,930 --> 00:33:12,180 Oh. 667 00:33:16,240 --> 00:33:17,980 You can sit right here. 668 00:33:17,980 --> 00:33:20,805 Cool, okay, awesome. 669 00:33:20,805 --> 00:33:22,390 Welcome to section. 670 00:33:22,390 --> 00:33:25,110 You'll notice that the difference is 32. 671 00:33:25,110 --> 00:33:28,150 So that's where he got that number. 672 00:33:28,150 --> 00:33:32,140 But it's not, as Rob says, the best way to do it, because it has the weird 673 00:33:32,140 --> 00:33:35,260 magic number confusion. 674 00:33:35,260 --> 00:33:38,710 ROB: You should use capital A minus lowercase a. 675 00:33:38,710 --> 00:33:43,045 You should not use just straight 32. 676 00:33:43,045 --> 00:33:45,020 SPEAKER 24: Why is that, again? 677 00:33:45,020 --> 00:33:49,860 Why should you not use 32? 678 00:33:49,860 --> 00:33:55,630 ALI: If we did it, it would be char newc = -32 + c. 679 00:33:55,630 --> 00:34:00,030 And if you don't see any of the rest of it, if this is all gone, and you 680 00:34:00,030 --> 00:34:04,720 just see this single line, then how do you know what the negative 32 is? 681 00:34:04,720 --> 00:34:07,000 It's just out of place. 682 00:34:07,000 --> 00:34:10,360 ROB: So your program would work completely fine. 683 00:34:10,360 --> 00:34:12,960 It's just a style thing. 684 00:34:12,960 --> 00:34:17,760 Someone going in and reading your code, they're like what does 32 mean? 685 00:34:17,760 --> 00:34:18,889 Maybe they don't-- 686 00:34:18,889 --> 00:34:22,090 I probably would not immediately realize that 32 happens to be 687 00:34:22,090 --> 00:34:25,100 difference between a lowercase letter and an uppercase letter, although 688 00:34:25,100 --> 00:34:26,510 we're going to use that fact in the next problem. 689 00:34:30,210 --> 00:34:31,610 32 is-- 690 00:34:31,610 --> 00:34:38,579 you could leave a comment above it saying 32 is the difference between 691 00:34:38,579 --> 00:34:45,290 'A' and 'a.' But at that point, why not just use 'A' and 'a', and you 692 00:34:45,290 --> 00:34:47,469 don't need the comment then. 693 00:34:47,469 --> 00:34:52,739 >> ALI: It's just the ways above it are a lot cleaner, style-wise. 694 00:34:52,739 --> 00:34:54,590 And so, since you're just starting programming-- 695 00:34:54,590 --> 00:34:57,775 or I guess not, since you're hacker edi-- never mind. 696 00:34:57,775 --> 00:34:59,420 It's just a good way to have-- 697 00:34:59,420 --> 00:35:01,230 it's better to have better style. 698 00:35:01,230 --> 00:35:02,850 It's easier for other people to read. 699 00:35:02,850 --> 00:35:06,560 ROB: You should never need to remember the ASCII table, ever. 700 00:35:06,560 --> 00:35:09,505 You should just be able to use the single quote characters. 701 00:35:12,390 --> 00:35:12,730 Questions? 702 00:35:12,730 --> 00:35:13,980 ALI: Everyone good? 703 00:35:16,020 --> 00:35:16,430 ROB: Okay. 704 00:35:16,430 --> 00:35:23,320 So the next problem is substantial. 705 00:35:23,320 --> 00:35:24,660 So the next problem asks us-- 706 00:35:27,880 --> 00:35:31,570 ALI: The next problem is asking for us to do the same thing but to use 707 00:35:31,570 --> 00:35:32,640 bitwise operators. 708 00:35:32,640 --> 00:35:35,170 ROB: And of course, we have not seen bitwise operators yet. 709 00:35:35,170 --> 00:35:38,874 So we'll now discuss those. 710 00:35:38,874 --> 00:35:39,540 ALI: Be excited. 711 00:35:39,540 --> 00:35:41,490 They're super fun. 712 00:35:41,490 --> 00:35:47,927 >> ROB: So the regular operators, like x plus y, minus, times, divide. 713 00:35:47,927 --> 00:35:50,850 ALI: Type it in this prompt. 714 00:35:50,850 --> 00:35:55,420 ROB: There's also %, if you haven't seen it, which you can use by the 715 00:35:55,420 --> 00:35:57,130 percent symbol. 716 00:35:57,130 --> 00:35:59,090 But we won't use those for this problem. 717 00:35:59,090 --> 00:36:01,400 We want to use bitwise operators. 718 00:36:01,400 --> 00:36:06,250 Now remember, we brought it up in lecture one. 719 00:36:06,250 --> 00:36:10,760 I'm not sure if we've discussed binary beyond that. 720 00:36:10,760 --> 00:36:13,710 But remember that every single number is represented-- 721 00:36:13,710 --> 00:36:14,540 well, everything-- 722 00:36:14,540 --> 00:36:17,860 is represented in 1s and 0s in binary. 723 00:36:17,860 --> 00:36:27,320 So that means that when I say the number 8, I happen to know that that's 724 00:36:27,320 --> 00:36:30,240 like, 1000. 725 00:36:30,240 --> 00:36:35,530 What bitwise operators let us do is operate on these bits-- 726 00:36:35,530 --> 00:36:37,000 is operate on these bits directly. 727 00:36:40,160 --> 00:36:42,490 Now I'm no longer dealing in terms of eight. 728 00:36:42,490 --> 00:36:45,930 I'm dealing in terms of 1000, and I want to do things with those 729 00:36:45,930 --> 00:36:47,670 individual bits. 730 00:36:47,670 --> 00:36:52,520 So Ali has written the bitwise operators here, but that's-- 731 00:36:55,060 --> 00:36:58,020 The number 8 we'll use as one of our example numbers. 732 00:36:58,020 --> 00:37:02,980 And the binary representation is 1000. 733 00:37:02,980 --> 00:37:06,520 We'll use another number, 5-- 734 00:37:06,520 --> 00:37:12,070 actually let's use 9 and 5. 735 00:37:12,070 --> 00:37:15,778 And the 5's binary representation is 00-- 736 00:37:15,778 --> 00:37:18,380 0101. 737 00:37:18,380 --> 00:37:19,480 ALI: Is everyone good on that? 738 00:37:19,480 --> 00:37:21,040 The binary stuff? 739 00:37:21,040 --> 00:37:23,740 It was from the first lecture? 740 00:37:23,740 --> 00:37:29,070 >> ROB: So even if you aren't entirely on top of how to convert things to 741 00:37:29,070 --> 00:37:31,700 binary, that's not entirely important for this problem. 742 00:37:31,700 --> 00:37:36,560 We'll be using it, but you'll have many more chances to figure out how to 743 00:37:36,560 --> 00:37:39,250 quickly turn things into binary. 744 00:37:39,250 --> 00:37:43,820 So using 9 and 5, now we have our bitwise operators. 745 00:37:43,820 --> 00:37:48,620 Oh, and also, 9 and 5, if it's an integer, then really it's 32 bits, 746 00:37:48,620 --> 00:37:53,150 which means we have like 0, 0, 0, 0 a lot of times, then 747 00:37:53,150 --> 00:37:55,330 101 on the very end. 748 00:37:55,330 --> 00:37:58,530 That's just because, no matter what you do, in integers, 32 bits. 749 00:37:58,530 --> 00:38:01,570 Just because we only need four bits to represent 9 doesn't mean we're not 750 00:38:01,570 --> 00:38:05,500 using up the other 27 bits for just 0s. 751 00:38:05,500 --> 00:38:10,110 ALI: Just to clarify, one of these numbers that's a 0 or a 1 is a bit. 752 00:38:10,110 --> 00:38:11,830 This one is 4 bits. 753 00:38:11,830 --> 00:38:14,320 As so Rob said that machines store them in 32. 754 00:38:14,320 --> 00:38:19,430 So then they would have 32 of either a 0 or a 1. 755 00:38:19,430 --> 00:38:22,190 Cool? 756 00:38:22,190 --> 00:38:23,610 ROB: The bitwise operators. 757 00:38:23,610 --> 00:38:27,310 The first one that we'll deal with, let's do &. 758 00:38:27,310 --> 00:38:31,260 So if we do 9 & 5. 759 00:38:31,260 --> 00:38:38,310 So what & does is, bit by bit, it compares the bits of the two numbers 760 00:38:38,310 --> 00:38:44,860 and if both of the numbers are 1, then it will return a 1. 761 00:38:44,860 --> 00:38:50,870 If one is a 0 and the other is a 1, or both are 0s, then it returns a 0. 762 00:38:50,870 --> 00:38:53,060 So you can think of it as your logical ANDs. 763 00:38:53,060 --> 00:38:59,270 Like you need true and true to return true, but true and false is false. 764 00:38:59,270 --> 00:39:02,390 So it's the same thing, but now we're dealing with it with just bits. 765 00:39:02,390 --> 00:39:04,910 >> ALI: So if you look at this, you'll have 1-- 766 00:39:04,910 --> 00:39:08,490 you'll line them up, so it'll be 1 and 0. 767 00:39:08,490 --> 00:39:11,036 Do you guys think that would be--what would that evaluate to? 768 00:39:11,036 --> 00:39:11,770 SPEAKER 25: 1. 769 00:39:11,770 --> 00:39:12,270 ALI: Cool. 770 00:39:12,270 --> 00:39:16,850 Or no. 771 00:39:16,850 --> 00:39:18,830 Sorry. 772 00:39:18,830 --> 00:39:21,290 So does that makes sense? 773 00:39:21,290 --> 00:39:23,200 So what is the collective answer again? 774 00:39:23,200 --> 00:39:24,750 Sorry. 775 00:39:24,750 --> 00:39:27,530 So if we have 1 and 0, then what do you get? 776 00:39:27,530 --> 00:39:30,260 ROB: So you think of how you say and out loud. 777 00:39:30,260 --> 00:39:37,550 If you have two bits, x and y, you need x and y to be 1 in order for it 778 00:39:37,550 --> 00:39:40,770 to evaluate to true--or, in order for it to evaluate to 1. 779 00:39:40,770 --> 00:39:45,650 If x or y are 0, then it evaluates to false or 0. 780 00:39:45,650 --> 00:39:49,165 LUCAS: It's good to remember also that 1 is true and 0 is false. 781 00:39:49,165 --> 00:39:51,684 So if you have true & false, it's false. 782 00:39:51,684 --> 00:39:53,570 But then, true & true, true. 783 00:39:53,570 --> 00:39:55,040 False & false, false. 784 00:39:55,040 --> 00:39:57,650 ALI: We have true & false. 785 00:39:57,650 --> 00:39:58,530 So 1 and 0. 786 00:39:58,530 --> 00:40:00,380 So then again, sorry, one more time? 787 00:40:00,380 --> 00:40:02,210 SPEAKER 25: It would be 0. 788 00:40:02,210 --> 00:40:03,560 Yeah, cool. 789 00:40:03,560 --> 00:40:05,400 And then we have 0 & 1-- 790 00:40:05,400 --> 00:40:06,260 SPEAKER 25: [INAUDIBLE] 791 00:40:06,260 --> 00:40:06,680 ALI: Yeah. 792 00:40:06,680 --> 00:40:09,790 So you can always swap them in-- 793 00:40:09,790 --> 00:40:12,150 Then if you have 0 and 0? 794 00:40:12,150 --> 00:40:12,618 SPEAKER 4: 1? 795 00:40:12,618 --> 00:40:14,490 0? 796 00:40:14,490 --> 00:40:16,230 ROB: So it's 0. 797 00:40:16,230 --> 00:40:18,870 It's not that both numbers have to be the same. 798 00:40:18,870 --> 00:40:22,030 It's that both numbers need to be 1. 799 00:40:22,030 --> 00:40:26,150 ALI: So both have to be true for it to be true. 800 00:40:26,150 --> 00:40:26,950 So that's 0. 801 00:40:26,950 --> 00:40:30,540 And then you have 1 % 1, which is? 802 00:40:30,540 --> 00:40:32,640 ROB: These are pretty good numbers. 803 00:40:32,640 --> 00:40:34,362 They have all possible-- 804 00:40:34,362 --> 00:40:36,210 ALI: Good work, wow. 805 00:40:36,210 --> 00:40:37,080 Okay, cool. 806 00:40:37,080 --> 00:40:39,220 So does that make sense to everyone? 807 00:40:39,220 --> 00:40:41,770 >> ROB: So now we'll do |. 808 00:40:41,770 --> 00:40:51,650 And this is going to be very similar, but now instead of x and y needing to 809 00:40:51,650 --> 00:40:54,880 be 1 in order for it to evaluate to one, now it's just x or 810 00:40:54,880 --> 00:40:56,360 y needs to be 1. 811 00:40:56,360 --> 00:40:59,580 ALI: So 1| 0 evaluates to-- 812 00:40:59,580 --> 00:41:00,270 CLASS: 1. 813 00:41:00,270 --> 00:41:01,690 ALI: Cool. 814 00:41:01,690 --> 00:41:03,710 0 | 1 evaluates to-- 815 00:41:03,710 --> 00:41:04,420 CLASS: 1. 816 00:41:04,420 --> 00:41:06,726 ALI: Cool, and then 0 | 0-- 817 00:41:06,726 --> 00:41:07,600 CLASS: 0. 818 00:41:07,600 --> 00:41:09,320 ALI: Yeah, and then 1 | 1-- 819 00:41:09,320 --> 00:41:10,180 CLASS: 1. 820 00:41:10,180 --> 00:41:12,090 ALI: Cool. 821 00:41:12,090 --> 00:41:14,060 So that's like two bitwise operators. 822 00:41:14,060 --> 00:41:15,430 Awesome. 823 00:41:15,430 --> 00:41:16,440 ROB: So now we'll do ^. 824 00:41:16,440 --> 00:41:18,470 ALI: Should we do all of them? 825 00:41:18,470 --> 00:41:20,620 ROB: Yeah, because I think we're going to use it-- 826 00:41:20,620 --> 00:41:22,340 using them all. 827 00:41:22,340 --> 00:41:23,150 ALI: Okay. 828 00:41:23,150 --> 00:41:23,570 So-- 829 00:41:23,570 --> 00:41:25,540 ROB: I guess we don't need to. 830 00:41:25,540 --> 00:41:31,830 ALI: So ^ works in that you have to have exactly one true and one false. 831 00:41:31,830 --> 00:41:34,330 ^ means exclusive OR. 832 00:41:34,330 --> 00:41:36,650 So now it's not-- 833 00:41:36,650 --> 00:41:41,220 if x and y are both 1, it's now false. 834 00:41:41,220 --> 00:41:46,920 That is the difference between ^ and |, is that OR, you can have, if x is 835 00:41:46,920 --> 00:41:49,440 true or y is true, then we're good. 836 00:41:49,440 --> 00:41:55,176 No, ^ says if x is true, y must be false, or else it's not true. 837 00:41:55,176 --> 00:41:56,814 Do you have a question? 838 00:41:56,814 --> 00:42:00,526 >> SPEAKER 26: [INAUDIBLE] 839 00:42:00,526 --> 00:42:00,990 ALI: Yeah. 840 00:42:00,990 --> 00:42:01,830 It's kind of similar. 841 00:42:01,830 --> 00:42:07,320 ROB: Yeah, so when you get to that low-level hardware stuff, these are 842 00:42:07,320 --> 00:42:10,870 the types of operations you're dealing with. 843 00:42:10,870 --> 00:42:13,200 At the hardware level, you will only deal with bits. 844 00:42:13,200 --> 00:42:14,950 You don't deal with numbers. 845 00:42:18,030 --> 00:42:23,502 ALI: Okay, for ^, or if you have 1 ^ 0, what should that evaluate to? 846 00:42:23,502 --> 00:42:24,840 CLASS: 1. 847 00:42:24,840 --> 00:42:25,480 ALI: Cool. 848 00:42:25,480 --> 00:42:26,010 If you have 849 00:42:26,010 --> 00:42:28,370 0 ^ 1? CLASS: 1. 850 00:42:28,370 --> 00:42:29,490 ALI: Cool. 851 00:42:29,490 --> 00:42:32,050 0 ^ 0? 852 00:42:32,050 --> 00:42:32,470 Cool. 853 00:42:32,470 --> 00:42:33,460 And then 1 ^ 1? 854 00:42:33,460 --> 00:42:35,050 CLASS: 0. 855 00:42:35,050 --> 00:42:37,710 ALI: Cool, awesome. 856 00:42:37,710 --> 00:42:38,620 The next one-- 857 00:42:38,620 --> 00:42:40,520 ROB: I think these are all we have to deal with. 858 00:42:40,520 --> 00:42:41,490 We'll only do these. 859 00:42:41,490 --> 00:42:43,242 ALI: The only time we'll have to do-- 860 00:42:43,242 --> 00:42:44,912 ROB: Oh, that will be for the last problem. 861 00:42:44,912 --> 00:42:47,070 SPEAKER 27: Wait, once again? 862 00:42:47,070 --> 00:42:47,940 ALI: Sorry, what was your question? 863 00:42:47,940 --> 00:42:49,564 >> SPEAKER 27: Can you explain that once again? 864 00:42:49,564 --> 00:42:50,100 The ^? 865 00:42:50,100 --> 00:42:51,490 SPEAKER 27: Exclus-- yeah. 866 00:42:51,490 --> 00:42:55,800 ALI: So what the exclusive OR means is that there has to be exclusively 867 00:42:55,800 --> 00:43:02,970 one true and one false, so one 1 and one 0, versus with OR, you can have-- 868 00:43:02,970 --> 00:43:06,170 one of them has to be true, or both of them can be true, for it 869 00:43:06,170 --> 00:43:07,130 to evaluate to true. 870 00:43:07,130 --> 00:43:10,030 SPEAKER 27: So 0 and 0 would be false. 871 00:43:10,030 --> 00:43:10,450 ALI: Yeah. 872 00:43:10,450 --> 00:43:11,780 But if you have 1 | 873 00:43:11,780 --> 00:43:14,290 1, that would evaluate to true. 874 00:43:14,290 --> 00:43:18,210 But if you have 1 ^ 1, as we did, that evaluates to false. 875 00:43:18,210 --> 00:43:21,220 Because it's not exclusively one is true. 876 00:43:21,220 --> 00:43:26,930 ROB: And this may or may not be helpful, but notice that the ^ is 877 00:43:26,930 --> 00:43:28,890 equal to just taking the | 878 00:43:28,890 --> 00:43:30,290 minus the &. 879 00:43:30,290 --> 00:43:32,250 And you can actually think of it this way. 880 00:43:32,250 --> 00:43:37,290 ^ is just ORing everything together but taking out any of the bits where 881 00:43:37,290 --> 00:43:38,610 both were true. 882 00:43:38,610 --> 00:43:41,620 So & returns everything where both were true. 883 00:43:41,620 --> 00:43:45,850 | returns everything where one or both were true. 884 00:43:45,850 --> 00:43:51,050 So subtracting that out from the | gives you the ^. 885 00:43:51,050 --> 00:43:52,040 ALI: Any questions? 886 00:43:52,040 --> 00:43:53,290 This was a lot of information. 887 00:43:55,980 --> 00:43:57,510 Everyone good? 888 00:43:57,510 --> 00:44:00,360 >> ROB: We can go over the next ones for the next problem, slash 889 00:44:00,360 --> 00:44:01,680 what time is it? 890 00:44:01,680 --> 00:44:04,010 They don't need this until the next problem. 891 00:44:04,010 --> 00:44:07,830 ALI: I thought it was this one. 892 00:44:07,830 --> 00:44:08,830 ROB: It's not. 893 00:44:08,830 --> 00:44:10,085 ALI: Are you sure? 894 00:44:10,085 --> 00:44:11,300 ROB: Yes, I'm positive. 895 00:44:11,300 --> 00:44:12,970 ALI: Why don't we start doing the next problem? 896 00:44:15,710 --> 00:44:19,790 The problem is, again, to change it from lowercase to uppercase, and this 897 00:44:19,790 --> 00:44:21,720 time to use bitwise operators. 898 00:44:21,720 --> 00:44:22,970 ROB: So we will-- 899 00:44:25,290 --> 00:44:29,670 let's start with the binary representation of 'A', capital 'A', 900 00:44:29,670 --> 00:44:31,750 which is 65. 901 00:44:31,750 --> 00:44:36,150 So in binary-- 902 00:44:39,960 --> 00:44:47,900 so 'A' = 65, which =, in binary-- 903 00:44:47,900 --> 00:44:51,380 I'm going to mess up the number of 0s-- that. 904 00:44:51,380 --> 00:44:53,670 ALI: So does that make sense to everyone? 905 00:44:53,670 --> 00:44:57,620 So, 1, no 2s, no 4s, no 8s, no-- 906 00:45:00,238 --> 00:45:07,075 no 16s, no 32s, and then one 64. 907 00:45:07,075 --> 00:45:08,685 ROB: Yeah, I think that's one too many 0s. 908 00:45:08,685 --> 00:45:09,600 ALI: Sorry. 909 00:45:09,600 --> 00:45:13,410 >> ROB: Okay, so we have the 64 set, and we have the 1 set, and combining those 910 00:45:13,410 --> 00:45:16,030 together, we get 65. 911 00:45:16,030 --> 00:45:17,470 ALI: Cool beans? 912 00:45:17,470 --> 00:45:19,640 ROB: So now, lowercase 'a'-- 913 00:45:22,220 --> 00:45:24,260 notice 97-- 914 00:45:24,260 --> 00:45:26,370 what is the difference between 97 and 65? 915 00:45:26,370 --> 00:45:27,530 SPEAKER 28: 32. 916 00:45:27,530 --> 00:45:28,130 ROB: Yeah. 917 00:45:28,130 --> 00:45:35,920 So it's 32, which is its own bit, so that's going to be 110001. 918 00:45:35,920 --> 00:45:41,200 And this is going to be true for all possible characters. 919 00:45:41,200 --> 00:45:49,800 So if we think of 'D', which is going to be 68, that's going to be 1000011. 920 00:45:49,800 --> 00:45:56,310 And then lowercase 'd' is going to be 68 plus 32, which is 100, which is 921 00:45:56,310 --> 00:46:00,010 going to be the same thing that this was, just flipping the 922 00:46:00,010 --> 00:46:04,300 32 again, so 1100011. 923 00:46:04,300 --> 00:46:08,610 So the difference between an uppercase and lowercase letter is just flipping 924 00:46:08,610 --> 00:46:10,170 that 32-bit position. 925 00:46:10,170 --> 00:46:17,670 SPEAKER 29: Isn't it 1000100? 926 00:46:17,670 --> 00:46:19,670 ROB: Wait, it's 100-- 927 00:46:19,670 --> 00:46:20,480 something I did wrong. 928 00:46:20,480 --> 00:46:21,730 This was not true. 929 00:46:28,710 --> 00:46:31,150 I was thinking that it was a difference of 3, so then I just stuck 930 00:46:31,150 --> 00:46:33,970 the binary 3 at the end. 931 00:46:33,970 --> 00:46:36,710 This should be right. 932 00:46:36,710 --> 00:46:37,150 ALI: Thank you. 933 00:46:37,150 --> 00:46:38,080 Good for pointing that out. 934 00:46:38,080 --> 00:46:40,320 That was sharp. 935 00:46:40,320 --> 00:46:43,450 All right, is that clear to people? 936 00:46:43,450 --> 00:46:45,700 >> ROB: The difference between the uppercase and lowercase version is 937 00:46:45,700 --> 00:46:49,250 always just going to be 32, and so it's always going to be a single bit 938 00:46:49,250 --> 00:46:51,840 that needs to be flipped to change between the two. 939 00:46:51,840 --> 00:46:54,330 ALI: So in that case, knowing the difference between the capital A and 940 00:46:54,330 --> 00:46:57,130 the lowercase a is a good thing. 941 00:46:57,130 --> 00:47:01,950 Not in your code, but conceptually at least, it's good. 942 00:47:01,950 --> 00:47:05,880 So you guys can go ahead and code that in Spaces now. 943 00:47:05,880 --> 00:47:08,580 Or should we just discuss it? 944 00:47:08,580 --> 00:47:09,760 ROB: We can just discuss it. 945 00:47:09,760 --> 00:47:10,680 I don't know. 946 00:47:10,680 --> 00:47:11,680 You guys can try. 947 00:47:11,680 --> 00:47:13,560 Talk amongst yourself for a bit. 948 00:47:13,560 --> 00:47:16,390 We'll give you a chance to code it. 949 00:47:16,390 --> 00:47:17,640 [INTERPOSING VOICES] 950 00:47:19,785 --> 00:47:22,695 ALI: --is cool 951 00:47:22,695 --> 00:47:26,590 SPEAKER 30: This is like hacker comedy. 952 00:47:26,590 --> 00:47:31,060 ALI: Yeah, we're just telling the news, going through it. 953 00:47:31,060 --> 00:47:32,310 >> ROB: And we're back. 954 00:47:34,950 --> 00:47:35,405 ALI: That's kind of weird. 955 00:47:35,405 --> 00:47:38,720 I feel like we should have a CS50 news station and we could 956 00:47:38,720 --> 00:47:41,500 actually sit like this. 957 00:47:41,500 --> 00:47:43,500 ROB: Hacker News. 958 00:47:43,500 --> 00:47:46,848 ALI: Hacker News 50. 959 00:47:46,848 --> 00:47:49,210 ALI: Feel free to ask any questions if you're confused. 960 00:47:52,280 --> 00:47:53,980 ROB: Is anyone stuck? 961 00:47:53,980 --> 00:47:57,160 ALI: Yeah, that's a good thing. 962 00:47:57,160 --> 00:47:58,590 ALI: Wait, are you stuck in your chair? 963 00:47:58,590 --> 00:48:01,010 Oh. 964 00:48:01,010 --> 00:48:02,260 I was a little concerned. 965 00:48:06,670 --> 00:48:12,150 ROB: Because I just realized that we are going to want 966 00:48:12,150 --> 00:48:13,300 another bitwise operator. 967 00:48:13,300 --> 00:48:16,985 For now, let's just convert uppercase to lowercase. 968 00:48:23,280 --> 00:48:25,220 ALI: Sorry, did everyone get that announcement? 969 00:48:25,220 --> 00:48:27,755 We're going to go from uppercase to lowercase. 970 00:48:27,755 --> 00:48:30,990 ROB: Then we'll discuss lowercase to uppercase right after that. 971 00:48:33,820 --> 00:48:36,650 Oh, no. 972 00:48:36,650 --> 00:48:39,560 We have a bug in this, which, I guess, we never tried to run. 973 00:48:39,560 --> 00:48:44,140 Which is just that char c needs to be declared outside of the loop, because 974 00:48:44,140 --> 00:48:45,805 its scope is restricted in the loop. 975 00:48:49,200 --> 00:48:53,060 SPEAKER 32: In this case, is it wrong to use a number? 976 00:48:53,060 --> 00:48:56,930 ROB: Eh, you don't need to. 977 00:48:56,930 --> 00:49:02,040 If I guaranteed you that capital 'A'--or, lowercase 'a' minus capital 'A' 978 00:49:02,040 --> 00:49:09,340 was exactly one bit, but that you don't know it's 32-- 979 00:49:09,340 --> 00:49:13,160 things would work just as well if it were the 16 bit that was flipped--or 980 00:49:13,160 --> 00:49:14,610 no, I guess it wouldn't-- 981 00:49:14,610 --> 00:49:18,270 or the 64 bit were flipped, possibly. 982 00:49:18,270 --> 00:49:23,230 But you shouldn't need to use 32. 983 00:49:23,230 --> 00:49:26,141 You could instead use lowercase 'a' minus capital 'A'. 984 00:49:26,141 --> 00:49:29,340 >> SPEAKER 32: Okay. 985 00:49:29,340 --> 00:49:32,996 What you can do if you feel like you're writing too much is you can define it 986 00:49:32,996 --> 00:49:33,435 at the top. 987 00:49:33,435 --> 00:49:41,830 So you can be like, int difference = lowercase 'a' minus capital 'A'. And 988 00:49:41,830 --> 00:49:43,980 then you can type difference instead. 989 00:49:43,980 --> 00:49:45,110 That's valid. 990 00:49:45,110 --> 00:49:48,002 But just using the number 32 is kind of-- 991 00:49:48,002 --> 00:49:51,104 SPEAKER 33: Even though it's because of the fact that it's the 32 bit? 992 00:49:51,104 --> 00:49:52,570 ALI: Yeah. 993 00:49:52,570 --> 00:49:54,960 ROB: There could be-- 994 00:49:54,960 --> 00:49:57,780 the only guarantee we need to make you is that the difference between the 995 00:49:57,780 --> 00:49:59,480 lowercase and uppercase is one bit. 996 00:49:59,480 --> 00:50:04,670 It shouldn't matter that it happens to be the 32 bit that's flipped. 997 00:50:04,670 --> 00:50:06,610 For time considerations, we'll start going over this. 998 00:50:09,170 --> 00:50:10,370 It's okay if you haven't finished. 999 00:50:10,370 --> 00:50:13,090 Don't worry about it. 1000 00:50:13,090 --> 00:50:16,260 ROB: Assuming c is a capital letter-- 1001 00:50:16,260 --> 00:50:18,030 we don't have to assume c is a capital letter. 1002 00:50:18,030 --> 00:50:20,750 We know c is a capital letter after this while loop-- 1003 00:50:20,750 --> 00:50:22,160 do-while loop. 1004 00:50:22,160 --> 00:50:28,590 So that means it looks something like this 'A'. So if we want to bring this 1005 00:50:28,590 --> 00:50:32,895 'A' to this 'a', what do we need to do? 1006 00:50:36,890 --> 00:50:41,920 So how do we convert and flip the bit? 1007 00:50:48,040 --> 00:50:49,450 ALI: How do we flip it? 1008 00:50:49,450 --> 00:50:53,010 >> ROB: So we know we want to flip the 32 bit. 1009 00:50:53,010 --> 00:50:59,722 So that will do 'a' minus 'A', or we could write 32, but-- 1010 00:50:59,722 --> 00:51:03,090 darn it, it's the other way around. 1011 00:51:03,090 --> 00:51:07,250 ALI: Okay so Rob here's calculating that number, magic number 32. 1012 00:51:07,250 --> 00:51:12,000 So he's finding out where that bit flip area would be. 1013 00:51:12,000 --> 00:51:14,810 And then what do we put where the question marks are? 1014 00:51:14,810 --> 00:51:16,740 ROB: How do we flip that bit? 1015 00:51:16,740 --> 00:51:22,784 In terms of the uppercase letter, it is a 0, and we want to become a 1. 1016 00:51:22,784 --> 00:51:23,660 SPEAKER 34: |. 1017 00:51:23,660 --> 00:51:24,910 ROB: Yeah. 1018 00:51:28,780 --> 00:51:38,190 So this is us saying we take 'A'. We're ORing it with 0-- 1019 00:51:38,190 --> 00:51:40,750 1000-- 1020 00:51:40,750 --> 00:51:44,720 and that = that. 1021 00:51:44,720 --> 00:51:45,930 ALI: Does that make sense to everyone? 1022 00:51:45,930 --> 00:51:48,812 We can go through it bit by bit if you want. 1023 00:51:52,030 --> 00:51:56,300 SPEAKER 35: Are you [INAUDIBLE]? 1024 00:51:56,300 --> 00:51:57,590 ALI: Where's the minus sign? 1025 00:51:57,590 --> 00:51:58,500 ROB: Oh, you mean in here? 1026 00:51:58,500 --> 00:51:59,070 ALI: Oh, yeah. 1027 00:51:59,070 --> 00:52:00,140 ROB: So I guess this is us. 1028 00:52:00,140 --> 00:52:05,670 We could say 32, and now we're just using bitwise operators. 1029 00:52:05,670 --> 00:52:07,010 ALI: We don't like magic numbers. 1030 00:52:07,010 --> 00:52:10,460 We're not going to do that. 1031 00:52:10,460 --> 00:52:13,592 Also I think we should just use a bitwise operator-- 1032 00:52:13,592 --> 00:52:19,042 SPEAKER 36: If you use a ^ A, will you get 32? 1033 00:52:19,042 --> 00:52:19,980 SPEAKER 37: Yeah, I think-- 1034 00:52:19,980 --> 00:52:21,090 ROB: Yeah you will. 1035 00:52:21,090 --> 00:52:23,320 ALI: This is how you would have done it for going 1036 00:52:23,320 --> 00:52:25,370 from capital to lowercase. 1037 00:52:25,370 --> 00:52:31,130 >> Now can anyone tell me why Rob and I changed the prompt and how we went 1038 00:52:31,130 --> 00:52:34,040 from uppercase lowercase, instead of lowercase to 1039 00:52:34,040 --> 00:52:37,810 uppercase as we did before? 1040 00:52:37,810 --> 00:52:44,520 Does anyone have any ideas why that might be more difficult? 1041 00:52:44,520 --> 00:52:44,880 ROB: So we'd do-- 1042 00:52:44,880 --> 00:52:46,178 ALI: Yeah, go ahead. 1043 00:52:46,178 --> 00:52:49,640 SPEAKER 38: The | operator was only really good for adding. 1044 00:52:49,640 --> 00:52:51,200 ALI: Yeah, exactly. 1045 00:52:51,200 --> 00:52:58,320 ROB: So what we want to do in the lowercase to uppercase case is, 1046 00:52:58,320 --> 00:53:00,175 convert this 'a'-- 1047 00:53:08,540 --> 00:53:14,820 so we want to put in some numbers here and use some operator so that it comes 1048 00:53:14,820 --> 00:53:21,910 out to capital A. We're still probably using 32 somehow, but now how do we 1049 00:53:21,910 --> 00:53:25,280 get this to come out to this? 1050 00:53:40,910 --> 00:53:42,750 So c-- 1051 00:53:42,750 --> 00:53:44,000 oh, I still want that too-- 1052 00:53:46,940 --> 00:53:51,055 ALI: So now if we're converting from lowercase to uppercase again, then we 1053 00:53:51,055 --> 00:53:55,170 do the ^ operation, as you guys said. 1054 00:53:55,170 --> 00:53:57,650 ROB: There are other ways you can do things. 1055 00:53:57,650 --> 00:54:01,680 A lot of the bitwise operators end up being very easily definable in terms 1056 00:54:01,680 --> 00:54:03,460 of other bitwise operators. 1057 00:54:03,460 --> 00:54:11,150 So I was going to not use ^ or at all, but ^ worked perfectly fine. 1058 00:54:11,150 --> 00:54:17,910 >> ALI: Okay, so we're going to go at the last problem, which is to basic-- 1059 00:54:17,910 --> 00:54:22,390 the last problem is to convert something into binary. 1060 00:54:22,390 --> 00:54:26,350 So, for instance, you have 50, and then you convert it to 1061 00:54:26,350 --> 00:54:31,400 binary, which is 110010. 1062 00:54:31,400 --> 00:54:36,010 And I think you guys all know about binary from lecture. 1063 00:54:36,010 --> 00:54:39,270 And you also are not supposed to put any leading 0s into it. 1064 00:54:39,270 --> 00:54:41,460 ROB: You can ignore that part for now. 1065 00:54:41,460 --> 00:54:45,250 The first pass, you can do it so that there are leading 0s. 1066 00:54:45,250 --> 00:54:49,200 And then we can modify that to have no leading 0s, if we have time. 1067 00:54:49,200 --> 00:54:50,630 ALI: So don't worry about that. 1068 00:54:50,630 --> 00:54:52,620 Just try to get in binary. 1069 00:54:55,880 --> 00:54:59,855 A good place to start would be-- 1070 00:54:59,855 --> 00:55:02,010 ROB: We need to ask for an integer. 1071 00:55:02,010 --> 00:55:03,150 ALI: Oh yeah, that's a good one. 1072 00:55:03,150 --> 00:55:04,170 We can start with that. 1073 00:55:04,170 --> 00:55:06,870 So you can change the prompt. 1074 00:55:06,870 --> 00:55:08,840 So instead of a character, now we're dealing with integers. 1075 00:55:13,670 --> 00:55:14,920 Cool? 1076 00:55:20,630 --> 00:55:25,300 >> ROB: And now what is our do-while condition going to be? 1077 00:55:25,300 --> 00:55:27,750 It's asking for a non-negative integer. 1078 00:55:31,080 --> 00:55:34,280 ALI: When do we want to keep prompting the user for something else? 1079 00:55:34,280 --> 00:55:35,950 When it's smaller than 0. 1080 00:55:35,950 --> 00:55:36,760 ALI: Right. 1081 00:55:36,760 --> 00:55:38,010 ROB: While i is less than 0. 1082 00:55:41,090 --> 00:55:45,120 ALI: Now we want to convert it. 1083 00:55:45,120 --> 00:55:48,150 Do you guys have any ideas of what to do? 1084 00:55:48,150 --> 00:55:49,400 Any inklings? 1085 00:55:53,610 --> 00:55:54,984 Yeah. 1086 00:55:54,984 --> 00:55:57,344 SPEAKER 39: Do we have to convert it, or can we just print some 0s and 1s 1087 00:55:57,344 --> 00:55:58,760 that would be the right number? 1088 00:55:58,760 --> 00:56:02,070 Could you do a for loop and count out by powers of 2? 1089 00:56:02,070 --> 00:56:03,320 ROB: Yes. 1090 00:56:05,750 --> 00:56:08,425 You don't need to convert i to its binary-- 1091 00:56:08,425 --> 00:56:13,720 the binary representation of i will not fit inside of i. 1092 00:56:13,720 --> 00:56:19,090 You just need to print the binary representation of i. 1093 00:56:19,090 --> 00:56:21,010 ALI: In other words, you do not need to go by powers of two. 1094 00:56:21,010 --> 00:56:23,710 ROB: Because i is already being stored as binaries, so you don't need to 1095 00:56:23,710 --> 00:56:25,110 convert it to binary. 1096 00:56:25,110 --> 00:56:27,260 Just go over its binary, and print it. 1097 00:56:30,080 --> 00:56:32,140 And you're going to need to use bitwise, because you want to look at 1098 00:56:32,140 --> 00:56:33,390 the actual bits. 1099 00:56:37,060 --> 00:56:38,600 You don't need to use bitwise, but please do. 1100 00:56:44,130 --> 00:56:47,160 And that's actually-- 1101 00:56:47,160 --> 00:56:50,200 you can just do powers of two by multiplying by 2. 1102 00:56:50,200 --> 00:56:55,120 We'll go over one of the last two binary bitwise operators, 1103 00:56:55,120 --> 00:56:56,410 which is this one. 1104 00:56:56,410 --> 00:56:58,730 So when you take-- 1105 00:56:58,730 --> 00:57:00,083 >> ALI: Can you go through both of them? 1106 00:57:00,083 --> 00:57:01,440 ROB: Yeah. 1107 00:57:01,440 --> 00:57:03,050 So let's go with 9. 1108 00:57:03,050 --> 00:57:05,210 So 9 left shift 1-- 1109 00:57:05,210 --> 00:57:12,220 what left shift means is just move all the bits to the left and insert a 0. 1110 00:57:12,220 --> 00:57:15,530 So we're moving all the bits of 9 to the left one 1111 00:57:15,530 --> 00:57:20,240 position, which gives us-- 1112 00:57:26,530 --> 00:57:31,570 So notice we have shifted everything left one, and inserted a 0 on the 1113 00:57:31,570 --> 00:57:32,730 right side. 1114 00:57:32,730 --> 00:57:36,755 If we had shifted left by two, we would shift everything left by two, 1115 00:57:36,755 --> 00:57:41,950 and insert two 0s to fill in the gaps. 1116 00:57:41,950 --> 00:57:43,200 Question? 1117 00:57:44,890 --> 00:57:47,510 ALI: So then right shift does a similar thing. 1118 00:57:47,510 --> 00:57:53,190 If you have 9 right shift 1 then you're just moving everything over to 1119 00:57:53,190 --> 00:57:57,890 the right, you lose a digit, so 100 instead. 1120 00:57:57,890 --> 00:58:04,142 ROB: So you it's fine that you are losing the bit that happened to be all 1121 00:58:04,142 --> 00:58:05,200 the way to the right. 1122 00:58:05,200 --> 00:58:08,850 There are different types of right shifts, but basically you can think of 1123 00:58:08,850 --> 00:58:11,410 right shift as just dividing by 2. 1124 00:58:11,410 --> 00:58:14,430 And you can think of left shift as multiplying by 2. 1125 00:58:14,430 --> 00:58:16,490 So if you converted this-- 1126 00:58:16,490 --> 00:58:22,350 if you converted 9 shift 1, which is equal to 10010, if you actually went 1127 00:58:22,350 --> 00:58:25,100 through the binary and figured out what that was, it would just be 18. 1128 00:58:25,100 --> 00:58:26,880 We just multiply by 2. 1129 00:58:26,880 --> 00:58:30,982 1001 left shift 2 is going to be 36. 1130 00:58:30,982 --> 00:58:32,400 We multiply by 4. 1131 00:58:41,740 --> 00:58:45,360 Okay, cool, so that's a handy bitwise operator for this one. 1132 00:58:52,570 --> 00:58:53,330 More confusion? 1133 00:58:53,330 --> 00:58:54,870 Do you guys want to try it out? 1134 00:58:54,870 --> 00:58:58,970 Or we can just jump right in and start it? 1135 00:58:58,970 --> 00:59:01,140 ROB: We should probably jump right in. 1136 00:59:01,140 --> 00:59:01,880 ALI: Yeah, Okay. 1137 00:59:01,880 --> 00:59:03,370 ROB: And walk through it together. 1138 00:59:03,370 --> 00:59:04,440 >> ALI: Well I'll just go into it. 1139 00:59:04,440 --> 00:59:09,700 So as he said in the beginning, we're going to just go through bit by bit. 1140 00:59:09,700 --> 00:59:11,890 So we're going to need a loop. 1141 00:59:11,890 --> 00:59:16,730 Does anyone have an idea of the best kind of loop to go through this? 1142 00:59:16,730 --> 00:59:20,544 We have while loops, do-while loops, for loops. 1143 00:59:20,544 --> 00:59:22,400 SPEAKER 40: Do-while loop? 1144 00:59:22,400 --> 00:59:25,150 ROB: So remember what I said before about, we are only using do-while 1145 00:59:25,150 --> 00:59:27,390 loops to get user input. 1146 00:59:27,390 --> 00:59:30,890 You will never pretty much ever use them for anything else. 1147 00:59:30,890 --> 00:59:33,660 ALI: The problem with do-while loops is it does something before it checks for 1148 00:59:33,660 --> 00:59:34,470 the condition. 1149 00:59:34,470 --> 00:59:36,540 So, it could do something bad-- 1150 00:59:36,540 --> 00:59:38,640 like, hurt-- 1151 00:59:38,640 --> 00:59:40,350 do something that's not possible. 1152 00:59:40,350 --> 00:59:42,290 So you want to check first. 1153 00:59:42,290 --> 00:59:47,620 ROB: There's a lot of cases where you can use do-while loops, but people 1154 00:59:47,620 --> 00:59:52,120 don't expect you to use them unless it's user input. 1155 00:59:52,120 --> 00:59:54,215 So we have used our do-while loop to get i. 1156 00:59:57,160 --> 01:00:01,260 How many times is our loop going to want to loop? 1157 01:00:01,260 --> 01:00:04,060 We want to loop over each of the bits of i. 1158 01:00:04,060 --> 01:00:06,302 How many bits are in i? 1159 01:00:06,302 --> 01:00:07,266 SPEAKER 41: 32. 1160 01:00:07,266 --> 01:00:07,750 ALI: Yeah. 1161 01:00:07,750 --> 01:00:09,850 ROB: So there are 32 bits. 1162 01:00:09,850 --> 01:00:12,920 We don't want to say 32. 1163 01:00:12,920 --> 01:00:16,230 We instead want to say sizeof-- 1164 01:00:16,230 --> 01:00:18,226 which, have you seen that in class yet? 1165 01:00:18,226 --> 01:00:19,100 ALI: Nope. 1166 01:00:19,100 --> 01:00:20,380 ROB: Why does it say to use that-- 1167 01:00:20,380 --> 01:00:22,160 sizeof(int) times 8-- 1168 01:00:22,160 --> 01:00:23,530 ALI: So, sizeof is-- 1169 01:00:23,530 --> 01:00:27,050 >> SPEAKER 42: [INAUDIBLE] 1170 01:00:27,050 --> 01:00:27,440 ALI: Interesting. 1171 01:00:27,440 --> 01:00:31,610 So, sizeof is a built-in C function where it just tells you the 1172 01:00:31,610 --> 01:00:32,500 number of bits in-- 1173 01:00:32,500 --> 01:00:33,220 ROB: Bytes. 1174 01:00:33,220 --> 01:00:34,210 ALI: Bytes, sorry-- 1175 01:00:34,210 --> 01:00:39,990 the number of bytes that a variable has to store a value. 1176 01:00:39,990 --> 01:00:46,205 So I think he went over in class, then, an int is 4 bytes maybe. 1177 01:00:46,205 --> 01:00:46,610 Cool? 1178 01:00:46,610 --> 01:00:50,610 ROB: But you shouldn't need to know that an int is 4 bytes. 1179 01:00:50,610 --> 01:00:52,670 You should be able to say-- 1180 01:00:52,670 --> 01:00:55,870 you should use sizeof(int) to get 4 bytes. 1181 01:00:55,870 --> 01:00:59,170 And that's actually important for some things. 1182 01:00:59,170 --> 01:01:01,650 An int might not be 4 bytes. 1183 01:01:01,650 --> 01:01:04,050 On another computer, an int might be 8 bytes. 1184 01:01:04,050 --> 01:01:08,500 So your program won't work on that computer because you hard-coded 32 1185 01:01:08,500 --> 01:01:12,230 bits when on that computer happens to be 64 bits. 1186 01:01:12,230 --> 01:01:15,260 ALI: So you have the total number of bits that you want it to reverse. 1187 01:01:15,260 --> 01:01:17,480 ROB: And the difference between a byte and a bit is that there 1188 01:01:17,480 --> 01:01:19,100 are 8 bits in a byte. 1189 01:01:19,100 --> 01:01:22,110 So sizeof(int) returns 4 bytes, then we have to multiply that 1190 01:01:22,110 --> 01:01:24,420 by 8 to get 32 bits. 1191 01:01:24,420 --> 01:01:27,680 >> ALI: Okay, so we know the maximum and we know we want to go from-- 1192 01:01:27,680 --> 01:01:29,260 we want to go through all of them. 1193 01:01:29,260 --> 01:01:32,950 So we want to go from 0 to numBits. 1194 01:01:32,950 --> 01:01:37,160 So does anyone know how to write the loop for that? 1195 01:01:37,160 --> 01:01:37,790 Anyone? 1196 01:01:37,790 --> 01:01:39,680 SPEAKER 43: [INAUDIBLE] 1197 01:01:39,680 --> 01:01:44,130 ALI: So you want to traverse the number i from the zeroth bit to the 1198 01:01:44,130 --> 01:01:47,840 first bit, the second bit, the third bit, all the way to numBits. 1199 01:01:47,840 --> 01:01:50,972 The max number of bits that you can get to. 1200 01:01:54,420 --> 01:01:57,780 ROB: What's the common loop we use what we want to do something x times? 1201 01:01:57,780 --> 01:01:58,280 ALI: For. 1202 01:01:58,280 --> 01:01:59,280 Perfect. 1203 01:01:59,280 --> 01:02:00,440 So we have for. 1204 01:02:00,440 --> 01:02:05,242 And then we'll create an iterator, which is not going to be i. 1205 01:02:05,242 --> 01:02:06,850 ROB: j. 1206 01:02:06,850 --> 01:02:08,080 ALI: J = 0. 1207 01:02:08,080 --> 01:02:09,740 So we'll start at 0. 1208 01:02:09,740 --> 01:02:13,110 And then we're going to have the condition to make sure, while you're 1209 01:02:13,110 --> 01:02:14,968 going through it every single time-- 1210 01:02:14,968 --> 01:02:16,960 [INTERPOSING VOICES] 1211 01:02:16,960 --> 01:02:17,960 ROB: j. 1212 01:02:17,960 --> 01:02:19,970 ALI: j is less than numBits. 1213 01:02:19,970 --> 01:02:23,680 So we don't want to do the actual equals one, because they're numBits 1214 01:02:23,680 --> 01:02:25,365 total, and we start at 0. 1215 01:02:25,365 --> 01:02:26,360 So we're actually going to have-- 1216 01:02:26,360 --> 01:02:27,400 SPEAKER 44: Oh. 1217 01:02:27,400 --> 01:02:29,300 ALI: The indexes don't end at numBits minus 1. 1218 01:02:29,300 --> 01:02:34,070 ROB: We label the bits 0 to 31, we don't label them 1 to 32. 1219 01:02:34,070 --> 01:02:36,970 ALI: And then we're going to increment each time by one, because we want to 1220 01:02:36,970 --> 01:02:38,050 check every single one. 1221 01:02:38,050 --> 01:02:39,851 So we'll go j++. 1222 01:02:39,851 --> 01:02:42,740 >> SPEAKER 45: Why are you doing that? 1223 01:02:42,740 --> 01:02:46,100 SPEAKER 46: Why are you going through every bit? 1224 01:02:46,100 --> 01:02:49,310 ROB: So we want to go through every bit and-- 1225 01:02:52,780 --> 01:02:55,100 So let's say we have some number. 1226 01:02:55,100 --> 01:02:56,195 ALI: 68. 1227 01:02:56,195 --> 01:02:57,470 ROB: We can use 68. 1228 01:02:57,470 --> 01:03:00,450 So the binary's going to be 100100. 1229 01:03:00,450 --> 01:03:05,540 So this is inside of i and we want to go over each of these bits. 1230 01:03:05,540 --> 01:03:06,450 Is it a 1? 1231 01:03:06,450 --> 01:03:07,320 Print 1. 1232 01:03:07,320 --> 01:03:08,120 Is it a 0? 1233 01:03:08,120 --> 01:03:09,100 Print 0. 1234 01:03:09,100 --> 01:03:10,530 Then we go on to the next bit. 1235 01:03:10,530 --> 01:03:11,210 Is it a 1? 1236 01:03:11,210 --> 01:03:11,610 Print 1. 1237 01:03:11,610 --> 01:03:11,830 Is a 0? 1238 01:03:11,830 --> 01:03:13,080 Print 0. 1239 01:03:14,880 --> 01:03:16,710 ALI: Cool, okay. 1240 01:03:16,710 --> 01:03:22,880 Now we're at the correct index for where that bit is. 1241 01:03:25,380 --> 01:03:28,080 How can we check that specific one? 1242 01:03:28,080 --> 01:03:33,130 Let's say we're at j = 0, so we want to check the 0 index of the bit. 1243 01:03:33,130 --> 01:03:37,980 So, in this case, this one. 1244 01:03:37,980 --> 01:03:41,930 >> So now how do we go about check--oh, it would be this one. 1245 01:03:41,930 --> 01:03:42,810 ROB: Yeah. 1246 01:03:42,810 --> 01:03:44,310 ALI: Sorry. 1247 01:03:44,310 --> 01:03:47,930 How would we go about checking that one, in a sense? 1248 01:03:47,930 --> 01:03:52,030 How would we check if it's a 0 or a 1? 1249 01:03:52,030 --> 01:03:52,730 Well let's-- 1250 01:03:52,730 --> 01:03:53,940 CLASS: And 1? 1251 01:03:53,940 --> 01:03:54,390 ALI: And? 1252 01:03:54,390 --> 01:03:55,010 Yeah. 1253 01:03:55,010 --> 01:03:57,810 ROB: So let's say we already have 1. 1254 01:03:57,810 --> 01:04:02,470 Then this &, this will return whether it was a 1 or a 0. 1255 01:04:05,580 --> 01:04:08,200 ALI: So we can go down. 1256 01:04:08,200 --> 01:04:10,695 And we can do as you guys said, of doing the & function. 1257 01:04:13,810 --> 01:04:15,560 ROB: int-- 1258 01:04:15,560 --> 01:04:18,290 how do I want to say this-- 1259 01:04:18,290 --> 01:04:24,250 isOne = i. 1260 01:04:24,250 --> 01:04:29,770 And for the first bit we happen to be looking at, we're doing i & 1. 1261 01:04:29,770 --> 01:04:33,302 Now what are we going to want to & by for the second bit? 1262 01:04:33,302 --> 01:04:34,294 SPEAKER 47: 2. 1263 01:04:34,294 --> 01:04:36,110 ROB: Yeah, the second that we're on 2. 1264 01:04:36,110 --> 01:04:39,100 ALI: You'll notice that the 1 has moved over to the left one. 1265 01:04:39,100 --> 01:04:44,320 So have we learned any bitwise operators to do that function? 1266 01:04:44,320 --> 01:04:45,160 ROB: The next one-- 1267 01:04:45,160 --> 01:04:46,280 ALI: Yeah, right, exactly. 1268 01:04:46,280 --> 01:04:48,670 So it's a left shift. 1269 01:04:48,670 --> 01:04:51,120 Instead of always checking with 1, we're going to shift it. 1270 01:04:51,120 --> 01:04:53,470 And how many times are we going to shift it? 1271 01:04:53,470 --> 01:04:59,340 If we're checking the zeroth one then we're going to move it over zero. 1272 01:04:59,340 --> 01:04:59,975 Yeah, exactly. 1273 01:04:59,975 --> 01:05:02,090 So you left shift it by j. 1274 01:05:02,090 --> 01:05:02,550 Perfect. 1275 01:05:02,550 --> 01:05:03,550 So now we know-- 1276 01:05:03,550 --> 01:05:06,450 we have this int of if it's 1, and then-- 1277 01:05:10,690 --> 01:05:14,670 so Rob's kind of cheating here-- 1278 01:05:14,670 --> 01:05:18,090 and so now he was going ahead and he's just saying that if it actually is 1, 1279 01:05:18,090 --> 01:05:23,540 because 1 evaluates to true, so we don't have to say isf isOne equals 1-- 1280 01:05:23,540 --> 01:05:29,210 so if isOne, then we print 1, and otherwise we print 0. 1281 01:05:29,210 --> 01:05:33,350 >> ROB: So our program has an overall bug. 1282 01:05:33,350 --> 01:05:35,845 Well, questions on this first. 1283 01:05:35,845 --> 01:05:41,692 SPEAKER 48: Can you ever put the isOne inside the four operation part and 1284 01:05:41,692 --> 01:05:47,804 then have it shift itself as the last part? 1285 01:05:47,804 --> 01:05:50,050 So isOne equals 1 and then-- 1286 01:05:50,050 --> 01:05:53,140 ROB: Yes, you can. 1287 01:05:53,140 --> 01:05:57,200 We can actually show that once we fix the bug that I'm about to 1288 01:05:57,200 --> 01:06:00,050 discuss with you all. 1289 01:06:00,050 --> 01:06:03,470 Notice the order we're going over things. 1290 01:06:03,470 --> 01:06:10,570 Given this binary representation, we are starting with the zeroth bit. 1291 01:06:10,570 --> 01:06:13,010 If it is a 0, then we are printing-- 1292 01:06:13,010 --> 01:06:15,620 well, it is a 0, so we are printing 0. 1293 01:06:15,620 --> 01:06:16,860 Then we go to the second bit. 1294 01:06:16,860 --> 01:06:19,100 It is a 0, so we print 0. 1295 01:06:19,100 --> 01:06:20,290 Then we go to the third bit. 1296 01:06:20,290 --> 01:06:22,950 It is a 1, so we print 1. 1297 01:06:22,950 --> 01:06:24,580 ALI: So it's going backwards. 1298 01:06:24,580 --> 01:06:27,906 So how do you guys suggest that we fix that and go the other way? 1299 01:06:27,906 --> 01:06:28,900 SPEAKER 49: [INAUDIBLE] 1300 01:06:28,900 --> 01:06:29,490 ROB: Yeah. 1301 01:06:29,490 --> 01:06:34,500 So instead of going from 0 to numBits, we'll go from numBits bits to 0. 1302 01:06:34,500 --> 01:06:35,600 ALI: So [INAUDIBLE] 1303 01:06:35,600 --> 01:06:36,940 can always go the other way. 1304 01:06:36,940 --> 01:06:38,970 ROB: NumBits minus 1, because that's the last nubmer-- 1305 01:06:38,970 --> 01:06:41,160 ALI: Because it goes from 0 to 31. 1306 01:06:41,160 --> 01:06:44,420 And then we'll always make sure that it's greater than 0 so you don't go 1307 01:06:44,420 --> 01:06:46,100 too far to the right. 1308 01:06:46,100 --> 01:06:48,800 And then you'll always subtract by 1 to go to the right. 1309 01:06:48,800 --> 01:06:52,240 >> ROB: So now we have a reverse loop, and the number is back into printing 1310 01:06:52,240 --> 01:06:53,586 in the correct way. 1311 01:06:53,586 --> 01:06:57,560 SPEAKER 50: Don't you have to change the shift thing too? 1312 01:06:57,560 --> 01:06:58,810 ALI: So no. 1313 01:07:03,810 --> 01:07:06,470 ROB: We're doing the same exact operations, except now 1314 01:07:06,470 --> 01:07:07,470 in the reverse order. 1315 01:07:07,470 --> 01:07:14,170 So instead of doing the shifting by numBits minus 1 to get whatever this 1316 01:07:14,170 --> 01:07:17,430 bit is, instead of doing that last, we'll just do it first. 1317 01:07:19,970 --> 01:07:22,970 By reversing this up here, we reverse the order of everything 1318 01:07:22,970 --> 01:07:24,190 that happens inside. 1319 01:07:24,190 --> 01:07:28,610 But we don't want to change what happens inside at all. 1320 01:07:28,610 --> 01:07:29,860 ALI: Does that make sense? 1321 01:07:32,240 --> 01:07:33,660 Processing? 1322 01:07:33,660 --> 01:07:41,200 Okay, so does anyone else have any other conceptual questions? 1323 01:07:41,200 --> 01:07:45,030 ALI: Before we go into his point which is-- 1324 01:07:45,030 --> 01:07:46,280 ALI: All right, well go. 1325 01:07:48,350 --> 01:07:53,160 ROB: Instead of using j to be our-- 1326 01:07:55,780 --> 01:08:00,550 instead of using j to always shift by, like one left shift j, I think what 1327 01:08:00,550 --> 01:08:04,120 you're saying is like, to j use itself. 1328 01:08:04,120 --> 01:08:09,560 So for j-- 1329 01:08:09,560 --> 01:08:14,656 I guess this is the hard--j = 1 << 31. 1330 01:08:14,656 --> 01:08:16,200 Is that what I want? 1331 01:08:16,200 --> 01:08:18,470 I think it's << 31. 1332 01:08:18,470 --> 01:08:22,840 j >= 1. 1333 01:08:22,840 --> 01:08:32,319 j >>, >> isOne, i & j-- 1334 01:08:32,319 --> 01:08:35,620 so now instead of using-- 1335 01:08:35,620 --> 01:08:38,370 ALI: You're constantly moving j instead of having an 1336 01:08:38,370 --> 01:08:40,520 index to check it. 1337 01:08:40,520 --> 01:08:42,130 It's not that bad. 1338 01:08:42,130 --> 01:08:43,960 We're using a similar kind of logic. 1339 01:08:43,960 --> 01:08:46,202 So if you go back-- 1340 01:08:46,202 --> 01:08:50,020 oh, but it's going to lose it. 1341 01:08:50,020 --> 01:08:51,380 Okay, you're just going to type it again. 1342 01:08:51,380 --> 01:08:56,880 If you notice that we have 1 << j amount of times, and we were 1343 01:08:56,880 --> 01:09:02,279 incrementing j every single time by one increment, but this time instead 1344 01:09:02,279 --> 01:09:06,010 they're doing the increment of j at the top inside the for loop. 1345 01:09:06,010 --> 01:09:10,890 So instead of always moving it down-- 1346 01:09:10,890 --> 01:09:15,220 ROB: The very first iteration of the for loop, we have j is 10000. 1347 01:09:15,220 --> 01:09:20,000 The next iteration of the for loop is 01000. 1348 01:09:20,000 --> 01:09:22,380 The next iteration is that. 1349 01:09:22,380 --> 01:09:26,800 And the 1 is just going to keep propagating down until we hit the very 1350 01:09:26,800 --> 01:09:29,729 end, where now it's this. 1351 01:09:29,729 --> 01:09:33,990 The next one, the 1 is going to be pushed off, and j is no longer greater 1352 01:09:33,990 --> 01:09:35,090 than or equal to 1. 1353 01:09:35,090 --> 01:09:38,380 I could also put greater than 0, same difference. 1354 01:09:38,380 --> 01:09:41,580 And then that's it. 1355 01:09:41,580 --> 01:09:43,720 Questions? 1356 01:09:43,720 --> 01:09:44,760 Yeah. 1357 01:09:44,760 --> 01:09:49,740 >> SPEAKER 51: [INAUDIBLE] 1358 01:09:49,740 --> 01:09:51,729 ROB: So that's the same as-- 1359 01:09:51,729 --> 01:09:57,270 have you seen j += 3? 1360 01:09:57,270 --> 01:10:00,960 So this is the same as j = j + 3? 1361 01:10:00,960 --> 01:10:05,560 You can do that for virtually any operator in C. So j >>= 3 is 1362 01:10:05,560 --> 01:10:10,490 equal to j = j >> 3. 1363 01:10:13,980 --> 01:10:16,760 So that works also with bitwise operators. 1364 01:10:16,760 --> 01:10:19,630 And I should not have put 31 here. 1365 01:10:19,630 --> 01:10:23,790 I should have put numBits - 1. 1366 01:10:23,790 --> 01:10:25,380 ALI: Yay, no magic numbers. 1367 01:10:25,380 --> 01:10:29,070 Okay, it's 4:00 though, so if you guys have to go--but does this make sense? 1368 01:10:29,070 --> 01:10:30,320 Do you guys have any other questions? 1369 01:10:33,920 --> 01:10:38,470 ROB: I was also going to throw in that if we wanted-- 1370 01:10:38,470 --> 01:10:41,326 has he discussed ternary operator at all? 1371 01:10:41,326 --> 01:10:42,730 ALI: No. 1372 01:10:42,730 --> 01:10:45,090 It's okay. 1373 01:10:45,090 --> 01:10:46,970 ROB: So you could look into it for next time. 1374 01:10:51,030 --> 01:10:52,080 ALI: Okay, really? 1375 01:10:52,080 --> 01:10:53,150 Now you're just showing off. 1376 01:10:53,150 --> 01:10:54,210 ROB: No, now it's-- 1377 01:10:54,210 --> 01:11:00,490 this is what gets people interested, and now they'll go look into it. 1378 01:11:00,490 --> 01:11:02,640 So now, that does the same thing, in one step. 1379 01:11:05,150 --> 01:11:10,950 So first check, is i & j 1? 1380 01:11:10,950 --> 01:11:12,530 Is it true? 1381 01:11:12,530 --> 01:11:17,000 If so, print a 1, else, print a 0. 1382 01:11:17,000 --> 01:11:19,190 ALI: But doing it the other way is perfectly fine as well. 1383 01:11:19,190 --> 01:11:20,920 It has the same logic. 1384 01:11:20,920 --> 01:11:22,730 So it's not like one is-- 1385 01:11:22,730 --> 01:11:24,620 ROB: At this point, we can get rid of those curly braces, because it's only 1386 01:11:24,620 --> 01:11:27,190 a one-line for loop. 1387 01:11:27,190 --> 01:11:29,370 ALI: Okay, so does that make sense? 1388 01:11:29,370 --> 01:11:30,370 That was kind of a jump. 1389 01:11:30,370 --> 01:11:34,870 But it was just kind of syntax, more so. 1390 01:11:34,870 --> 01:11:36,250 >> Cool? 1391 01:11:36,250 --> 01:11:37,500 Any other questions? 1392 01:11:40,880 --> 01:11:41,570 ROB: All right. 1393 01:11:41,570 --> 01:11:42,310 ALI: All right, cool. 1394 01:11:42,310 --> 01:11:43,560 Thanks for coming to section.