"pin = 1 << p"

i quote:
" //this is line uses a bitwise operator
//shifting a bit left using << is the same
//as multiplying the decimal number by two.
pin = 1<< p;"
from: http://arduino.cc/en/Tutorial/ShftOut12

Say p was equal to 2, then what is pin equal to? i dont understand what the << is doing.

Edit: furthermore what is a bitwise operator, and what does it mean by shifting a bit left.

http://www.arduino.cc/playground/Code/BitMath

thanks,

from that link it means:

pin = 1 << 1 is 1

pin = 1 << 2 is 4

pin = 1 << 3 is 8
etc..
does anyone understand why it isnt just pin 1, 2, 3, 4, 5 ,6, 7,etc?

it looks you never heard of binary...
here are the binary translation of the value you mantionned... hope you'll see the action of the shift operator...

0x01 = 00000001b
0x02 = 00000010b
0x04 = 00000100b
0x08 = 00001000b
0x10 = 00010000b
...
and for the reference 3 and let's say 5 would be coded like that:
0x03 = 00000011b
0x05 = 00000101b

if you don't know binary it might be a good idea to read the basic there:

take also the care to learn about two complement, that'll avoid you some painful problems with signed value...

from that link it means:

pin = 1 << 1 is 1

No, "1 << 1" is 2.

"1 << 0" is 1.

No, "1 << 1" is 2.

"1 << 0" is 1.

good catch, missed that :slight_smile:

In general, "x << y" can be rewritten as

x * 2y

so, 5 << 3 = 5 * 23 = 5 * 8 = 40

An old programming trick to do an integer multiplication by ten is to shift the multiplicand left once, store the result in a temporary, then shift the result twice more (three shifts total = multiply by 8) , then add in the temporary.