Check out a free preview of the full A Tour of Web 3: Ethereum & Smart Contracts with Solidity course

The "Bits & Bit Shifting" Lesson is part of the full, A Tour of Web 3: Ethereum & Smart Contracts with Solidity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen explains the differences between base10, base16, and base2 numbers. The left shift operator (<<) shifts the bits in a binary number to the left by adding zeros to the right of the number. It's the binary equivalent of multiplying a decimal number by 10.

Preview
Close

Transcript from the "Bits & Bit Shifting" Lesson

[00:00:00]
>> Okay, so I know for a lot of people, some of this code makes no sense but the general idea hopefully makes a ton of sense, you can see the outline. The real only thing that makes probably no sense to most people is this one right here, so I'm going to start going over bits.

[00:00:14]
And this is where I kind of wanted that have that whiteboard, but we're going to use my favorite program, of course, the GNU Image Manipulation Program, or GIMP for short. All right, fantastic, look at that I even have this, so the first question I'm gonna ask everybody is, can somebody name this function?

[00:00:31]
>> Is equal.
>> It does have an equal sign in it.
>> Is divisible.
>> Close, is divisible, I wouldn't go with that one, good try though Lancebe, I appreciate the try.
>> Is it a union?
>> I wouldn't call it a union, all right, excellent but this is really good that means we you know we have some things we need to learn here.

[00:00:51]
And this is gonna be one of the most quintessential parts to interacting with the blockchain because once again especially when it comes to eatherium, storage is in bits. So is it Myelin or Milon correctly have named the function, I want to give it away ,I'm not gonna tell any, don't, hey, don't look at chat.

[00:01:12]
Alright, so first let's talk boolean logic alright, so I think everybody here is probably familiar with the ampersand sign. So if I did two ampersand signs, everyone knows what that means, so A ampersand B, the output. Let's pretend that while I'm writing this, that A is either zero or one, which is false or true and B is either zero or one, so what is the output to zero and zero, just shout it out.

[00:01:40]
>> Zero.
>> Correct, what is the output of 0 and 1?
>> Zero.
>> Zero, correct, what is the output of 1 and 0?
>> Zero.
>> Correct, what is the output to 1 and 1?
>> 1.
>> Yes, awesome, okay, I know this is very trivial but trust me, it helps make the bridge, let's do the exact same thing with A or B output straight over.

[00:02:07]
Let's just do this quickly, I'm gonna start yelling it, alright, zero or zero is 00 or one is one one or zero is one and one or one is one. This makes sense because ors are either or has to be true and have to be true or it's false, alright, these are very important operators.

[00:02:28]
I think everyone understands these that kind of like an essential level, let's also talk about not, so if I had not A, the output of course is going to be 0 becomes 1, 1 becomes 0, right? False becomes true, true becomes false, that's why people convert boolean into JavaScript with two bangs.

[00:02:48]
Because it coerces it into a boolean, flips it's value, reflips its value back to either true or false, for good or for worse, that is a real thing that happens out in the real world. So now, let's talk about one other operation that you probably have never seen, I'm going to put two symbols here just so it makes more sense, two carats.

[00:03:10]
It's called exclusive or it is my favorite, it's just my favorite thing in the universe, it, I'm right now already feeling all warm and fuzzy about it. So exclusive or simply means that something is either true like or let me save this one, it simply means that either A or B has to be true, but not both.

[00:03:31]
So that means if both are false, it is clearly false, if one is true, it is clearly true, if both are true, it is false, so there you go that's its truth table. So I think we all understand these truth tables, right, nothing in here feels confusing, we all get this fundamentally, this actually works in C because zero was truly false.

[00:03:54]
And one or any nonzero was true and that is why, by the way, you see that funny trick that people will use every now and then in JavaScript where they'll do an index of operation. And when that returns negative 1, meaning it's not found, they do a ~ because the opposite of negative 1 is 0 and we'll explain why, and the opposite of 0 is negative 1.

[00:04:17]
Therefore, negative one becomes the only false value in all other values become true. So you don't have to do that whole like greater than or equal to zero thing, instead you just hit them with the DD. Now we'll explain that in a little bit, but you'll see that in JavaScript and people always hate it, all right, so, we've talked about boolean logic.

[00:04:33]
Next, let's talk about basis bases are very important to understand, so I'm going to create a new little board here. And we're going to do this is we count in something called base 10, make sense 10 thingies, base 10, right? And so when I type in the number say 69, classic main number from Lord Ilan, what does this actually mean?

[00:04:58]
Well, this actually means I take 10 to the power of one multiplied by six plus 10 to the power of zero multiplied by nine, I add those two numbers together, right that is 60 plus 9. And so that is how we do that, so if I were to add a 1 right here, that's 10 to the power of 2, multiplied by 1 plus 10 to the power of 1 multiplied by 6 plus 10 to the power of 0 multiplied by nine.

[00:05:23]
For those that don't know, any thing taken to the power of 0 equals 1 and so our accounting system is literally 10 to the 0, ten to 1, ten to 2, ten to 3. And that makes sense, because when you multiply by 10, what do you do, you add a 0, right, when you divide by 10, you remove a 0, that makes sense.

[00:05:43]
We're just literally shifting it left or right by multiplying by 10, that is why multiplying or dividing by 10 was everyone's favorite thing that happened in grade school math because it was the easiest. It was the most exciting, so that is a base 10 thing, that is how we think you probably have never thought about thinking about numbers that way but that's actually what is happening.

[00:06:03]
So let's talk about a different base, you've seen this a bunch of times, you've just never put it into words, have you ever seen hexadecimal, the zero through nine, A through F? Hexadecimal is simply base 16, so if we were to look at say the number hexadecimal, hexadecimal is always denoted by 0x, a leading 0x, so let's say we look at the number 45.

[00:06:26]
Well, what's happening in hexadecimal and now it's not our 45, it's a different value, well, that's the same thing as doing four multiplied by 16 to 1 plus 5. I did this backwards for whatever reason, mirroring it, or I didn't do it, I guess it mirrored from the previous example, multiplied by 16 to the zero.

[00:06:45]
So that's the same thing as 4 multiplied by 16 plus 5 multiplied by 1, or 2 to the 2 multiplied by 2 to the 6, which is to the 8, so that's 250 way up, I did that wrong. That's not to the 8, I did the totally wrong anyways, that's 64 plus 5, right, which thus equals 69, bam, quick math, so you can see how I got that number, right?

[00:07:10]
Is that I simply just did the exact same base walking, except for I did it with 16s instead of 10s, now, why do we choose A through F? Well, think about it when we count with base 10, we use 10 symbols to represent our language, which is 0 through 9, we need 6 more symbols for base 16.

[00:07:31]
So it makes perfect sense why it's A through F, right, I mean, that just makes perfect sense, so there we go, we have 16 symbols, 0 through 9, A through F. That means f = 15, F equals, we'll put it there, 15, right, just like 9 is 9 in our language.

[00:07:46]
So if you had a base 16 or a hexadecimal number, and you had base or a hexadecimal one, how would you multiply it by 10, if you will, you'd multiply by 16. Multiplying by 16 produces 0, multiply by another 16, produces a 0, this is kind of very important stuff to know, so we're gonna do the exact same thing except for one more time.

[00:08:09]
We're gonna do with base 2, base to only as two symbols, you want to guess what those two symbols are, 0 and 1 that's right, so base 2, 0 and 1, this makes perfect sense. This is binary, binary is a base 2 counting language, which means if you had four binaries, right here, this is two to the zero, which is one.

[00:08:31]
Two to the two, or one which is two, two to the two, which is four, two to the three, which is eight, so this number is simply eight, right, if I were to add one to it, I'd add one right here. If I were to add one again, I wouldn't just call this thing to know, I'd have to roll it over just like if you add one to nine, you don't write 10 in place, you actually write a zero here and put a one right here.

[00:08:57]
So that is counting in binary, right and so you can keep on going, I could keep on counting, because that's how it works, does that make sense? For the most part, and the reason why I chose four is because two to the three or eight is the same thing or.

[00:09:12]
Yeah, two to the three plus two to the two plus two to the one plus two to the zero is the same thing as hexadecimal, so hexadecimal, f is represented by for binary numbers or refer to as a nibble. So 1111 or 15, 1 plus 2 is 3, 2 plus 4 is 7, 7 plus 8 is 15, so that means two numbers are required to represent a bit.

[00:09:39]
So in hexadecimal, it's two of these symbols in base two or binary, it is eight numbers to represent a bit. That makes sense, because these are bits or the fundamental unit of the computer effectively. So eight bits makes a byte so everyone kind of tracking with this does this all make sense this is just basis I needed to cover this.

[00:09:59]
Because it just is really hard to think about it unless you understand that hexadecimal and binary are no different and decimal, they just have different ways you count. All right, so we don't have any questions, so we've already done some conversion, we can do some more conversions. I'll do one more conversion, let's convert, let's see, let's do a new one, can someone shout out a easy two digit number?

[00:10:24]
>> 23.
>> 23, perfect, all right 23, let's convert this to binary, so remember, you start with what is the largest number, that is, what is the largest power of two that this number contains?
>> 16.
>> 16 perfect, so we're gonna say this one represents 16, which means we should have eight, four, two, one left.

[00:10:45]
All right, so 23 minus 16 is going to equal a number that I'm currently coming up with right now which is seven, correct, okay. It's a joke, anyways never, so what's the largest number that this one contains?
>> 4.
>> 4 perfect, so we write in four because remember this one's 8, this one's 16, this one is 4, so now we do it again, so now we have 3, so what's the largest one that this one contains?

[00:11:11]
>> 2 which now becomes 1, what's the largest power of 2 that this one contains, 1 which is power of two to zero, which is one, there you go, this number is binary 23. So we just did that live on the fly, so you can kind of see how these things are represented, this is what's happening underneath the hood when you use a unsigned integer.

[00:11:32]
This I can think about it, there's this whole concept of endianness, we're not gonna even think or worry about that, don't worry about it, also, enjoy perfect. All right, so just like in base 10, when we multiply by 10, we get a 0, in base 2, when we multiply by 2, we get the exact same effect, so this number, 23 times two or 46 is represented just like that.

[00:11:59]
I just simply added a zero, let's prove it out, that is 2, 4, 8, 1, 6, 32, 32, that is 40, that is 646, right, we just walked through and we just did that exact same concept. Except you multiply by 2, you get yourself a 0 and so of course, since we use binary so much in computers, we instead of doing something like this, I want to shift it over three times.

[00:12:28]
You don't do that, right, instead, you use this operator, which is an operator I just got done using, I want to shift this thing three times. Or I want to multiply this number by two, three times, which will produce three leading zeros, alright, so this is good, so that's good knowing about binary, right?

[00:12:47]
You're starting to understand why this is, everyone kind of get this concept, it's just like multiplying by 10. Alright, so this is called a left shift, because we're shifting our binary to the left, a right shift is dividing by 2, so show you right shift this way, that is why when you right shift an integer, it floors it.

[00:13:03]
Because a it's a non floating type which is represented differently but also the remainder is shifted off of the edge, when you divide by two, you just don't get that number anymore.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now