C语言有哪些实用方法与技巧分享?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1592个文字,预计阅读时间需要7分钟。
C语言实用技巧+避免向上取整
C语言常用方法技巧
除法向上取整
#define DIV_ROUND_UP(n, d) (((n)+(d)-1) / (d))
大端小端选择
low-endian or high-endian
typedef union { short W; /* Word access */ struct { /* Byte access */ #ifdef LOW_ENDIAN byte low, high; /* in low-endian arch */ #else byte high, low; /* in high-endian arch */ #endif } B; } word;
求余数运算
a = a % 8; => a = a & 7;
说明:位运算只需一个指令周期;取余通常需要调用子程序。
平方运算
a = pow(a, 2.0); => a = a * a;
说明:内置乘法运算器的处理器中,乘法运算比求平方运算更快;即使没有内置乘法运算器,乘法运算的子程序也比平方运算子程序效率高。
本文共计1592个文字,预计阅读时间需要7分钟。
C语言实用技巧+避免向上取整
C语言常用方法技巧
除法向上取整
#define DIV_ROUND_UP(n, d) (((n)+(d)-1) / (d))
大端小端选择
low-endian or high-endian
typedef union { short W; /* Word access */ struct { /* Byte access */ #ifdef LOW_ENDIAN byte low, high; /* in low-endian arch */ #else byte high, low; /* in high-endian arch */ #endif } B; } word;
求余数运算
a = a % 8; => a = a & 7;
说明:位运算只需一个指令周期;取余通常需要调用子程序。
平方运算
a = pow(a, 2.0); => a = a * a;
说明:内置乘法运算器的处理器中,乘法运算比求平方运算更快;即使没有内置乘法运算器,乘法运算的子程序也比平方运算子程序效率高。

