C语言高低字节转换
#include
#include
#include
#define _G_HI24_BYTE(x) (((x) >> 24) & 0xFF)
#define _G_HI16_BYTE(x) (((x) >> 16) & 0xFF)
#define _G_HI8_BYTE(x) (((x) >> 8) & 0xFF)
#define _G_HI_BYTE(x) ((x) & 0xFF)
#define GetBit(Number,pos) ((Number >> pos) & 1)
#define SetBit(Number,pos) ((Number) |= (1<<pos))
#define ClrBit(Number,pos) ((Number) &=~(1<<pos))
#define FanBit(Number,pos) ((Number) ^= (1<<pos))
using namespace std;
int main1();
int main()
{
uint16_t checksum = 257;
uint8_t ck[2] = {0};
ck[0] = (uint8_t)(checksum & 0xFF);
ck[1] = (uint8_t)(checksum >> 8);
printf("ck[0]:%d ck[1]:%d\n", ck[0], ck[1]);
ck[0] = _G_HI_BYTE(checksum);
ck[1] = _G_HI8_BYTE(checksum);
printf("_G_HI_BYTE ck[0]:%d ck[1]:%d\n", ck[0], ck[1]);
ck[0] = 0x01;
ck[1] = 0x01;
uint16_t checksum1 = ck[0]|ck[1]<<8;
printf("checksum1:%d\n", checksum1);
uint16_t checksum2 = ck[0] + (ck[1]<<8);
printf("checksum2:%d\n", checksum2);
uint8_t num = 0xAA;
for(int i = 0; i < 8; i++)
{
printf("GetBit%d:%x\n", i, GetBit(num,i));
}
num = 0;
for(int i = 0; i < 8; i++)
{
printf("ClrBit%d:%x\n", i, SetBit(num,i));
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67