00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll be covering binary numbers.
00:13 everywhere! At the heart of all the digital goodness in our lives is one or more microprocessors. Inside a microprocessor, there are millions to billions of transistors.
00:25 The funky collection of lines on the right is the circuit diagram symbol for a transistor. When an electrical charge is applied to the transistor’s base, current can flow through the transistor between the collector and the emitter.
00:40 Turn the charge off, and the current stops flowing.
00:46 Transistors are small switches. Apply a charge, and the switch is on. Remove the charge, and the switch is off. By changing the charge to different values on a series of switches, you can form a pattern.
Switches that are on represent 1, switches that are off represent 0. These patterns can be used to represent other things. The pattern
101 means the number 5.
01:19 This particular mapping is based on a mathematical transform. I’ll explain more about that later.
01:26 The patterns can mean other things as well. Pi gets approximated by a sixteen-digit floating-point number, which is represented by the following binary code.
01:36 This code is actually comprised of three numbers: a sign indicator and an exponent value and a fractional decimal value.
"a" is ASCII 97 in decimal, which in the CPU is represented by this binary value.
Even Mona Lisa’s sly smile on your screen is really just a large array of these numbers. Each pixel in the picture is represented by a number. This drab-looking square of greenish is the hexadecimal number
7F8053. The hex number breaks down into three parts: the red, green, and blue that make up the color.
7F8053 is decimal 8,355,923—or this long string of binary digits. It’s binary all the way down.
02:33 So, what is this binary number stuff? Let’s take a quick detour and think about the numbers you probably know a little better: decimal. Consider 4,239. This number can be broken down into four parts: 4,000 plus 200 plus 30 plus 9.
02:55 Each of these numbers can be thought of as the combination of a number and a power of 10. With 1000 being 10 to the 3, 100 being 10 squared, 10 being—well—10, and the ones digit being 10 to the 0.
03:12 Although base-10 feels right or special to us, that’s just because it is what was taught to us at a young age. There’s an argument that base-10 really only exists because of the number of fingers we have.
03:23 Historical civilizations have used different bases. The Babylonians used base-60, while the Mayans employed two different bases, base-20 and base-360, depending on whether you were a priest or not.
03:36 Those might seem kind of crazy, but they actually make math with numbers like 12 easier. How many hours on a clock? How many degrees in a circle? Yep, you’re using more than just base-10 yourself.
03:48 You’re just not necessarily conscious of it.
03:52 A number can have any integer base. Commonly in mathematics, the subscript here indicates the base. 4,239 base 10 is subscript 10. 4,239 base n has subscript n. For the general case of base-n, each digit is a power of n. 4,239 base n is 4 times n cubed plus 2 times n squared plus 3 times n plus 9 times n to the 0. Note that no digit in a base-n number can be larger than n itself. In base-10, each digit must be less than 10.
04:38 Back to those switches: that on and off stuff, binary numbers, or base-2. Just like base-10 or base-n, each digit is a power—in this case, of 2.
1011 in binary is 1 times 2 cubed plus 0 times 2 squared—still 0—plus 1 times 2
05:02 plus 1 times 2 to the 0. This gives you 8 plus 2 plus 1 in base-10 for a total of 11 in base-10.
05:15 In computer science, there are four bases that are commonly used: decimal because that’s what humans are used to, binary because that’s what the machines want, and the last two are things that translate back and forth to binary easily—octal, which is base-8, and hex, or hexadecimal, which is base-16. Both octal and hex map to binary values, and with a bit of practice, you can convert them in your head.
5 in decimal is 4 plus 1, giving
0101 in binary, 2 squared plus 1. Octal, or base-8, allows for digits to be up to and including 7. So decimal 5 is also 5 octal. Hex, or base-16, allows for digits up to 15 in decimal. So one more time, the answer is also 5. Let’s try another one. 8 in decimal is 2 cubed, so the fourth bit is on. 8 decimal is bigger than the maximum value of a digit of 7 in octal.
06:20 So just like the other bases, you add a digit and roll the counter over. Or, going the math route, 10 octal is 1 times 8 to the first power plus 0. In hex land, you’re still under 15. So far, the conversion from decimal to hex is pretty simple. It will get worse, I promise. Let’s add 1 more to our 8 and get 9.
06:44 I didn’t even have to use my fingers. Adding one in decimal means adding one more in binary as well, so the first bit position increments. Adding one more to the octal value so the digit goes up by one, and good old hex still isn’t complicated.
07:02 It’s coming, wait for it. This time, I’ll add two more to the decimal value. And that means incrementing the binary value by 2. That’s a power of 2, so in this case, just turn on the second position. Two more to the octal case still hasn’t exceeded the max digit, so 11 octal becomes 13 octal. Notice that I don’t call these 11 and 13. 11 and 13 are decimal concepts. For clarity, I will try to remember to keep using the digit style.
07:35 This is the technically correct style, but most people won’t want to be corrected if they use the incorrect style. You will hear people say things like “13 octal.” Alright.
Hexadecimal is base-16. That means the largest digit can be the equivalent of 15. How do you represent 15 as a single digit? You switch to the alphabet. 10 decimal is
A in hex. 11 decimal is
B in hex.
That goes all the way up to 15, which is
F. Last one: let’s do something a bit bigger. This time, I’m going to start on the right-hand side with the hex value.
B is 11 decimal, so this is 11 times 16—because this is base-16—plus 5. This turns into 265 in octal, and
1011 0101 in binary.
08:31 I’ve grouped the binary number into two sets of four digits. This is done on purpose. Each of these four digits directly corresponds to a digit in hex. This is why hex gets used.
It is a convenient short-form for binary.
1011 is decimal 11, or
B in hex, and
0101 is 5 in both decimal and hex. If you have the binary equivalent of the sixteen digits of hex memorized, you can quickly substitute one value for another. Fun fact—and this is the problem with my sense of humor, you’re never going to believe what I’m about to say is true—half a byte—that’s four bits or a single hex digit—is called a nibble.
09:14 Who says computer people can’t be fun at parties? And, of course, in the more human-readable format, 11 decimal times 16 plus 5 is 181 decimal.
09:28 Now that you’ve got binary in your brain, next up, I’ll show you bitwise operations that affect binary values.
Become a Member to join the conversation.