Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Any reasonable compiler will implement x%256 using bitwise arithmetic, but C semantics require that x == d*(x/d) + (x%d), which means that if x is negative (x%256) must be negative, while (x&0xff) will be positive.

This difference means that the AND is still faster than the MOD when used on signed integers, if the compiler is not smart enough to prove that the input will never be negative.

For example, with gcc 4.7.3 the resulting assembly is

  movl %edi, %edx
  sarl $31, %edx
  shrl $24, %edx
  leal (%rdi, %rdx), %eax
  andl $255, %eax
  subl %edx, %eax
I.e., six instructions instead of one. This is still faster than using a divide.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: