본문 바로가기

IT Tech/Programming

[C언어] BIT READ/WRITE MACRO

반응형




#define READ_U8(_val, _ptr)  {                     \
    _val = *((unsigned char *) (_ptr));            \
}
#define READ_S8(_val, _ptr)  {                     \
    _val = *((char *) (_ptr));                     \
}
#define READ_U16(_val, _ptr)  {                    \
    _val = *((unsigned short *) (_ptr));           \
}
#define READ_S16(_val, _ptr)  {                    \
    _val = *((short *) (_ptr));                    \
}
#define READ_U24(_val, _ptr)  {                    \
    _val = *((unsigned int *) (_ptr)) >> 8;        \
}
#define READ_S24(_val, _ptr)  {                    \
    _val = *((int *) (_ptr)) >> 8;                 \
}
#define READ_U32(_val, _ptr)  {                    \
    _val = *((unsigned int *) (_ptr));             \
}
#define READ_S32(_val, _ptr)  {                    \
    _val = *((int *) (_ptr));                      \
}

#define WRITE_U8(_ptr, _val)  {               \
    *((unsigned char *) (_ptr)) = _val;       \
}
#define WRITE_S8(_ptr, _val)  {               \
    *((char *) (_ptr)) = _val;                \
}
#define WRITE_U16(_ptr, _val)  {              \
    *((unsigned short *) (_ptr)) = _val;      \
}
#define WRITE_S16(_ptr, _val)  {              \
    *((short *) (_ptr)) = _val;               \
}
#define WRITE_U24(_ptr, _val)  {              \
    *((unsigned int *) (_ptr)) = _val;        \
    *((unsigned int *) (_ptr)) <<= 8;         \
}
#define WRITE_S24(_ptr, _val)  {              \
    *((int *) (_ptr)) = _val;                 \
    *((int *) (_ptr)) <<= 8;                  \
}
#define WRITE_U32(_ptr, _val)  {              \
    *((unsigned int *) (_ptr)) = _val;        \
}
#define WRITE_S32(_ptr, _val)  {              \
    *((int *) (_ptr)) = _val;                 \
}



[EX]
char *input_data = xxx.data;
unsigned short aaa=2;

WRITE_U16(input_data, aaa)
input_data += 2;

....


READ_U16(aaa, input_data);


반응형