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:

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.