C语言:记录32bit数据的一些常用位操作

最近协议里面用到了这个东西,每次都要重新写,还怕出错,特记录之:

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


#define GET_LOW_BYTE0(x)        ((x >> 0) & 0x000000FF)    /* 获取第0个字节 */
#define GET_LOW_BYTE1(x)        ((x >> 8) & 0x000000FF)    /* 获取第1个字节 */
#define GET_LOW_BYTE2(x)        ((x >> 16) & 0x000000FF)   /* 获取第2个字节 */
#define GET_LOW_BYTE3(x)        ((x >> 24) & 0x000000FF)   /* 获取第3个字节 */

#define GET_BIT(x, bit)         ((x & (1 << bit)) >> bit)   /* 获取某个bit位 */

#define CLEAR_LOW_BYTE0(x)      (x & 0xFFFFFF00)            /* 清零第0个字节 */
#define CLEAR_LOW_BYTE1(x)      (x & 0xFFFF00FF)            /* 清零第1个字节 */
#define CLEAR_LOW_BYTE2(x)      (x & 0xFF00FFFF)            /* 清零第2个字节 */
#define CLEAR_LOW_BYTE3(x)      (x & 0x00FFFFFF)            /* 清零第3个字节 */

#define CLEAR_BIT(x, bit)       (x & (~(1 << bit)))         /* 清零某个bit位 */

#define SET_LOW_BYTE0(x)        (x | 0x000000FF)            /* 第0个字节置1 */
#define SET_LOW_BYTE1(x)        (x | 0x0000FF00)            /* 第1个字节置1 */
#define SET_LOW_BYTE2(x)        (x | 0x00FF0000)            /* 第2个字节置1 */
#define SET_LOW_BYTE3(x)        (x | 0xFF000000)            /* 第3个字节置1 */

#define SET_BIT(x, bit)         (x | (1 << bit))             /* 将某个bit位置为1 */

#define BIT_M_TO_N(x, m, n)     ((unsigned int)(x << (31-(n))) >> ((31 - (n)) + (m))) /* 获取第[n:m]位的值 */

int main()
{

    // 获取单字节
    unsigned int a = 0x12345678;

    printf("0x%x byte0: 0x%x\n", a, GET_LOW_BYTE0(a));
    printf("0x%x byte1: 0x%x\n", a, GET_LOW_BYTE1(a));
    printf("0x%x byte2: 0x%x\n", a, GET_LOW_BYTE2(a));
    printf("0x%x byte3: 0x%x\n", a, GET_LOW_BYTE3(a));
    printf("---------------------------------------\n");

    // 获取某一bit位
    unsigned int b = 0x68; /* 二进制:01101000b */

    printf("0x%x bit0: %d\n", b, GET_BIT(b, 0));
    printf("0x%x bit1: %d\n", b, GET_BIT(b, 1));
    printf("0x%x bit2: %d\n", b, GET_BIT(b, 2));
    printf("0x%x bit3: %d\n", b, GET_BIT(b, 3));
    printf("0x%x bit4: %d\n", b, GET_BIT(b, 4));
    printf("0x%x bit5: %d\n", b, GET_BIT(b, 5));
    printf("0x%x bit6: %d\n", b, GET_BIT(b, 6));
    printf("0x%x bit7: %d\n", b, GET_BIT(b, 7));
    printf("---------------------------------------\n");

    // 清零某个字节
    unsigned int c = 0x12345678;

    printf("0x%x clear byte0: 0x%08x\n", c, CLEAR_LOW_BYTE0(c));
    printf("0x%x clear byte1: 0x%08x\n", c, CLEAR_LOW_BYTE1(c));
    printf("0x%x clear byte2: 0x%08x\n", c, CLEAR_LOW_BYTE2(c));
    printf("0x%x clear byte3: 0x%08x\n", c, CLEAR_LOW_BYTE3(c));
    printf("---------------------------------------\n");


    // 清零某一bit位
    unsigned int d = 0x68; /* 二进制:01101000b */
    printf("0x%x clear bit3: 0x%x\n", d, CLEAR_BIT(d, 3));
    printf("---------------------------------------\n");


    // 置某个字节为1
    unsigned int e = 0x12345678; 
    printf("0x%x set byte0 to 1: 0x%08x\n", e, SET_LOW_BYTE0(e));
    printf("0x%x set byte1 to 1: 0x%08x\n", e, SET_LOW_BYTE1(e));
    printf("0x%x set byte2 to 1: 0x%08x\n", e, SET_LOW_BYTE2(e));
    printf("0x%x set byte3 to 1: 0x%08x\n", e, SET_LOW_BYTE3(e));
    printf("---------------------------------------\n");

    // 将某个bit位置为1
    unsigned int f = 0x68; /* 二进制:01101000b */
    printf("0x%x set bit2 to 1: 0x%x\n", f, SET_BIT(f, 2));
    printf("---------------------------------------\n");


    // 判断某一位的值是否为1
    unsigned int g = 0x68; /* 二进制:01101000b */
    if (g & (1 << 3))
    {
        printf("0x68's bit3 is 1\n");
    }
    else 
    {
        printf("0x68's bit3 is 0\n");
    }
    printf("---------------------------------------\n");


    unsigned int h = 0x68; /* 二进制:01101000b */
    printf("0x68's [3:2] bit range value: 0x%08x\n", BIT_M_TO_N(h, 2, 3));

    return 0;
}

运行:

0x12345678 byte0: 0x78
0x12345678 byte1: 0x56
0x12345678 byte2: 0x34
0x12345678 byte3: 0x12
---------------------------------------
0x68 bit0: 0
0x68 bit1: 0
0x68 bit2: 0
0x68 bit3: 1
0x68 bit4: 0
0x68 bit5: 1
0x68 bit6: 1
0x68 bit7: 0
---------------------------------------
0x12345678 clear byte0: 0x12345600
0x12345678 clear byte1: 0x12340078
0x12345678 clear byte2: 0x12005678
0x12345678 clear byte3: 0x00345678
---------------------------------------
0x68 clear bit3: 0x60
---------------------------------------
0x12345678 set byte0 to 1: 0x123456ff
0x12345678 set byte1 to 1: 0x1234ff78
0x12345678 set byte2 to 1: 0x12ff5678
0x12345678 set byte3 to 1: 0xff345678
---------------------------------------
0x68 set bit2 to 1: 0x6c
---------------------------------------
0x68's bit3 is 1
---------------------------------------
0x68's [3:2] bit range value: 0x00000002

 

上一篇:DSP STM32 ARM 51单片机 比较


下一篇:Java生成64bit、32bit的ID