关于字节顺序的转换 -- linux-2.4.26\include\linux\byteorder

在处理网络数据包的时候,牵涉到字节顺序的变换,下边是源代码,很清晰的表示了怎么进行大端小端的字节转换。主要是在编程中要知道这几个宏定义的意义。

#ifndef _LINUX_BYTEORDER_SWAB_H
#define _LINUX_BYTEORDER_SWAB_H

/*
 * linux/byteorder/swab.h
 * Byte-swapping, independently from CPU endianness
 *    swabXX[ps]?(foo)
 *
 * Francois-Rene Rideau <[email protected]> 19971205
 * separated swab functions from cpu_to_XX,
 * to clean up support for bizarre-endian architectures.
 *
 * See asm-i386/byteorder.h and suches for examples of how to provide
 * architecture-dependent optimized versions
 *
 */


/* casts are necessary for constants, because we never know how for sure
 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
 */

#define ___swab16(x) \
({  \
    __u16 __x = (x); \
    ((__u16)( \
        (((__u16)(__x) & (__u16)0x00ffU) << 8) | \
        (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
})

#define ___swab24(x) \
({  \
    __u32 __x = (x); \
    ((__u32)( \
        ((__x & (__u32)0x000000ffUL) << 16) | \
         (__x & (__u32)0x0000ff00UL) | \
        ((__x & (__u32)0x00ff0000UL) >> 16) )); \
})

#define ___swab32(x) \
({  \
    __u32 __x = (x); \
    ((__u32)( \
        (((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
        (((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \
        (((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \
        (((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
})

#define ___swab64(x) \
({  \
    __u64 __x = (x); \
    ((__u64)( \
        (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
        (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
        (__u64)(((__u64)(__x) 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值