Transcript from the "Hexadecimal" Lesson

[00:00:00]

>> Who here has ever heard the phrase, machine code? Yeah me, when you think of machine code, what do you think of? You think of that binary ones and zeros, right? And that's technically correct, the best kind of correct, but generally when the low level program is referred to machine code ,we're describing what's called hexadecimal.

[00:00:28] And if you thought reading binary was tricky, I got some bad news for you, it gets a little more tricky. But essentially, hex is the lowest form of computing we can do when we talk about machine code. We can't read ones and zeros, it just wouldn't make any sense, we wouldn't know where the breaks are, we wouldn't know if we're talking about an 8 bit integer or a 16 bit integer.

[00:00:53] We don't know if it's signed or unsigned things like that. So we use hex, hex is an abbreviated form of binary and we can actually convert back and forth. Just hex is a human readable format, and I'm putting air quotes on readable because you'll see it may not seem reasonable at first, but once you understand how to decode hex, you'll be like, so actually computer instructions are just numbers that you call and those perform some operation?

[00:01:22] Yeah, and those numbers are actually just ones and zeros? Yeah, that's how computers work. Which is like super fascinating. We rarely get a chance to go this deep, but you have to today cuz you're stuck here with me, so I appreciate that and let me and letting me nerd out about this.

[00:01:39] So I've said before hexadecimal, it's an intermediate step between binary and sort of high level programming. It's, concise enough for machines serene, so it's very short, it's very abbreviated. It's generally only, we'll say 3 characters or so sometimes 2, sometimes more, but usually 2 or 3. So it's very fast to read if you know what you're doing.

[00:02:05] So it's good for machines and it's good for humans. It's kinda the in between stuff because, like we said, writing ones and zeros just isn't that practical. And I wanna note here before you get intimidated and you close your laptop and say screw Jams, your friend and masters, this is not relevant at all, I don't need you to memorize how to read hexadecimal.

[00:02:26] I don't think necessarily that's gonna make you a better engineer, and that's all I wanted the other day to make you a better engineer. All right, but I think it's important to know how it is or how it works and how to read it, just at a super high level so that when you see it in the future, and you will if you're dealing web assembly.

[00:02:42] That you're like, okay I don't know what 0X27 means, but I know how to convert that and I know generally, what that means because I know that, that's actually a number in his calling instructions. When we think about hex, we said binary is base 2, so that's a 100 zero.

[00:03:05] Our current number system is decimal base, so that's zero through 9. hexadecimal is base 16, so that means it goes 1, 16, 256, 512, etc. And this is the part, I think, cuz I tried to learn hexidecimal for years and I always give up, I give up really early.

[00:03:28] So I'm teaching you how I learned it, actually, fairly recently I would say. Just because I had to know I got so frustrated with that building something and I'm an engineer and I've been an engineer for many years now, and there's parts that I don't know, so I try to dive in.

[00:03:45] So the part that is gonna throw you, is that hex uses the letters A through F to represent numbers. And that's where it looks confusing. So when you see something like 0X2E7, and you're like, what? Is that programming? What are we even doing here? Know that the letter E stands for 14.

[00:04:14] So now you can think of like, that's 214 and a 7. And it's written that way so we can get all the way up to basic scene without repeating ourselves. Now converting it is a little tricky, that's why I have this handy image here to help you process.

[00:04:32] So two 2E7 is equivalent to the number 743. And we got that through math, up but all we're doing is multiplying the value of the number against that position. So in this, in our instance or example of 2E7, the 2 is at the leftmost position. So that represents 256 multiplied by 2 plus 16 because the next position is 16, that's E, and E represents 14 times 7, that's how we get 743.

[00:05:05] Are you confused, a little bit, no? I see head saying no, you got it. All right, I know, intuitively you look at it and doesn't really make sense. But once you remember that it's base 16 and that letters represent a number, we see that actually this is just a number.

[00:05:28] And actually all of the computer instructions generally that we read are going to be in hex, and computer structures are just numbers. So we can't manually call, say add, we can't manually add two numbers, but we call an instruction like 0X2D, which points to a number, which actually represents an instruction on machine level code.

[00:05:51] And that's why it's called machine level code. So we're going down, we're getting in there, it's getting gnarly. Okay, so just a quick exercise just so you know, because I really want you to kind of grasp this concept, because hex is one of those things that programmers look at and they just walk away and give up.

[00:06:14] We'll do a quick exercise, what does 1F7 equal in numbers? So, 1F7, I'll give you a minute. And remember, here's our handy handy table of, we know that F equals 15, and we know that 1 is in 256 place is in the value it represents, and while you're thinking of that, check out this site, it's by the BBC.

[00:06:46] I don't know why, but I happened to come across this, and honestly, it is one of the easiest sites I've seen for how to understand hexadecimal, how to convert from that to binary and how that works on our decimal base system. So shout out to the BBC, who knew?

[00:07:05] And, all right, anybody have the answer yet? I see a hand in our chat, go ahead.

>> So just the question, so the hex number is in itself, the instructions to recreate a number?

>> Yes, well actually this converts down to binary where we think of it in terms of numbers because that makes sense to us at a cognitive level.

[00:07:32] But actually this converts down into ones and zeros. But when I say number, it's always a representation of the ones and zeros, it's just, I can't say 000001 means 1 in actual computing. So we say, 001 in terms of hex or we just say the number 1, I know it's a little tricky.

[00:07:58] But this really, for me this clarifies things for me learning assembly. This clarified how to think about computing and that everything is just a number at the core. There's no strings, there's no objects, there's no struts, it's just a number. And those numbers represent an actual binary bit of code, which represents a machine level instruction.

[00:08:23] Yeah, I see people still nodding haven't lost yet, thank goodness.

>> What is the problem with the decimal system instead of using hexadecimal can we use this one?

>> So, I hate asking rhetorical questions but at a low level, how would we represent a decimal number? We would do it with binary.

[00:08:48] But because a decimal number has a points, it's actually known as a floating point number. All right, it has a point and then it has almost an infinite number of zeros this way on the right hand side and has an infinite number of zeros and ones on the left hand side.

[00:09:05] So we couldn't really use a decimal system, because at the core we still need representing in binary. And to get the proper precision on decimals, we take a very, very, very, very, long number. Versus when we use hex, it's very short, it's very readable and it converts to a very, very small binary code.

[00:09:26] At the core, we really want computers to be as fast as possible and by being as fast as possible that means processing as small in number as possible, as few bits as possible to get our desired outcome. So remember I said I'm gonna nerd out a bit more.

[00:09:43] Computer CPU is just a bunch of switches, it's going to [SOUND] Really fast. I mean, we can't even comprehend how fast they move. And what it's doing is, it's loading information from what's known as the register. I didn't put this in the notes because it's not totally necessary, but you asked them to nerd out.

[00:10:02] It's known as the register, and the register is just a cache of ones and zeros at the lowest level. And what the CPU does is, it grabs instructions from the register and grabs data from register. It performs operations from them, and then puts them either back in the register or it sends those out to the larger computer which gets stored in RAM and things like that.

[00:10:21] But because we can, model processors or any processor at all, can only move so much data at once. It can only go [SOUND], the switches can only move so fast at a time. We really want to maximize the amount of computation we can do per cycle. And that's what it's called, is a cycle, is when a computer, it runs through all the instruction says give me some more, give me some more, give me some more and it just is constantly doing that.

[00:10:47] So, we really want this instruction to be as small as possible so we can fit as many instructions per CPU clock cycle as possible. So in that case, we wouldn't use floating point number of decimal because they're just too large. We can accomplish the same thing at a much smaller footprint, good question though.

[00:11:07] Okay, back to the what does the numbers represent? It represents 503, and we got that by multiplying 256 by 1, it's in the first position, F is equal to 15, multiply that by 16 plus 7, we get 503. Okay, I know you're like well, I sorta get it now, I don't see the utility.

[00:11:32] Again, this is because you'll probably come across this as you'll all be expert web assembly programmers in the future I'm hoping. You will come across this because assembly uses hex to represent most of its instructions or technically, if you're decompiling a program as in reverse engineering, you'll see the written text, it'll just be a series of hexadecimal numbers and it just looks like gibberish to you.

[00:12:00] Alright, it looks like gibberish to most programmers. But now you see that those are actually numbers and those numbers correspond to either actual numbers or instructions to the computer. Question in the chat, yes.

>> Why is it, so it was 116 2026 it's not 1 8 32?

>> Yeah, so our explanation earlier was that the base system represents at a given position, what are the possible values for that position?

[00:12:32] So base 2 means there's only two possible combinations, one and zero, base 10 means zero through 9, base 16 means there are 16 possible different numbers for a possible combination. So we start off at, we always start at one, because we need a one to get odd numbers.

[00:12:49] It's funny enough yet, if we started at just 16, if we would only have positive numbers which would make computing a little weird. So that's why it goes from 16 to 256 instead of 8 and etc. Why base 16? Honestly, I don't know, I would imagine it's to fit the maximum number of instructions so you wanna have the widest possible number of possible characters, without getting too big, but without getting into 32 bit number or 32 or 64, things like that.

[00:13:22] But yeah, I think there are historical reasons why it's base 16, I'm not terribly familiar. I just accept that's how the universe is, I don't fight against it.