Who in this lecture, we're going to talk about Question 20, how does the binary number system work?
The binary number system is used to represent numbers using only two digits, zero and one, for example,
that this email number 13 is one one zero one in the binary number system.
As you probably know, every piece of data is stored in the computer's memory as a series of bits beat
is the smallest unit of information, and it can only have two values zero or one.
This means every information we want to store in the computer's memory.
A number string, a complex object, or an entire program is in the end stored as a series of zeros
and ones in this lecture will focus on the numbers.
As we mentioned, the binary number system represents numbers as the zeros and ones, so it fits perfectly
how data is stored in a computer's memory.
You may think that the binary number system is not something you need to understand, but it all happens
under the hood.
After all, there are programmers all around the world who have no idea how the binary number system
works and they are doing fine.
But there are a lot of aspects of programming that are affected by how binary number system work, and
it's not possible to understand all of them without knowing the binary system at all.
If you are not convinced.
Let me give you a little spoiler from the next lecture.
We will talk about a banking applications client can lose all protection granted by daily transaction
limits, and in the end, have the account cleaned out by someone who accessed it illegally.
It could be a void that if the programmer understood how operations on binary numbers work.
All right, before we try to understand the binary number system, let's do so with the system we use
on a daily basis.
The decimal number system the base of the system is number 10.
You must know that you can build a volume number system based on the number rather than zero.
But 10 was probably the most natural for the human race as we have 10 fingers and we started our journey
of understanding mathematics by counting them.
Let's consider the following decimal number.
You probably don't need much explaining here.
I simply know what this number is.
You can imagine what it means to have a $231 or any other currency.
Use a folder with eight hundred and thirty one pictures or a book with eight hundred and thirty one
pages.
We are so used to the system that we don't even think about the numbers.
We simply see them and know by instinct what they mean.
But let's break it down.
Each digit has its base.
The further to the left it is, the more significant it is.
It means it carries more weight of the number eight.
Here means 800 free means 30.
Why one simply means one.
We could mark it digit with an index counting them from right to left.
Now, for each of the digits, we want to calculate 10 to the power of the index multiplied by the digit
itself.
The sum of those numbers is the final number we want to represent.
Let's make sure of that eight times 100 plus three times 10 plus one times one is 831.
Also, remember that the number two the power of zero is one.
No, it is clear why numbers most to the left are most significant because we multiplied by 10 to the
largest power rate.
We now understand exactly how did this small number system works.
Let's move on to the binary number system.
It actually works almost the same.
The only difference is the base of the system.
It will not be done, but two.
Let's consider this number this time.
You probably don't feel what this number means, but don't worry, we'll figure it out in a second.
Let's start with the same as before by marking each digit with its index starting from the right in
the decimal number system.
We calculated the powers of 10 and then multiplied them by the digit itself.
Here it's the same, but we calculate the powers of two.
Let's calculate the sum it's eight plus four plus zero plus one would be 13.
This means one one zero one in the binary number system is 13.
Did this among?
No system, right?
This gives us the basics that are needed to understand some operations related to programming.
The important thing to realize is that on a limited number of beats, we can store unlimited number.
It's actually the same with the decimal number system.
For example, the largest number we can represent with three digits is nine hundred ninety nine in the
binary number system.
Let's consider for beats.
In this case, the largest number that can be represented is 15, because if each bit is set one, then
the number is eight plus four plus two plus one, which is 15.
Each numeric type in C-sharp occupies a certain number of bits in the memory.
For example, an integer takes 42 bits.
The largest number we can represent with end is this, which is only two more than two billion.
And here is something interesting.
This number is actually two to the power of 31, not 42.
So what happened with one bit?
Well, remember that with integers, we can also represent negative numbers.
This one bit is saved to store information, whether the number is negative or not.
Which leaves us 31 bits for the actual number.
Here are sizes and ranges of the integral numeric types used in C-sharp.
There is one more thing that we must understand since each numeric type because its size limit and it
simply can't represent a number that is larger.
What happens when some arithmetic operation exceeds this limit in such situations?
Something quite interesting happens.
For example, if I add two billion to two billion when operating on IANS, I will get this result in
this lecture.
I will explain to you how it works.
But in the next one, we'll learn how to handle such situations when programming before we can understand
what happens and why.
The result is negative.
We must understand how this binary numbers work.
But as before?
Let's start with decimal numbers for simplicity.
You probably know this technique of adding numbers.
If not, please read the article I linked in the resources attached to this lecture with binary numbers.
It works the same.
Let are binary far-reaching to binary.
15.
Remember, 13 is one one zero one and 15 is one one one one.
First, we add the numbers from the first column to the right.
One plus one is two, but we can't use two in the binary number system.
This means we need to carry it over to.
The next column will write zero in the first column of the result, because the module of the sum we
calculate that would was to enter the base of the system, which also is to is zero.
Now the second column again, the sum is two, so we carry over to the next goal.
Again, the third column.
Now this one is free.
We carry over one to the next go on and we leave one in the result.
This is because the modulo of the sum we calculated wood was free and the base of the system, which
is two, is one.
Finally, the Forbes.com: The sum is free again.
So we carry over one and we leave one in the result.
It turned out that we actually needed the fifth column to fit the one that we carried from the fourth
column.
This time it's simple.
The sum is one, and we added to the result.
All right, we have our result.
It's 16 plus eight plus four plus zero zero.
What is 28?
This is correct because 13 plus 15 is also 28.
But notice a very important thing.
We needed to use one more digit to represent this number.
Now let's go back to thinking about computers.
If we had a numeric type which only has four beats, it would simply not be able to hold the result
we had.
So what would happen?
Well, the last most significant bit would just be discarded as the actual result.
The computer could see would not be one one one zero zero, which is 28, but one one zero zero, which
is 12, something completely different and simply wrong from the arithmetic point of view.
Now you know what adding two billion to two billion gave some weird number before.
If you are curious why it was negative.
Remember that the most significant bit represents a sign.
So if it happens to be one, then C-sharp will interpret the whole number as something.
Latif, because in this beat zero means positive numbers and one means negative numbers.
All right.
This lecture was touching very low level topics, but now you understand the basics of the binary number
system.
In the next lecture, we'll talk about how it affects our everyday programming.
During the interview, you can be asked, for example, what is the decimal representation of number
one zero one?
Well, it's five, because it's two to the power of zero plus two to the power of two, which gives
one plus four Woody's five.
Why arithmetic operations in programming can give unexpected results, like, for example, adding two
large integers can give a negative number because there is a limited number of bits reserved for each
numeric type.
For example, for integer, it's 32 bits.
If the result of the arithmetic operation is so large that it doesn't fit on this amount of bits, some
of the bits of the result will be trimmed, giving an unexpected result that is not right.
All right.
That's it for this lecture.
Thanks for watching and see you in the next one.