Binary Operators, Base 10, binary and conversion

Question:

What is int a = 1 << 10;

What is int a = 1024 >> 10;

Concept explanation:

These binary operator works on the birary representation of the value. In short, it allow you to manipulate the binary value to obtains result quicker by manipulation the position of each digits. If you have never came any of this concept before, or even if you know, here is a quick reminder.

Humans count in base 10, decimal system. Some will argue that is because we have 10 fingers and that it made things easier for counting, but I will leave the matter for the Internet debate.

Computers uses the same approach as us, but instead of using base 10, it uses a base 2 approach, meaning that each position into the digit represent either 1 or 2.

Let's say you have the value 115 in decimal.
It means : 100 + 10 + 5
1 into the hundred means 100, (1 * 100)
1 into the 10 means 10, (1 * 10)
5 into the 1 means 5 (1 * 5)

Let's say we take the number 119, and want to add 1 to the value... Technically we cannot add 1 to the 1 position, because 9 is the limit, so naturally, you would add 1 to the 10th value to turn it into 120.

When we move to binary, it would be a bit easier to see with exponent...

So lets assume that every position of the digit represent the exponent of the base
(base exp(position) * value) = value at the position.

10³ 10² 10¹ 10⁰   ------> Our base 10 exponent position.
5   7   1   1     ------> Our number in base 10.

So you would add up all those numbers as follow:
(10³ * 5) + (10² * 7) + (10¹ * 1) + (10⁰ * 1) = 5711

Moving into base two

When teaching programming or any thing related to binary, I always start with this technique as it allow pretty much anyone to understand with a proper things we use in our daily lives. Everyone can look at 5854 and not even think about what it means under the hood. The representation of the number is baked into our brain as we grew up with base10, we count with base10, we receive money with base10, with calculate our shoppings in base10, we look at the speed we drive or fly in base10. So it is normal it has become a second nature for us.

The thing is that binary number are not very more complicated than this, while some might think that the zeros and ones makes it all complex. Really it is not, it is the same concept as we saw before but instead of using the base 10 we use the base 2.

If you want to add 1 to 9 (9 + 1) --- You will run out of spaces in the digits positions, so you add one position on the left and voila: you have your 10 and you can start adding to 19 before adding a +1 to the 10th position.

A binary only takes 0 or 1. So every position can hold 2 values, either 1 or 0, on or off, turn off or turn on...

In base 2,
0 = 0
1 = 1
Oups, we have run out of places, so we need to add another digits to the left
10 would be 3 in base 10. And so on.

Following our previous example: lets take a random base2 number
10101

Once again we dissect it with the same approach we did for base 10.

2⁴ 2³ 2²  2¹ 2⁰	  ------> Our base 2 exponent position.
1  0  1   0  1    ------> Our number in base2.

And then we do:
((2⁴ * 1) + (2³ * 0) + (2² * 1) (2¹ * 0) + (2⁰ * 1)) =
(16) + 0 + 4 + 1 = 21

Binary shifting operators

So to get back into our main problem.
What is

int a = 1 << 10

Means that we take 1 in binary representation and switch all the bits 10 position to the left.

Therefore:
1 << 10 = 1000000000

In short we can say that
1000000000 = (2¹⁰ * 1) = 1024 in decimal.

The right shift operator does the same thing, but in the other direction.

So in our example of 1024 >> 10.
We take the value of 1024 in base2(binary) and move the digits by 10 units to the right.

1000000000 >> 10 = 0000000001 = 1 in decimal.

More infos

Pikuma content on this matter is more in depth and incredbibly well explained CODE by Charles Petzold(Chapter 3-4)