求余数 12%8=4
n%m也能计算出余数,但效率可能比位操作要低一些
/*************************************************************//** Calculates fast the remainder of n/m when m is a power of two. @param n in: numerator @param m in: denominator, must be a power of two @return the remainder of n/m */ #define ut_2pow_remainder(n, m) ((n) & ((m) - 1))
#include <stdio.h> #include <stdlib.h> /*************************************************************//** Calculates fast the remainder of n/m when m is a power of two. @param n in: numerator @param m in: denominator, must be a power of two @return the remainder of n/m */ #define ut_2pow_remainder(n, m) ((n) & ((m) - 1)) int main(){ int n=12; int m=8; int a; a=ut_2pow_remainder(n,m); printf("%d",a); return 0; }
时间: 2024-10-16 01:55:01