C语言宏定义的几种简单用法
来源:互联网 发布:音箱测试软件 编辑:程序博客网 时间:2024/05/17 08:29
1、计算数组的大小
#define countof(a) (sizeof(a)/sizeof(*(a)))
2、转换大小写字母
#define FS_TOUPPER(x) ((((x) >= 'a') && ((x) <= 'z')) ? (x) - 'a' + 'A' : (x))#define FS_TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? (x) - 'A' + 'a' : (x))
3、大小端模式转换
#define SWAP16(x) (((x) << 8) & 0xff00) | (((x) >> 8) & 0xff)#define SWAP32(x) ((((uint32_t)(x) >> 24) & 0xff) | (((uint32_t)(x) >> 8) & 0xff00) | (((uint32_t)(x) << 8) & 0xff0000) | (((uint32_t)(x) << 24) & 0xff000000))
4、生成字,双字
#define MAKE_DWORD(b3, b2, b1, b0) (((uint32_t)((uint8_t)(b3) << 24)) | ((uint8_t)(b2) << 16) | ((uint8_t)(b1) << 8) | ((uint8_t)(b0)))#define MAKE_WORD(h, l) (((uint32_t)(h) << 8) | ((l) & 0xffff))
5、获取高,低字节
#define GET_WORD_HIGH(x) ((uint32_t)(x) >> 16)#define GET_WORD_LOW(x) ((uint32_t)(x) & 0xffff)#define GET_WORD_HIGHBYTE(x) ((uint32_t)(x) >> 24)#define GET_WORD_LOWBYTE(x) ((uint32_t)(x) & 0xff)
以上宏定义的示例程序
#include <stdio.h>#include <stdint.h>#define countof(a) (sizeof(a)/sizeof(*(a)))#define FS_TOUPPER(x) ((((x) >= 'a') && ((x) <= 'z')) ? (x) - 'a' + 'A' : (x))#define FS_TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? (x) - 'A' + 'a' : (x))#define SWAP16(x) (((x) << 8) & 0xff00) | (((x) >> 8) & 0xff)#define SWAP32(x) ((((uint32_t)(x) >> 24) & 0xff) | (((uint32_t)(x) >> 8) & 0xff00) | (((uint32_t)(x) << 8) & 0xff0000) | (((uint32_t)(x) << 24) & 0xff000000))#define MAKE_DWORD(b3, b2, b1, b0) (((uint32_t)((uint8_t)(b3) << 24)) | ((uint8_t)(b2) << 16) | ((uint8_t)(b1) << 8) | ((uint8_t)(b0)))#define MAKE_WORD(h, l) (((uint32_t)(h) << 8) | ((l) & 0xffff))#define GET_WORD_HIGH(x) ((uint32_t)(x) >> 16)#define GET_WORD_LOW(x) ((uint32_t)(x) & 0xffff)#define GET_WORD_HIGHBYTE(x) ((uint32_t)(x) >> 24)#define GET_WORD_LOWBYTE(x) ((uint32_t)(x) & 0xff)void test_countof(){ int a[10] = {}; printf("%d\n",countof(a));}void test_FSTOUPPER(){ char a[14] = "This is a test"; char ans_up[14]; char ans_low[14]; for (int i = 0;i<14;i++){ ans_up[i] = FS_TOUPPER(a[i]); printf("%c",ans_up[i]); } printf("\n"); for (int j = 0; j<14; j++){ ans_low[j] = FS_TOLOWER(ans_up[j]); printf("%c",ans_low[j]); }}void test_SWAP(){ uint16_t x = 0x1234; uint32_t y = 0x12345678; printf("%x\n",SWAP16(x)); printf("%x\n",SWAP32(y));}void test_MAKE_WORD(){ uint8_t a = 0x12; uint8_t b = 0x34; uint8_t c = 0x56; uint8_t d = 0x78; printf("%x\n",MAKE_WORD(a,b)); printf("%x\n",MAKE_DWORD(a,b,c,d));}void test_GET_WORD(){ uint32_t a = 0x12345678; printf("%x\n",GET_WORD_HIGH(a)); printf("%x\n",GET_WORD_LOW(a)); printf("%x\n",GET_WORD_HIGHBYTE(a)); printf("%x\n",GET_WORD_LOWBYTE(a));}void main(void) { test_countof(); test_FSTOUPPER(); test_SWAP(); test_MAKE_WORD(); test_GET_WORD();}
0 0
- C语言宏定义的几种简单用法
- C语言宏定义的用法
- c语言中宏定义的用法
- 【C语言】宏定义的用法总结
- C语言:宏定义的#和##用法
- C语言宏定义的简单总结
- C语言宏替换的几种用法 【转载】
- C语言中定义结构体的几种方法
- C语言定义数组的几种形式
- C语言定义数组的几种形式
- C语言中字符串的几种定义方式
- C语言:定义字符串的几种方式
- C语言中定义结构体的几种方法
- C宏定义的简单总结,#的用法
- C语言宏定义,内置宏的用法
- C语言宏定义,内置宏的用法
- C语言中宏定义的一个变态用法
- C语言中宏定义的一个变态用法
- EhCache实例
- codechef Annual Parade
- mysql_multi管理多实例
- CodeForces 178C3
- 机器学习之逻辑回归和softmax回归及sklearn和tensorflow代码示例
- C语言宏定义的几种简单用法
- Android中常用的设计原则与设计模式
- Nginx搭建负载环境
- Invoking Webservice from PL/SQL (UTL_DBWS&UTL_HTTP)
- 随机数组
- 装X式的阅读代码,无需开启Android Studio来运行项目
- 安装Grid执行root.sh出现ohasd failed解决方案
- Android自定义View使用总结
- POJ 1860 - Currency Exchange(SPFA正环)