Coding 101 Episode 1 (Transcript)
Today on Coding 101, we're going to learn all about converting binary to decimal!
Netcasts you love, from people you trust. This is TwiT
Bandwidth for Coding 101 is provided by CacheFly. At cachefly.com. This episode is brought to you by lynda.com learn what you want, when you want with access to over 2,000 high quality online courses and training videos. All for one low monthly price. To try it free for 7 days, visit lynda.com/c101
Father Robert Ballecer: Welcome to coding 101. It's the show where we give you – US, us give you – the knowledge to live in the wonderful world of the programmer. I'm Father Robert Ballecer.
Shannon Morse: And I'm Shannon Morse and for the next 30 minutes, we're going to teach you everything you need to know about codes, so you can be a code monkey.
Fr. Robert: That's right. So Shannon, Queen of all things that are code, queen of all things that are here on coding 101, what topic do you want to start with?
Shannon: I think it would be smart to start with binary's first.
Fr. Robert: Good call. When we think about coding, when we think about how coding is depicted in the movie world, it's often with just letters and numbers just scrolling up the screen. So why not show people that basic unit of understanding, what is binary? Now Shannon, have you experienced Binary at all, do you even know a little bit of what it is?
Shannon: A little bit, it's 1's and 0's all over the place, and they meet in different letters and different numbers.
Fr. Robert: Right, good. Actually that's good, we'll start with that. When we talk about binary, we're talking about a binary system, so it's based on 2. So 2 to the power of 0, 2 to the power of 1, 2 to the power of 2, etc, etc, etc. Each time we have either a 1 or a 0, it means that we either count it or we don't. Are you following me so far?
Shannon: Yeah, that makes sense.
Fr. Robert: So let’s say we had 2 bits. And remember, a bit is either on or off, it's only got 2 values, it can either be a 1 or a 0. If I have 2 bits and they can only be 1 or 0, how many combinations can I have?
Shannon: 2?
Fr. Robert: No, let’s go up.
Shannon: 4!
Fr. Robert: Bang! Exactly. Because I can go 11, 00, 10 or 01, right?
Shannon: You're correct!
Fr. Robert: Exactly. So that's how the binary system works. As I add places, as I add numbers, I increase the number of possible combinations, of possible values I have, and each time I add a position, I increase it by 2.
Shannon: Oh boy, so we're back in math class, basically.
Fr. Robert: We're back in math, sorry. We've got to do the math. Let me give you a quick exercise to show you how binary works. We've got this wonderfully, expertly drawn...
Shannon: It's beautiful!
Fr. Robert: Exactly, it's artwork. Now what number do you see, just read off the numbers that you see here.
Shannon: I see 101010 and 10, so four 1's and four 0's.
Fr. Robert: Yes. Here's the thing though, when we're talking about binary, we're not reading from left to right, we're reading from right to left. So I'm actually looking at 01010101.
Shannon: So a computer, it's going to start at the first place and go all the way out.
Fr. Robert: When I'm reading binary anyway, yeah, it's going to start over there. Now here's how I actually calculate what this value is. Each of these is a position, again as I mentioned before, 2 to the power of that position. So this would be 2 to the power of 0, 2 to the power of 1, 2 to the power of 2, 2 to the power of 3, 2 to the power of 4, 2 to the power of 5, 2 to the power of 6, and 2 to the power of 7, and back in math class, we know that 2 to the power of 0 is 1, 2 to the power of 1 is 2, 4, 8, 16, 32, 64 and 128, the last value. Right?
Shannon: Got it. So one thing to keep in mind, is that the first placement is always to the power of 0.
Fr. Robert: Always to the power of 0.
Shannon: Not 1.
Fr. Robert: And the reason that is, think about it, if it was to the power of 1, that means the very first value could only be 0 or 2. 2 to the power of 1 is 2, which means my numbers could only be even. So what this does, this very first position allows me to have odd numbers, because if I have a 0, it's an even number.
Shannon: You have 4 options.
Fr. Robert: Yeah, if it's a 1, I have an odd number. Here's how it works now. If I've got these 8 positions with those powers to them, if it's a 0, I don't count it. If it's a 1, I count it. So 0 means there's nothing there. This position means I'm adding 2. This position means I'm adding 0, this position means I'm adding...
Shannon: What is that, 4?
Fr. Robert: No, this is 4, this is 8. This is 0, this is 32, this is 0, this is 128. So that I'm doing to calculate the value, is I just add all of these up, and I get a total of 170. So in binary, 01010101 = 170.
Shannon: Wow, that blows my mind!
Fr. Robert: It blows your mind, and it's one of those things, where I think we've all seen it before, and some of us know how to calculate it in our heads, some of us don't,
Shannon: Exactly.
Fr. Robert: This is the simplest way to do it, but the reason we're talking about binary, Shannon, is because we're talking about programing, right?
Shannon: Yes! So what exactly does binary have to do with programing?
Fr. Robert: Binary is actually a really good way to get introduced to how programing method works. Because if we're trying to learn programing, we're really trying to figure out “How do you break down a problem, into small solvable pieces?”.
Shannon: Yeah!
Fr. Robert: Yeah, exactly. So I'm going to need your help on this, because if I go to my IDE, my integrated developer environment, my programmer console, and I type in – convert 01010101 to a decimal, it will have no idea what I'm talking about. I actually have to break it down into the component pieces, those parts that allow me to figure out what's going on. So if I wanted to convert this into decimal, what would you say is the first step that I have to take?
Shannon: You would want to create the total variable, right?
Fr. Robert: Yes. I need a place to store all this, right? If I'm going to be adding up all these variables, I'm going to need a place to say “Start adding up anything I give to you”. So I create a holding spot that says “here are the values I want you to total.” So I've got my holding spot, what's my next step?
Shannon: Now you're looking at each of the positions, correct? So you want to make sure each one has a value of 0 all the way up to 7. Got to remember that 0!
Fr. Robert: Right, we always start at 0. So I've give each position a value, I know that this one is worth 1, this one is worth 2, this one's is worth 4, this one is worth 8, 16, 32, 64, 128. So I now know the values of all those positions.
Shannon: And then if it's 0, you're going to skip that, correct? And go on to the next one?
Fr. Robert: Exactly, because if it's 0, it means I'm not adding it, ignore it and move on to the next one. But what happens if it's 1?
Shannon: So that one always gets raised... 2 to the power of that position. So 2 to the power of 1, to the power of 3, to the power of 5 and to the power of 7.
Fr. Robert: Right.
Shannon: Haha got it!
Robert So I've got all my values, what's my last step?
Shannon: So you add all of those together, correct? To the total variable that you are going to use for your coding, and then after that, let’s see, you have no more variables to calculate.
Fr. Robert: I have no more variables to calculate, so I say “Hey, give me my total”. My total of 170. Now that's great – actually, you know what? Give yourself a hand.
Shannon Yay! (clapping) A good old pat on the back!
Fr. Robert: It seems simple and I know people out there who are rolling their eyes going “I learned this in math class back in high school”,
Shannon: But you've really got to understand this to get the basics of coding, to get the theory behind it.
Fr. Robert: Exactly. You have to know how to talk to the computer. You have to know how to break down any problem. This is simple but it falls for anything in the real world. If I want to send and elevator from the ground floor to the top floor, I need to do the same thing. I need to say, ok, what data do I have, what sensors do I have available to me, and how do I calculate where that car is and how fast it needs to move and what direction. Same idea.
Shannon: Good point! Real life uses.
Fr. Robert: Real life uses. Now we're going to get over to our code warrior, we've brought in a special expert who is going to show you how you take this knowledge, and turn it into that real world programming, but before we get there, Shannon, do you think we should talk about the good folks at lynda.com?
Shannon: We should, I love those guys.
Fr. Robert: Now lynda.com, for any of you who don't know, is a source for knowledge on the internet. They just give us the things that we need in order to be able to learn the subjects that we want to learn. You want to learn programing, it's there. If you want to learn Adobe Premier, it's there. You want to learn about photography, it's there, lynda.com is your one stop shop for knowledge. Lynda.com is an online learning company that can help anyone learn creative software and business skills, to achieve both personal and professional goals. With a lynda.com subscription, members receive unlimited access to a vast library of current and engaging video tutorials across a wide variety of subjects. Everything from creative and software skills, to business negotiation and programming. At lynda.com, you'll learn to create and build applications from the foundations of object oriented programing in C and C+ plus, to desktop and mobile apps for today's popular operating systems. You'll explore the fundamentals of programing, building applications with dot net, PHP and my sequel, managing data with sequel data bases, and connecting to cloud services, and oh so much more. No you know, Snubs, one of the things I like about lynda.com is, it let’s me back into the programming that I learned way back when. That kind of knowledge that's sitting in the back of you head, you're like Oh, I wish I could access it! Lynda.com gives me a way I can jump into that, I can skip chapters, I can go directly to the knowledge I need to re-learn to refresh my memory. You can improve your skills, learn new software, and keep up with technology with lynda.com, they have over 2,000 courses with more added daily. They have popular topics including foundations of programing, HTML, PHP with my sequel, objective seed, java, java script, and word press. Instructors are working professionals at the top of their fields and expert teachers, and you get high quality video production with state of the art video's. These aren't the homemade video's you will find on youtube, with some guy with a shaky camera and a bad microphone. You get curated course content, each lynda.com course is carefully structured so that users can go from start to finish, or jump to a specific chapter. You get easy to follow videos that help you find the answers that you need, this is huge! Search-able transcripts that let you search within a video to help you save time and find exactly what you are looking for. They have courses for all experience levels from beginner, intermediate and advanced, from a wide range of technical skills, creative techniques, business strategies, and more. You can watch from your computer, tablet or mobile device, you can switch and pick up on the chapter where you left off, that means learning at your own pace on your own schedule. So Snubs, here's what we want our coding 101 audience to do. We want them to try lynda.com, the source of all knowledge on the internet. It's only $25 per month for access to lynda's entire course library, or for $37.50 per month, you can subscribe to the premium plan which includes exercise files that let you follow along with the instructors exact same assets. You can try lynda.com right now with a free 7 day trial to access the entire library. That's over 2,000 courses free for 7 days. That's lynda.com/c101. We thank lynda for their support of Coding 101.
Fr. Robert: Snubs, you think maybe we should invite our expert?
Shannon: Who's our expert?!
Fr. Robert: Well we've got a coding warrior, Mr. Lou Maresca, Lou are you on the line,
Lou Maresca: Hi guys, how's it going? thanks for having me.
Shannon: Hi Lou!
Fr. Robert: Now Lou, for the folks at home who maybe don't know you from the other TwiT shows you've been on, who are you and who do you work for?
Lou: So I'm Louis Maresca, I work for Microsoft, I've been there for about 10 years, actually this January.
Fr. Robert: You are going to be our coding expert for module 1. Now I understand we're going to be coding in C#. Before anyone out there gets crazy – Snubs -
Shannon: Like me!
Fr. Robert: Like you. Everyone has their favorite programing language. What's yours?
Shannon: Well I learned Java back when I was in college, and before that it was HTML so both very, very easy and good starters.
Fr. Robert: Exactly. I've been a big fan of the visual language, I do a lot of assembly, just because I can say “I do assembly”, even though it's really not useful anymore. Everyone has their own favorite language, but we're starting with C# just because it's actually free. You can download the visual studio express for free from Microsoft. Right Lou?
Lou: That's right. You can do express, or you can even do monodevelop.com which is the mac version which you do free as well.
Fr. Robert: Now if we are not using your language of twist, just wait, we're going to get there. But for now, Lou, we were talking about the binary stuff, about how we can convert a binary string into a decimal number. Tell me, do you have a way to do it in code?
Lou: We sure do. So what we're going to do today, we're actually going to show you a way to have the user enter a binary number and then we're going to take that binary number and we're going to convert it to decimal for you, so we're going to take that binary, convert it to a number so you can actually see what you're typing in. So the first thing that we're going to do is we're going to want to basically ask the user what they should type in. Like for instance, if they should type in the binary number, the decimal or what not. So what we're going to do is use a fancy-smanchy object or thing that does that, called the console object.
Shannon: What is a console object?
Lou: So a console object basically is just a thing, objects are just things that do things, in object oriented languages like C#. Basically, a console object, you there are what they call console applications, the old school applications that just do text based type applications, those are called console applications. They have them in Mac, they have them in Linux, they them in Unix, they have them in Windows, and basically just asks the user to do things and then they enter the data in there, and then there you go, way to go.
Shannon: Ok, what do you say we get into it!
Fr. Robert: So first of all, Lou, can you actually show us this programing working?
Lou: Definitely. So basically what we're going to do, this is the programming working. It's going to ask the user what binary number it wants, let’s enter our arbitrary binary number, and then it will convert the binary number to a number, and I have 2 ways to output it here. This is doing it twice, but basically there's 2 ways to output it. So easy enough, right?
Fr. Robert: So this simple program, show us the code.
Lou: Sure. The first thing that we want to do is we want to ask the user for the binary number, so that's what we call the console, what we call writeline. Console write, console writeline, writeline means it enters the whole line and then it ends, or write means it continues to enter it on the same line. So we'll just do writeline, we're going to say “hey, please enter a binary number”, and this is a common...
Fr. Robert: So hold on, that console, console.writeline is just a way to tell the compiler “hey, I would like you to output whatever string I put in the following parenthesis.
Lou: That's right. It's going to output it on this old school console app and this is going to say “please enter the binary number”. And then what we want to do, is give the chance for the user to actually enter the binary number, and we're going to capture that for them. And the way we're going to do that, is by using Console.ReadLine, we're going to do it this way. This is basically saying “Hey, console, go and get whatever the user types, and once they hit enter, capture that information for us”.
Fr. Robert: Ok, and just like we did in our beginning demonstration, you're creating a variable there called binary number, which the console command is then going to fill the value.
Lou: That's exactly right. You can go type in garbage if you really wanted it to, and it would capture it in this that we call a string variable right now. What we want to make sure is that they enter a binary number.
Shannon: Question for you, Lou, whenever the user actually types in their number, it is going to end up in between those parenthesis according to the code? After the read line?
Lou: That's right. So what it will end up doing is it will – right now what it will do is it will send the information that the user entered in, the variables or the number or whatever into the binary number, and then the program will just end right now, but yeah, it will basically run between these parenthesis.
Shannon: Cool, ok!
Fr. Robert: So Lou, let me just break it down for the users at home. Right now what we are looking at is console.writeline is the same thing as saying “print”, and you've said “print the following phrase”, which is “please enter a binary number. Everything in that parenthesis between those two quotation marks, it's going to output to the screen, it's going to “write” to the screen. Then it's going to sit there and wait, and the reason it's going to wait is because you've got that console.readline open in closed parenthesis, which essentially tells the computer “wait here until someone gives me a value”. And then when they give you that value, it's going to take that value and put it inside the string variable called binary number.
Lou: That's exactly right. So once we actually have the number, we're going to do some fancy-schmancy conversion of that number, we're not going to really worry about that now, but basically what it's going to do is this fancy-schmancy function here and try to confuse everybody, it will basically convert that number to....
Fr. Robert: let me just say, folks, you're going to get this a lot. Consider this the black box part. We want you to focus on the console commands, these are the important things. How to output things to the screen, and how to get things in. We're going to give you all of this code in the show notes, along with links to the compiler. So what we want you to do is just copy and past exactly as you see it from our show notes, into the compiler, and focus on the things that we're teaching you, and in this case, we're teaching all about the console.
Lou: That's right. So easy enough. Once we convert this, once we do this black box magic under here, we get a number back, and then we want to basically display to these, we want to say “Hey, here's the number that you basically converted to”. Again what we're going to do is use the same type of thing, but this time we're going to use just the write command, again, writeline writes an entire line and then ends, and then the next line is used, or you can do console.write which does a write and it does only one line and you can continue to append to that line. So what I'm going to do is append to it, I'm going to append to that number that you've created. So basically it will say - your decimal number is just a decimal, “Your decimal number is” and then it will say whatever the number is on the same exact line.
Fr. Robert: So what you've done here is console.write, gives you, again, because we know it's like a print, it's printing out your “decimal number is”, and because you used write instead of writeline, it's going to stay on the same line, it's not going to move down to the next line like it did in the first console command. Then the second command that you give it, “console.write(number); just outputs whatever is inside the variable called number.
Lou: That's it, easy enough. And then I added another little thing down here, basically this read will basically just pause the app so that the console will stay up there and wait for the user to hit enter, just to quit the app basically, so it's just another enter key. So if we run this, should do exactly what we do for us, so “Please enter a binary number:” we'll enter some binary number that we divy 4, and there you go, your decimal number is now converted to 12 and it did all the same lines, your decimal number is 12.
Fr. Robert: And so Snubs, I want to call off to you over here, because I think the best way to make sure our audience is learning about the console command, I know they're going to be freaking out about all the code that's actually required to do the conversion, but we don't want them to worry about that, right?
Shannon: Yep.
Fr. Robert: We want them to focus on the console. So Lou, if we go back to your console screen, looking at your code, what if we gave our audience the challenge of re-writing just what they see on screen, just what is printed on the screen. Because right now, it says “Please enter a binary number:” and then your decimal number is whatever. Tell me a string that you want them to change it to.
Shannon: I want them to – let’s see – what about “Please enter a calculation:” or something.
Fr. Robert: Please enter a Snubs number!
Shannon: Haha!
Fr. Robert: How about this, “Please enter the binary code you believe represents snubs:”
Shannon: Oh that's a good one!
Fr. Robert: ok, let’s do that. So Lou, if I wanted to do that, how would I change the code to make that happen.
Lou: So basically what you want to do is just change this line up here to basically “Please enter a number that represents Snubs:” - is that what you said? I can't remember.
Fr. Robert: That's it!
Shannon: That's cool!
Fr. Robert: And what if we wanted to change it so that instead of moving down to the next line, it just stayed on the next line for requesting the number.
Shannon: Oh, I know! You would delete “line”.
Fr. Robert: Ok, let’s try that. Now run that, let’s see what it looks like.
Lou: Now when I enter my number, or whatever I want to enter...
Shannon: It ends up that you enter it on the same line, and then you get the answer on the line below it.
Fr. Robert: And what happens if we want to get the answer on the line below that?
Shannon: You add in line again?
Fr. Robert: Yeah, exactly. We'll see (“Console.write(your decimal number is”); - I could just add “line” to that, which just forces it down, if we run that now...
Shannon: That's so cool.
Lou: There we've got it, we've got our value.
Shannon: I think it looks better the other way.
Fr. Robert: Now Snubs, I've got to ask you, isn't this the way that people learn? I mean, we could go over all of the code, we could go over all the different ways to do this command,
Shannon: Obviously, we will have to break down how to get into this program in the first place, and how to find this file so you can start this program and do this yourself, but this is a great way to show people how to input your own values, and how to output whatever you want. Basically, you break it, and then you fix it.
Fr. Robert: Now folks, this is what we're trying to do. This is why we've asked Lou to create this very simple program, which is – please, download the IDE, put the code in from our show notes, so you don't have to write anything, and just try to to fix the console write commands.
Shannon: Right, exactly, I totally agree.
Fr. Robert: There's nothing that will teach you more about code than doing just that. Now Lou, if they wanted to find out more about the console command, where would be a good resource for them to go?
Lou: So C# language is well documented all over the net, but some of the good places are msdn.microsoft.com or you can even go to stackoverflow.com, that's a good place to go and ask questions about programming in C#, but like I said, best place is msdn.microsoft.com you just type in C# language conventions.
Fr. Robert: Right. And someone in the chat room is asking “Is convert binary to decimal a built in function?” It is not a built in function, it is a Coding101 Lou Maresca function, but again, we don't want you to play with that – unless you feel REALLY comfortable with it. Here's what I like about what we're going to be able to do on coding 101: If you're a really advanced user, and you want to tweak the function, and you know how it actually works, then by all means, do the same thing, get in there, break it and fix it. But if you're just learning, if this is literally the first thing you've ever done with a programming language, then stick to the console.write and the console.read. Just follow what we've asked you to do and get comfortable with the interface.
Shannon: Yes, definitely. And I'm going to be here to help you guys along too, because this is new to me as well, I'm going to be learning as much as you guys so I'll be asking the same questions as you, and if you have any questions, of course, you can contact us.
Fr. Robert: And we're teaching here. Now Lou, I do want to give the folks at home just a little bit of a taste of what we're going to be looking for in the future, this is a very simple command, we're talking about console.read and console.write, what do you have for the future? What are we going to be learning the folks in C#?
Lou: We're going to ask the user for information, get the information back, we're going to learn about loops, we're going to learn about variables, we're going to learn about how to convert from binary to decibel, from decibel back to binary, so lots of cool stuff.
Shannon: Awesome, I can't wait!
Fr. Robert: It is kind of exciting, right?
Shannon: It is, I can't wait to start writing my own programs.
Fr. Robert: Now let me ask you this, can we do this Snubs? I think in order to follow along with the folks at home, we need to do a good faith effort to do the programming as well.
Shannon: Oh yeah, absolutely. Give me some homework, I'm ready!
Fr. Robert: We'll do some homework. So this is what we're going to do, we're going to make sure that all of this code ends up in the show notes, we're going to make sure that there are a couple of challenges, we're going to ask you to change a couple of things, like we did with Snubs, we're going to ask you to change some simple things. Change what comes out on the screen, change how input comes back into the screen, and we're even going to leave some homework for the more advanced users, we're going to ask you to get in there and ask you to actually change the function. No matter what your skill level, we're going to do something for you, just make sure to go to our show page. You think maybe we should tell them where our show page is?
Shannon: Absolutely. So we just opened up our new show page, so you can subscribe, you can find all the feeds, you can download all the different versions of coding101, whether you watch audio or video. Twit.tv/coding101.
Fr. Robert: And also, if you want to ask us questions, outside of that page, outside of say, sending an email to twit, you can reach us most easily, at least for me, is on my twitter page, right? I respond almost immediately. If you tweet me, just tweet me @padresj and I'll get back to you as soon as possible. If you have a comment about something that you saw on the show, if you have a suggestion for something that we could teach a little bit better, or if you have your favorite language that you want us to consider for a module of coding101, please, please let us know. What about you Snubs, where can we find you?
Shannon: I'm @Snubs on twitter, that's probably the best place to find me, and I just looked up the correct link for our show, and it would be twit.tv/coding-101 – at least that's what it's showing me here.
Fr. Robert: coding-101, that's something that was created by the programmers, I don't trust those programmers.
Lou: /code works.
Shannon: /code, alright.
Fr. Robert: Also, do you know that we do this show live? Every Thursday, right after “Know How”, which by the way, I'm the host and Shannon is my most excellent producer, after Know How 1:30 every Thursday pacific time, we're going to be bringing you another lesson in basic programing languages. There it is. So go ahead and drop in at live.twit.tv and maybe you can find a little something-something. As long as you're there, Shannon do you know about our chat room?
Shannon: A little bit, yeah, they chat with us, they talk to us...
Fr. Robert: They're actually a big part of this show. If you watch us live, you'll be able to jump into our chat room at irc.twit.tv and talk to us during the show. Tell us what you want to see, ask us for clarificationon something you don't understand. It's all part of this experiment that is TwiT.tv. Well Lou, thank you very much for talking to us, again, very simple program, we're going to make sure that your code ends up in our show notes, that people can copy you and Shannon, want to take us out?
Shannon: End of line.
Lou: Haha!
Shannon: Haha, get it?!