I was making a project for a client of mine where I had to make a list with two different colors for the items (like the iTunes list) so I started to think of a possible way to make this. It was clear enough I need to know the odd or the even number in order to do that. So I started to do some google search and I got over this blogpost by Keith Peters. I have to say, this is a very interesting way of thinking 😀
“iseven = ((num & 1) == 0)
Makes sense when you look at binary numbers:
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
Each of the odd numbers has a 1 in the far right column. The even numbers have 0 there. Mask it with “& 1″ and you can see which it is.”
So I made a for loop and used this code. It works exactly how it was intended to work!!
Thanks Keith Peters for your blogpost 😀
you might check out this post from grant skinner also.
http://www.gskinner.com/blog/archives/2008/05/core_as3_modulu.html
Yes, but Keith Peters says it’s a little faster with ((num & 1) == 0) instead of ((num % 2) == 0) 😉
Yes, you can use bitwise for a lot of things…
Like finding the modulo, dividing by 2.
Can you guess what this piece of code does?
a=a^b;
b=b^a;
a=a^b;
Based on one of the xor’s property 🙂
I have no idea 🙂
I guess the num%2=0 has been around for more than 10 years now. I remember doing this in C++ back in school.