Let's say I have the number X and I want to see if it is divisible by Y. What would be the most optimized way to do this?
So far I have:
int X = 12;
int Y = 4;
(X ^ Y) & 0b111 ==0 # Check if X XOR Y (mask size Y) == 0
Though I'm hardcoding 0b111
(the mask size of Y). By the way, I don't care about the language, I'm just tagging this with C.
By the way, using Compiler Explorer I get:
int is_divisible_by(int x, int y) {
return x % y == 0;
};
# -O3
is_divisible_by:
movl %edi, %eax
cltd
idivl %esi # seems to just be doing straight division?
xorl %eax, %eax
testl %edx, %edx
sete %al
ret