在C中将大端点转换为小端点[不使用提供的函数]在C中,我需要编写一个函数来将大端点转换为小端点。我不能使用任何库函数。
3 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
swapped = (num>>8) | (num<<8);
swapped = ((num>>24)&0xff) | // move byte 3 to byte 0 ((num<<8)&0xff0000) | // move byte 1 to byte 2 ((num>>8)&0xff00) | // move byte 2 to byte 1 ((num<<24)&0xff000000); // byte 0 to byte 3
0xdeadbeef
0xefbeadde
.
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
#include <byteswap.h>
__bswap_32 (uint32_t input)
__bswap_16 (uint16_t input)
守着一只汪
TA贡献1872条经验 获得超3个赞
#include <stdint.h>//! Byte swap unsigned shortuint16_t swap_uint16( uint16_t val ) { return (val << 8) | (val >> 8 );}//! Byte swap shortint16_t swap_int16( int16_t val ) { return (val << 8) | ((val >> 8) & 0xFF);}//! Byte swap unsigned intuint32_t swap_uint32( uint32_t val ){ val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF ); return (val << 16) | (val >> 16);}//! Byte swap intint32_t swap_int32( int32_t val ){ val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF ); return (val << 16) | ((val >> 16) & 0xFFFF);}
更新
int64_t swap_int64( int64_t val ){ val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL ); val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL ); return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);}uint64_t swap_uint64( uint64_t val ){ val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL ); val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL ); return (val << 32) | (val >> 32);}
- 3 回答
- 0 关注
- 699 浏览
添加回答
举报
0/150
提交
取消