当前位置:网站首页>C语音常用的位运算技巧
C语音常用的位运算技巧
2022-07-05 01:13:00 【漫天飞舞的雪花】
一:判断奇偶性
void odd_even(int n)
{
if(n & 1 == 1)
{
printf("n是奇数!\n");
}
}二:交换两个数字
int swap(int x, int y)
{
x = x ^ y;
y = x ^ y;
x = x ^ y;
}基于异或运算的如下性质:
1.任意一个变量X与其自身进行异或运算,结果为0,即X^X=0
2.任意一个变量X与0进行异或运算,结果不变,即X^0=X
3.异或运算具有可结合性,即a^ b ^ c =(a ^ b)^ c = a ^(b ^ c)
4.异或运算具有可交换性,即a ^ b = b ^ a
三:找出不重复的数字
int array[] = {1, 2, 3, 4, 5, 1, 2, 3, 4};
采用:1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 3 ^ 4;
//等价于:
(1 ^ 1) ^ (2 ^ 2) ^ (3 ^ 3) ^ (4 ^ 4) ^ 5;
//等价于:
0 ^ 0 ^ 0 ^ 0 ^ 5;
//结果为:
5;四:m的n次方
想通过二进制解决问题,还得把数字拆成二进制来观察。比如计算m的15次方:
m^15 = m^8 * m^4 * m^2 * m^1; //^表示幂运算
换成二进制:
m^1111 = m^1000 * m^0100 * m^0010 * m^0001;
我们可以通过"& 1"和">> 1"来逐位读取1111,等于1时将该位代表的乘数累乘到最终结果。代码如下:
int Pow(int m, int n)
{
int res = 1;
int tmp = m;
while(n != 0)
{
if(n & 1 == 1)
{
res *= tmp;
}
tmp *= tmp;
n = n >> 1;
}
return res;
}五:检测整数n是否是2的幂次
int ispow(int N)
{
return ((N & (N - 1)) == 0) ? 1 : 0;
}六:求绝对值
int abs(int n)
{
return (n ^ (n >> 31)) - (n >> 31);
}七:求两个数的最大值
int max(int x, int y)
{
return x ^ ((x ^ y) & -(x < y));
}八:位技巧
一个32bit数据的位、字节读取操作
(1)获取单字节:
#define GET_LOW_BYTE0(x) ((x >> 0) & 0x000000ff) /* 获取第0个字节 */
#define GET_LOW_BYTE1(x) ((x >> 8) & 0x000000ff) /* 获取第1个字节 */
#define GET_LOW_BYTE2(x) ((x >> 16) & 0x000000ff) /* 获取第2个字节 */
#define GET_LOW_BYTE3(x) ((x >> 24) & 0x000000ff) /* 获取第3个字节 */
(2)获取某一位:
#define GET_BIT(x, bit) ((x & (1 << bit)) >> bit) /* 获取第bit位 */
(3)清零某个字节:
#define CLEAR_LOW_BYTE0(x) (x &= 0xffffff00) /* 清零第0个字节 */
#define CLEAR_LOW_BYTE1(x) (x &= 0xffff00ff) /* 清零第1个字节 */
#define CLEAR_LOW_BYTE2(x) (x &= 0xff00ffff) /* 清零第2个字节 */
#define CLEAR_LOW_BYTE3(x) (x &= 0x00ffffff) /* 清零第3个字节 */
(4)清零某一位:
#define CLEAR_BIT(x, bit) (x &= ~(1 << bit)) /* 清零第bit位 */
(5)置某个字节为1:
#define SET_LOW_BYTE0(x) (x |= 0x000000ff) /* 第0个字节置1 */
#define SET_LOW_BYTE1(x) (x |= 0x0000ff00) /* 第1个字节置1 */
#define SET_LOW_BYTE2(x) (x |= 0x00ff0000) /* 第2个字节置1 */
#define SET_LOW_BYTE3(x) (x |= 0xff000000) /* 第3个字节置1 */
(6)置位某一位:
#define SET_BIT(x, bit) (x |= (1 << bit)) /* 置位第bit位 */
(7)判断某几位连续位的值
/* 获取第[n:m]位的值 */
#define BIT_M_TO_N(x, m, n) ((unsigned int)(x << (31-(n))) >> ((31 - (n)) + (m)))九:判断某一位的值
#include <stdio.h>
int main (void){
unsigned int a = 0x68;
if(a & (1 <<3)){
printf("0x68的第三位的值位1\n");
}else {
printf("0x68的第三位的值位0\n");
}
}十:求余运算
int a=X%Y;Y必须是2^N。
公式为:a=X&(2^N-1) 或者 a=X &( ~Y);十一:除法运算
a=a*4 改为用位操作 a=a<<2;
a=a/4 改为用位操作 a=a>>2;十二:获取连续的几位的值
#define CALC_MASK(offset,len,mask) \
if((offset+len)>16) \
{ \
return error; \
} \
else \
mask = (((1<<(offset+len)))-(1<<offset))
\\ data获取bit6和bit7的值;
CALC_MASK(6,2,mask);
usData=(data&mask)>>6;边栏推荐
- Discrete mathematics: propositional symbolization of predicate logic
- Classification of performance tests (learning summary)
- When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
- Database postragesql client authentication
- How to use words to describe breaking change in Spartacus UI of SAP e-commerce cloud
- Which financial products with stable income are good
- Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
- Digital DP template
- 【海浪建模2】三维海浪建模以及海浪发电机建模matlab仿真
- What is the current situation and Prospect of the software testing industry in 2022?
猜你喜欢

微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益

A simple SSO unified login design

dotnet-exec 0.6.0 released

【海浪建模3】三维随机真实海浪建模以及海浪发电机建模matlab仿真

Wechat applet: exclusive applet version of the whole network, independent wechat community contacts

dotnet-exec 0.6.0 released

Implementation steps of master detail detail layout mode of SAP ui5 application

Database performance optimization tool

Expansion operator: the family is so separated

The performance of major mainstream programming languages is PK, and the results are unexpected
随机推荐
[untitled]
Postman automatically fills headers
Call Huawei order service to verify the purchase token interface and return connection reset
La jeunesse sans rancune de Xi Murong
“薪资倒挂”、“毕业生平替” 这些现象说明测试行业已经...
BGP comprehensive experiment
“薪資倒掛”、“畢業生平替” 這些現象說明測試行業已經...
大专学历,33岁宝妈又怎样?我照样销售转测试,月入13k+
JS implementation determines whether the point is within the polygon range
那些一门心思研究自动化测试的人,最后都怎样了?
Global and Chinese market of optical densitometers 2022-2028: Research Report on technology, participants, trends, market size and share
Heartless sword English translation of Xi Murong's youth without complaint
Yyds dry goods inventory [Gan Di's one week summary: the most complete and detailed in the whole network]; detailed explanation of MySQL index data structure and index optimization; remember collectio
Ruby tutorial
When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Check if this is null - checking if this is null
RB technology stack
整理混乱的头文件,我用include what you use
Apifox (postman + swagger + mock + JMeter), an artifact of full stack development and efficiency improvement