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;

  • 相关阅读:
    几种流行Webservice框架性能对比
    java实现的单点登录
    SQL Server性能调优——报表数据库与业务数据库分离
    控制台console输出信息原理理解
    查看程序是否启动或者关闭--比如查看Tomcat是否开启!直接用ps命令查看进程就行了啊
    Tomcat日志问题
    客户端用httpurlconnection来进行http连接的
    一个tomcat上放多个webapp问题,那这多个webapp会不会竞争端口呢?不会!安全两码事
    ps -ef能列出进程号,和端口号没任何关系
    Tomcat打印运行时日志(控制台),访问日志,启动日志
  • 原文地址:https://www.cnblogs.com/banwhui/p/4754089.html
Copyright © 2011-2022 走看看