I’ve been crunching through Kernighan and Ritchie “The C Programming Language” and read that x mod y == x & y-1. So, I worked it out with pencil and paper, and worked fine, so then I tried to test it and here is the problem:
Code:
#include <stdio.h>
main()
{
int i, j;
for(i = 1; i < 10; i++){
for(j = 1; j < 10; j++)
printf("%3d",i & j-1);
printf("\n");
}
}
Gives the output:
0 1 0 1 0 1 0 1 0
0 0 2 2 0 0 2 2 0
0 1 2 3 0 1 2 3 0
0 0 0 0 4 4 4 4 0
0 1 0 1 4 5 4 5 0
0 0 2 2 4 4 6 6 0
0 1 2 3 4 5 6 7 0
0 0 0 0 0 0 0 0 8
0 1 0 1 0 1 0 1 8
and
#include <stdio.h>
main()
{
int i, j;
for(i = 1; i < 10; i++){
for(j = 1; j < 10; j++)
printf("%3d",i % j);
printf("\n");
}
}
gives the output:
0 1 1 1 1 1 1 1 1
0 0 2 2 2 2 2 2 2
0 1 0 3 3 3 3 3 3
0 0 1 0 4 4 4 4 4
0 1 2 1 0 5 5 5 5
0 0 0 2 1 0 6 6 6
0 1 1 3 2 1 0 7 7
0 0 2 0 3 2 1 0 8
0 1 0 1 4 3 2 1 0
Notice, the only change was the % that became &.
Any input would be much appreciated
The equation
is only correct for
ya power of 2.If
y = 2^k, the binary representaion ofyis one 1-bit followed byk0-bits (and preceded by a number of 0-bits depending on the width of the type), and the representation ofy-1isk1-bits (preceded by 0s).Then if you write
x = q*y + rwith0 <= r < y, the binary representation ofrneeds at mostkbits, and the lastkbits ofq*yare all 0, so the remainder ofxmoduloyconsists of the least significantkbits ofx, which are obtained by the bitwise and withy-1.For an odd
y > 1,y-1is even, and sox & y-1is always even, hence(y+1) % y == 1 != (y+1) & (y-1). For evenynot a power of 2, replace 1 with the power of 2 corresponding to the lowest set bit ofyiny+1.