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;

  • 相关阅读:
    Android批量插入数据库提升速度(9.9)
    Android中database所在文件夹路径(9.6)
    Eclipse更改默认工作环境编码为UTF-8(9.6)
    Android下Sqlite的使用(9.7)
    Android下ListView的分页(9.6)
    【转】Tarjan算法 资料合集
    【转】BYV--有向图强连通分量的Tarjan算法
    Codeforces Round #403---C题(DFS,树)
    codeforces#403—B题(二分,三分)
    【转】毛虫算法——尺取法
  • 原文地址:https://www.cnblogs.com/banwhui/p/4754089.html
Copyright © 2011-2022 走看看