Skip to content

Instantly share code, notes, and snippets.

@x0nu11byt3
Created December 22, 2023 10:28
Show Gist options
  • Save x0nu11byt3/c470ad9032e0989ce72d5d94918d8bbe to your computer and use it in GitHub Desktop.
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.
#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