当前位置:网站首页>Common bit operation skills of C speech
Common bit operation skills of C speech
2022-07-05 01:26:00 【Snowflakes flying all over the sky】
One : Judge parity
void odd_even(int n)
{
if(n & 1 == 1)
{
printf("n Is odd !\n");
}
}Two : Exchange two numbers
int swap(int x, int y)
{
x = x ^ y;
y = x ^ y;
x = x ^ y;
} Based on the following properties of XOR operation :
1. Any variable X XOR with itself , The result is 0, namely X^X=0
2. Any variable X And 0 Xor operation , The result remains unchanged. , namely X^0=X
3. XOR operations are associative , namely a^ b ^ c =(a ^ b)^ c = a ^(b ^ c)
4. XOR operation is exchangeable , namely a ^ b = b ^ a
3、 ... and : Find numbers that are not repeated
int array[] = {1, 2, 3, 4, 5, 1, 2, 3, 4};
use :1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 3 ^ 4;
// Equivalent to :
(1 ^ 1) ^ (2 ^ 2) ^ (3 ^ 3) ^ (4 ^ 4) ^ 5;
// Equivalent to :
0 ^ 0 ^ 0 ^ 0 ^ 5;
// The result is :
5;Four :m Of n Power
Want to solve the problem through binary , You have to break the numbers into binary to observe . For example, calculation m Of 15 Power :
m^15 = m^8 * m^4 * m^2 * m^1; //^ Represents a power operation
Change to binary :
m^1111 = m^1000 * m^0100 * m^0010 * m^0001;
We can go through "& 1" and ">> 1" To read... Bit by bit 1111, be equal to 1 Multiply the multiplier represented by this bit to the final result . The code is as follows :
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;
}5、 ... and : Detection integer n Whether it is 2 The power of
int ispow(int N)
{
return ((N & (N - 1)) == 0) ? 1 : 0;
}6、 ... and : Find the absolute value
int abs(int n)
{
return (n ^ (n >> 31)) - (n >> 31);
}7、 ... and : Find the maximum of two numbers
int max(int x, int y)
{
return x ^ ((x ^ y) & -(x < y));
}8、 ... and : Bit skill
One 32bit Bits of data 、 Byte read operation
(1) Get single byte :
#define GET_LOW_BYTE0(x) ((x >> 0) & 0x000000ff) /* For the first 0 Bytes */
#define GET_LOW_BYTE1(x) ((x >> 8) & 0x000000ff) /* For the first 1 Bytes */
#define GET_LOW_BYTE2(x) ((x >> 16) & 0x000000ff) /* For the first 2 Bytes */
#define GET_LOW_BYTE3(x) ((x >> 24) & 0x000000ff) /* For the first 3 Bytes */
(2) Get someone :
#define GET_BIT(x, bit) ((x & (1 << bit)) >> bit) /* For the first bit position */
(3) Clear a byte :
#define CLEAR_LOW_BYTE0(x) (x &= 0xffffff00) /* Clear the ground 0 Bytes */
#define CLEAR_LOW_BYTE1(x) (x &= 0xffff00ff) /* Clear the ground 1 Bytes */
#define CLEAR_LOW_BYTE2(x) (x &= 0xff00ffff) /* Clear the ground 2 Bytes */
#define CLEAR_LOW_BYTE3(x) (x &= 0x00ffffff) /* Clear the ground 3 Bytes */
(4) Clear someone :
#define CLEAR_BIT(x, bit) (x &= ~(1 << bit)) /* Clear the ground bit position */
(5) Set a byte to 1:
#define SET_LOW_BYTE0(x) (x |= 0x000000ff) /* The first 0 Byte set 1 */
#define SET_LOW_BYTE1(x) (x |= 0x0000ff00) /* The first 1 Byte set 1 */
#define SET_LOW_BYTE2(x) (x |= 0x00ff0000) /* The first 2 Byte set 1 */
#define SET_LOW_BYTE3(x) (x |= 0xff000000) /* The first 3 Byte set 1 */
(6) Set one to :
#define SET_BIT(x, bit) (x |= (1 << bit)) /* Put in the first place bit position */
(7) Judge the value of some consecutive bits
/* For the first [n:m] The value of a */
#define BIT_M_TO_N(x, m, n) ((unsigned int)(x << (31-(n))) >> ((31 - (n)) + (m)))Nine : Judge the value of a bit
#include <stdio.h>
int main (void){
unsigned int a = 0x68;
if(a & (1 <<3)){
printf("0x68 The third value bit of 1\n");
}else {
printf("0x68 The third value bit of 0\n");
}
}Ten : The remainder
int a=X%Y;Y Must be 2^N.
Formula for :a=X&(2^N-1) perhaps a=X &( ~Y);11、 ... and : Division operations
a=a*4 Change to bit operation a=a<<2;
a=a/4 Change to bit operation a=a>>2;Twelve : Get the value of consecutive digits
#define CALC_MASK(offset,len,mask) \
if((offset+len)>16) \
{ \
return error; \
} \
else \
mask = (((1<<(offset+len)))-(1<<offset))
\\ data obtain bit6 and bit7 Value ;
CALC_MASK(6,2,mask);
usData=(data&mask)>>6;边栏推荐
- User login function: simple but difficult
- 【海浪建模3】三维随机真实海浪建模以及海浪发电机建模matlab仿真
- JS implementation determines whether the point is within the polygon range
- Database postragesql client authentication
- After reading the average code written by Microsoft God, I realized that I was still too young
- Global and Chinese market of nutrient analyzer 2022-2028: Research Report on technology, participants, trends, market size and share
- 無心劍英譯席慕容《無怨的青春》
- [wave modeling 2] three dimensional wave modeling and wave generator modeling matlab simulation
- Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
- Senior Test / development programmers write no bugs? Qualifications (shackles) don't be afraid of mistakes
猜你喜欢

FEG founder rox:smartdefi will be the benchmark of the entire decentralized financial market

Arbitrum: two-dimensional cost
![[CTF] AWDP summary (WEB)](/img/4c/574742666bd8461c6f9263fd6c5dbb.png)
[CTF] AWDP summary (WEB)
![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](/img/e8/de158982788fc5bc42f842b07ff9a8.jpg)
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

Roads and routes -- dfs+topsort+dijkstra+ mapping

Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization

实战模拟│JWT 登录认证

流批一体在京东的探索与实践

Chia Tai International Futures: what is the master account and how to open it?

Nebula importer data import practice
随机推荐
Detailed explanation of multi-mode input event distribution mechanism
[microprocessor] VHDL development of microprocessor based on FPGA
JS implementation determines whether the point is within the polygon range
【微处理器】基于FPGA的微处理器VHDL开发
Take you ten days to easily complete the go micro service series (IX. link tracking)
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
I was beaten by the interviewer because I didn't understand the sorting
Check if this is null - checking if this is null
[FPGA tutorial case 10] design and implementation of complex multiplier based on Verilog
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
Main window in QT application
Can financial products be redeemed in advance?
Heartless sword English translation of Xi Murong's youth without complaint
PHP 约瑟夫环问题
Hand drawn video website
There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
[FPGA tutorial case 9] design and implementation of clock manager based on vivado core
Innovation leads the direction. Huawei Smart Life launches new products in the whole scene
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花