zoukankan      html  css  js  c++  java
  • C语言:记录32bit数据的一些常用位操作

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

    #include <stdio.h>
    #include <string.h>
    
    #define GET_INT_LOW_BYTE0(x) ((x >> 0) & 0x000000FF)            /* GET BYTE 0 */
    #define GET_INT_LOW_BYTE1(x) ((x >> 8) & 0x000000FF)            /* GET BYTE 1 */
    #define GET_INT_LOW_BYTE2(x) ((x >> 16) & 0x000000FF)           /* GET BYTE 2 */
    #define GET_INT_LOW_BYTE3(x) ((x >> 24) & 0x000000FF)           /* GET BYTE 3 */
    
    #define GET_BIT(x, bit) ((x & (1 << bit)) >> bit)               /* GET BIT N */
    
    #define CLEAR_INT_LOW_BYTE0(x) (x &= 0xFFFFFF00)                /* CLEAR BYTE 0 */
    #define CLEAR_INT_LOW_BYTE1(x) (x &= 0xFFFF00FF)                /* CLEAR BYTE 1 */
    #define CLEAR_INT_LOW_BYTE2(x) (x &= 0xFF00FFFF)                /* CLEAR BYTE 2 */
    #define CLEAR_INT_LOW_BYTE3(x) (x &= 0x00FFFFFF)                /* CLEAR BYTE 3 */
    
    #define CLEAR_BIT(x, bit) (x &= (~(1 << bit)))                  /* CLEAR BIT N */
    
    #define SET_INT_LOW_BYTE0(x) (x |= 0x000000FF)                  /* SET BYTE 0 TO 1 */
    #define SET_INT_LOW_BYTE1(x) (x |= 0x0000FF00)                  /* SET BYTE 1 TO 1 */
    #define SET_INT_LOW_BYTE2(x) (x |= 0x00FF0000)                  /* SET BYTE 2 TO 1 */
    #define SET_INT_LOW_BYTE3(x) (x |= 0xFF000000)                  /* SET BYTE 3 TO 1 */
    
    #define SET_BIT(x, bit) (x |= (1 << bit))                       /* SET BIT N TO 1 */
    
    #define GET_INT_BIT_M_TO_N(x, m, n) ((unsigned int)(x << (31 - (n))) >> ((31 - (n)) + (m))) /* GET BIT M~N */
    
    int main()
    {
    
        // 获取单字节
        printf("------------------- Get single byte --------------------
    ");
        unsigned int a = 0x12345678;
    
        printf("0x%x byte0: 0x%x
    ", a, GET_INT_LOW_BYTE0(a));
        printf("0x%x byte1: 0x%x
    ", a, GET_INT_LOW_BYTE1(a));
        printf("0x%x byte2: 0x%x
    ", a, GET_INT_LOW_BYTE2(a));
        printf("0x%x byte3: 0x%x
    ", a, GET_INT_LOW_BYTE3(a));
    
        // 获取某一bit位
        printf("-------------------- Get a bit -------------------
    ");
        unsigned int b = 0x68; /* 二进制:01101000b */
    
        printf("0x%x bit0: %d
    ", b, GET_BIT(b, 0));
        printf("0x%x bit1: %d
    ", b, GET_BIT(b, 1));
        printf("0x%x bit2: %d
    ", b, GET_BIT(b, 2));
        printf("0x%x bit3: %d
    ", b, GET_BIT(b, 3));
        printf("0x%x bit4: %d
    ", b, GET_BIT(b, 4));
        printf("0x%x bit5: %d
    ", b, GET_BIT(b, 5));
        printf("0x%x bit6: %d
    ", b, GET_BIT(b, 6));
        printf("0x%x bit7: %d
    ", b, GET_BIT(b, 7));
    
        // 清零某个字节
        printf("-------------------- clear single byte -------------------
    ");
        unsigned int c1 = 0x12345678;
        unsigned int c2 = 0x12345678;
        unsigned int c3 = 0x12345678;
        unsigned int c4 = 0x12345678;
    
        printf("0x12345678 clear byte0: 0x%08x
    ", CLEAR_INT_LOW_BYTE0(c1));
        printf("0x12345678 clear byte1: 0x%08x
    ", CLEAR_INT_LOW_BYTE1(c2));
        printf("0x12345678 clear byte2: 0x%08x
    ", CLEAR_INT_LOW_BYTE2(c3));
        printf("0x12345678 clear byte3: 0x%08x
    ", CLEAR_INT_LOW_BYTE3(c4));
    
        // 清零某一bit位
        printf("-------------------- clear single byte -------------------
    ");
        unsigned int d = 0x68; /* 二进制:01101000b */
    
        printf("0x68 clear bit3: 0x%x
    ", CLEAR_BIT(d, 3));
    
        // 置某个字节为1
        printf("---------------------- set one byte to 1-----------------
    ");
        unsigned int e1 = 0x12345678;
        unsigned int e2 = 0x12345678;
        unsigned int e3 = 0x12345678;
        unsigned int e4 = 0x12345678;
    
        printf("0x12345678 set byte0 to 1: 0x%08x
    ", SET_INT_LOW_BYTE0(e1));
        printf("0x12345678 set byte1 to 1: 0x%08x
    ", SET_INT_LOW_BYTE1(e2));
        printf("0x12345678 set byte2 to 1: 0x%08x
    ", SET_INT_LOW_BYTE2(e3));
        printf("0x12345678 set byte3 to 1: 0x%08x
    ", SET_INT_LOW_BYTE3(e4));
    
        // 将某个bit位置为1
        printf("----------------------- set bit to 1 ----------------
    ");
        unsigned int f = 0x68; /* 二进制:01101000b */
    
        printf("0x68 set bit2 to 1: 0x%x
    ", SET_BIT(f, 2));
    
        // 判断某一位的值是否为1
        printf("------------------------ check whether the value of a bit is 1 ---------------
    ");
        unsigned int g = 0x68; /* 二进制:01101000b */
    
        if (g & (1 << 3))
        {
            printf("0x68's bit3 is 1
    ");
        }
        else
        {
            printf("0x68's bit3 is 0
    ");
        }
    
        // 获取某几位连续的值
        printf("------------------------ get bit range value ---------------
    ");
        unsigned int h = 0x68; /* 二进制:01101000b */
        printf("0x68's [2:3] bit range value: 0x%08x
    ", GET_INT_BIT_M_TO_N(h, 2, 3));
    
        return 0;
    }

    运行:

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

  • 相关阅读:
    如何面试程序员?
    开始做项目
    ===
    依赖注入获得一个对象却想返回不同的值(Error)
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]ResultSet can not reread row data for column 4.
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
    .net 4.5新特性
    有限状态机简单示例
    JavaScript入门经典(第四版)文摘
    小强升职记读后感
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/14519475.html
Copyright © 2011-2022 走看看