zoukankan      html  css  js  c++  java
  • __BIG_ENDIAN 和__LITTLE_ENDIAN

    只要通过引入头文件<endian.h>便可以在编译时通过宏判断字节序了

    #if __BYTE_ORDER == __LITTLE_ENDIAN

    #elif __BYTE_ORDER == __BIG_ENDIAN

    #else
    #error "Unknown byte order"
    #endif

    <endian.h>节选

    /* Definitions for byte order, according to significance of bytes,   from low addresses to high addresses.  The value is what you get by   putting '4' in the most significant byte, '3' in the second most   significant byte, '2' in the second least significant byte, and '1'   in the least significant byte, and then writing down one digit for   each byte, starting with the byte at the lowest address at the left,   and proceeding to the byte with the highest address at the right.  */#define __LITTLE_ENDIAN 1234#define __BIG_ENDIAN    4321#define __PDP_ENDIAN    3412/* This file defines `__BYTE_ORDER' for the particular machine.  */#include <bits/endian.h>

    完整举例

    #include <sys/types.h>
    #include <byteswap.h>
    #include <endian.h>
    #include <stdint.h>
    #include <inttypes.h>

    #if __BYTE_ORDER == __BIG_ENDIAN
    # define cpu_to_le16(x) bswap_16(x)
    # define le16_to_cpu(x) bswap_16(x)
    # define cpu_to_le32(x) bswap_32(x)
    # define le32_to_cpu(x) bswap_32(x)
    # define cpu_to_be16(x) (x)
    # define be16_to_cpu(x) (x)
    # define cpu_to_be32(x) (x)
    # define be32_to_cpu(x) (x)
    # define cpu_to_be64(x) (x)
    # define be64_to_cpu(x) (x)
    #elif __BYTE_ORDER == __LITTLE_ENDIAN
    # define cpu_to_le16(x) (x)
    # define le16_to_cpu(x) (x)
    # define cpu_to_le32(x) (x)
    # define le32_to_cpu(x) (x)
    # define cpu_to_be16(x) bswap_16(x)
    # define be16_to_cpu(x) bswap_16(x)
    # define cpu_to_be32(x) bswap_32(x)
    # define be32_to_cpu(x) bswap_32(x)
    # define cpu_to_be64(x) bswap_64(x)
    # define be64_to_cpu(x) bswap_64(x)
    #else
    #error "unknown endianess!"
    #endif

    /* min()/max() that do strict type-checking. Lifted from the kernel. */
    #define min(x, y) ({
    typeof(x) _min1 = (x);
    typeof(y) _min2 = (y);
    (void) (&_min1 == &_min2);
    _min1 < _min2 ? _min1 : _min2; })

    #define max(x, y) ({
    typeof(x) _max1 = (x);
    typeof(y) _max2 = (y);
    (void) (&_max1 == &_max2);
    _max1 > _max2 ? _max1 : _max2; })

    /* ... and their non-checking counterparts. */
    #define min_t(type, x, y) ({
    type _min1 = (x);
    type _min2 = (y);
    _min1 < _min2 ? _min1 : _min2; })

    #define max_t(type, x, y) ({
    type _max1 = (x);
    type _max2 = (y);
    _max1 > _max2 ? _max1 : _max2; })

    typedef uint8_t u8;
    typedef uint16_t u16;
    typedef uint32_t u32;
    typedef uint64_t u64;
    typedef int8_t s8;
    typedef int16_t s16;
    typedef int32_t s32;
    typedef int64_t s64;

  • 相关阅读:
    LeetCode
    LeetCode
    一篇真正教会你开发移动端页面的文章(一)
    移动端页面开发资源总结
    Vue 模板
    使用 concurrently 并行地运行多个命令(同时跑前端和后端的服务)
    值得H5前端学习的60个JS插件(含DEMO演示)
    解读浮动闭合最佳方案:clearfix
    JavaScript 如何工作的: 事件循环和异步编程的崛起 + 5 个关于如何使用 async/await 编写更好的技巧
    JavaScript 运行机制详解:再谈Event Loop
  • 原文地址:https://www.cnblogs.com/banwhui/p/4754089.html
Copyright © 2011-2022 走看看