Bitwise counting


#1

Hi,

I'm kinda stuck at the chapter "Introduction to Bitwise Operators", but I don't understand how this bitwise counting works.
I tried to search myself in the internet, but couldn't find anything (probably didn't look properly and got lazy :stuck_out_tongue:)
Would someone be so kind and give me a link (if that's allowed of course) or even explain it himself.

I really appreciate your help :smiley:


#2

We are given some completed examples to work with:

one = 0b1
two = 0b10
three = 0b11
four 
five 
six 
seven 
eight 
nine 
ten 
eleven 
twelve

Note that there no quotes on the values, only the prefix, 0b that tells the compiler to treat these as binary numbers.

0b1 is obviously 1 (one). Since there are only two digits in the binary number system, we can only work with 1's and 0's to represent our numbers.

Study the table in the instructions to see how the values of each bit double as we move to the left.

0b1 shifted left, gives, 0b10, which is 2 * 1, or 2 to the first power. Shift to the left again, we get, 0b100, or 4, which is 2 * 2, or 2 to the second.

Just as in decimal, a left shift multiplies the digit by 10, in binary it is 2.

The number 12 is less than 16, so will be represented in 4 bits or less.

2^3 + 2^2 + 2^1 + 2^0 == 8 + 4 + 2 + 1 == 15 == 0b1111
 8  +  4  +  0  +  0  == 12 == 0b1100

You should be able to complete this list with this knowledge in hand.


#3

Thank you very much! :slightly_smiling:


#4