Created
December 22, 2023 10:28
-
-
Save x0nu11byt3/c470ad9032e0989ce72d5d94918d8bbe to your computer and use it in GitHub Desktop.
In c, you could subtract 1 and do a bitwise-and. to make the calculation equivalent to the modulo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main ( int argc, char** argvs ) { | |
int a = -127; | |
int b = 4; | |
int mod_ = a % b; | |
int mod_alt_a = a - b * ( a / b ); | |
// mod_alt works as alternative to modulo | |
int mod_alt = a & ( b - 1 ); | |
printf(" mod_alt: %d \n", mod_alt); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment