But what I want to know is what is << doing exactly, I read something about moving the binary number
<< is a left shift . From the linked article: "Shifting left by n bits on a signed or unsigned binary number has the effect of multiplying it by 2^n."
but since 1 is 1 in binary wouldn't this make it 1? And then the other 1 would not be 1 000 000? Why is that that it becomes 64?
In your example, the value of bits[i] is always 0 or 1, and i is the position of each bit, so shifting bits[i] left by i will get you the numeric value that bit represents in a byte.
Break it down:
The statement bits[] = {1, 0, 0, 0, 0, 0, 1, 0} is the same as
bits[0] = 1;
bits[1] = 0;
bits[2] = 0;
bits[3] = 0;
bits[4] = 0;
bits[5] = 0;
bits[6] = 1;
bits[7] = 0;
And the loop with character += bits[i] << i; is the same as
char character = 0;
character += bits[0] << 0;
character += bits[1] << 1;
character += bits[2] << 2
character += bits[3] << 3;
character += bits[4] << 4;
character += bits[5] << 5;
character += bits[6] << 6;
character += bits[7] << 7;
Substituting the values of the bits array:
char character = 0;
character += 1 << 0;
character += 0 << 1;
character += 0 << 2
character += 0 << 3;
character += 0 << 4;
character += 0 << 5;
character += 1 << 6;
character += 0 << 7;
Replacing the << operator with its mathematical equivalent (this is now pseudocode):
char character = 0;
character += 1 * 2^0;
character += 0 * 2^1;
character += 0 * 2^2;
character += 0 * 2^3;
character += 0 * 2^4;
character += 0 * 2^5;
character += 1 * 2^6;
character += 0 * 2^7;
Exponentiate:
char character = 0;
character += 1 * 1;
character += 0 * 2;
character += 0 * 4;
character += 0 * 8;
character += 0 * 16;
character += 0 * 32;
character += 1 * 64;
character += 0 * 128;
Multiply:
char character = 0;
character += 1;
character += 0;
character += 0;
character += 0;
character += 0;
character += 0;
character += 64;
character += 0;
Simplify:
char character = 0;
character += 1;
character += 64;
So character = 0 + 1 + 64 = 65.