Bit shifting is fun : 1st lesson

Found a nice little trick thanks to this sketch. I was studying the code behind it and this line intrigued me:
ellipse(0, 0, halfsize<<1, halfsize<<1);

I knew “<<” meant a bitwise operation, but I only saw it in some processing example about faster color manipulation. First time, I see it in a case like this. So I guessed doing a 1 left bit shift on an integer multiplies it by 2. (Wrote this little thing to test it)


for (int i = 0; i<=10; i++)
println("i= " + i + " i<<1= " + str(i<<1) );

Wikipedia confirms and completes it ( right shifting by 1 bit divides an integer by 2).


Tags: