码迷,mamicode.com
首页 > 其他好文 > 详细

宏ut_2pow_remainder

时间:2015-11-23 12:57:59      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

求余数 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;
}

 

宏ut_2pow_remainder

标签:

原文地址:http://www.cnblogs.com/taek/p/4987956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!