Sword 位运算取余操作

/* 位运算取余操作 */

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include <assert.h>

void test1()
{
    unsigned a = 12;
    unsigned b = 7;
    unsigned c = 0;

    c = a % b;

    printf("----[%u]---
", c);
}

void test2()
{
    unsigned a = 12;
    unsigned b = 32;
    unsigned c = 0;

    /*
    设计说明:
        位运算取余必要条件
            要求除数必须是2的幂次方
    */
    c = a & (b - 1);

    printf("----[%u]---
", c);
}

int main()
{
    test2();
    getchar();
    return 0;
}

原文地址:https://www.cnblogs.com/zhanggaofeng/p/11891961.html