Test if a number is even
ubuntuincident.wordpress.com
Test if a number is even
1–10 of 68 posts
Re: Test if a number is even
#2You don't have to guess, you could turn or O3 or look at the disassembly.
Re: Test if a number is even
#3Re: Test if a number is even
#4I like the algorithms that are the opposite of this where they try to find the slowest possible way to determine if a number is even.
Re: Test if a number is even
#5> I think the optimizer recognizes modulo 2 and converts it to bitwise AND. You don't have to guess, you could turn or O3 or look at the disassembly.
Re: Test if a number is even
#6For instance if you're making a loop to count the bits that are set in a number, the compiler can recognize the entire loop and turn it into a single popcnt instruction (e.g. https://lemire.me/blog/2016/05/23/the-surprising-cleverness-... )
Re: Test if a number is even
#7I like the algorithms that are the opposite of this where they try to find the slowest possible way to determine if a number is even.
Re: Test if a number is even
#8> I think the optimizer recognizes modulo 2 and converts it to bitwise AND. You don't have to guess, you could turn or O3 or look at the disassembly.
Re: Test if a number is even
#9I like the algorithms that are the opposite of this where they try to find the slowest possible way to determine if a number is even.
Re: Test if a number is even
#10A quick check in the compiler explorer (godbolt.org) confirms that this is indeed true for GCC on x86_64 and aarch64, but not for clang on the same (clang does optimize it with -O3).