当前位置:网站首页>C language | bitwise operator
C language | bitwise operator
2022-04-22 07:25:00 【ZXXL】
Bit operation
Bit operators , It mainly includes
And , or operator &,|
Inverse character ~
Shifter <<,>>
XOR ^
Before we start, let's look at a piece of code to realize binary output , The observation position is convenient
Reference link Know the author —— Manon loves learning
void printf_bin(int num)
{
int i, j, k;
unsigned char *p = (unsigned char*)&num + 3;//p Point first num Next 3 Byte address , namely num The highest byte address of
for (i = 0; i < 4; i++) // Sequential processing 4 Bytes (32 position )
{
j = *(p - i); // Take the first address of each byte , From high byte to low byte , namely p p-1 p-2 p-3 address
for (int k = 7; k >= 0; k--) // Processing of each byte 8 bits , Note that the binary number inside the byte is stored according to people's habits !
{
if (j & (1 << k))//1 Move left k position , And the byte content before the single j Or operation , Such as k=7 when ,00000000&10000000=0 -> The highest bit of this byte is 0
printf("1");
else
printf("0");
}
printf(" ");// Every time 8 Bit plus a space , Convenient view
}
printf("\r\n");
}
And operators &
And 0 clear 0, And 1 unchanged
function 1: Extract bit data
function 2: eliminate (0)
e.g.
#include <stdio>
int main()
{
// take int The variable of a Of the 28 Positional clarity 0, Other bits remain the same
int a = 0xffffffff;
a = a & 0xefffffff;
printf("a = 0x%x\n", a);
printf_bin(a);
// take int The variable of b Of the 17 Positional clarity 0, Other bits remain the same
int b = 0xffffffff;
b = b & 0xfffdffff;
printf("b = ");
printf_bin(b);
return 0;
}
Or operator |
or 1 Set up 1, or 0 unchanged
function : Set up 1
Example : take int The variable of a Of the 22-23 Location 1, Other bits remain the same
int a ;
a = a | 0xc00000
Negation operator ~
Take the opposite 1 Turn into 0, Take the opposite 0 Turn into 1
Example : take int The variable of a Of the 28 Positional clarity 0, Other bits remain the same
a = a & ~ (0x10000000);
Shift Operators << / >>
The shift operator shifts to the left / Move right
The leftmost position when moving left ( Include sign bits ) Move out of memory space , The rightmost digit is filled with zero
When moving right, the rightmost bit is removed , Leftmost zeroing
char c = 0x80 = 0b 1000 0000
a = a & ~(0x1<<28)
a &= ~(0x1<<28);
a = a + 3;
a += 3;
a = a & ~(0x3<<26);
Move left and right circularly, and the leftmost bit is connected with the rightmost bit ( Bit operation cannot be realized )
Exclusive or operator ^
Exclusive or 1 Take the opposite , Exclusive or 0 unchanged
Example : take a Of the 28 Bit exclusive or 1, the a Of the 28 Bit inversion
And Take the opposite ~ The difference between lie in Exclusive or ^ Of Direct action object by a No 28 position
a = a ^ (0x1<<28);
版权声明
本文为[ZXXL]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220610405386.html
边栏推荐
- a5 transceiver 信号vod和预加重调整关系
- Redis advanced
- 浅谈时间复杂度与空间复杂度
- Change the class with parameters in the first part of the last operation to be presented in the way without parameters, and the function remains unchanged.
- Codeforces Round #776 (Div. 3)
- [number theory] prime number (I): basic concepts, properties, conjectures and theorems
- 【数论】【不定方程】n元一次不定方程、佩尔方程、毕达哥拉斯定理、费马大定理
- 【数论】素数(二):素数筛法(埃式筛、欧拉筛、区间筛)
- 【数论】同余(七):快速幂、矩阵快速幂
- 【数论】素数(三):素数判断法(朴素法、模6法、Rabin-Miller及改进)
猜你喜欢

详解冒泡序列与数组名

SQL review, grammar notes, fresh out

What is the internal structure of stack frame?

LeetCode - 7 - (二叉树的最近公共祖先、轮转数组、二叉树的直接、下一个排列、组合总和)

Points for attention in Modelsim simulation acceleration

14 lines of code to complete arbitrary selection of image crawling

(2) Basic configuration of SQL server and connection to SQL server using Navicat

面试官常问的,对象分配的一般过程及特殊情况

Idea does not display the run dashboard view window

二叉树链式结构操作LeetCode+牛客(详解)
随机推荐
Codeforces Round #610 (Div. 2)
Win10 modify command line default font
【数论】同余(五):多元线性同余方程
L2-005 集合相似度(set判重)
[number theory] prime number (4): decomposition of numbers (Pollard rho)
面试官常问的,对象分配的一般过程及特殊情况
Design a circle class with private member radius representing radius and function get_ Radius () is used to obtain the radius and area () is used to calculate the area of the circle; (2) Define a tabl
双向循环链表(详)
Why is the data stored in the leaf node of the non primary key index the primary key value
抽象类和抽象方法
八大排序的思想及其代码
Written examination for summer internship of meituan spring recruitment -- 20220312
296 · 数组去重
[number theory] prime number (V): Mason prime number (lucas_lehmer decision method)
牛客xb月赛45题解
[number theory] [indefinite equation] n-ary primary indefinite equation, Pell equation, Pythagoras theorem, Fermat theorem
SQL Server quick start
(六) Sql Server的DCL丶DML语法
Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
Host cannot Ping virtual machine in bridging mode