当前位置:网站首页>C language bit operation - learning 26
C language bit operation - learning 26
2022-06-12 00:39:00 【XG. Solitary dream】
This paper is finally updated at 2022 year 02 month 21 Japan , Has more than 6 I didn't update it . If the article content or picture resources fail , Please leave a message for feedback , I will deal with it in time , thank you !
Operator
- Instructions :
- 1. Bit operator , except Bitwise negation operator “~” Outside , Others are Two eyes ( element ) Operator , That is, there is an operation object on both sides of the operator ;
- 2. Bitwise The data type of the operand Can only be integer or Character ;
- 3. Bit operations can be combined with assignment operators , Composition bit compound assignment operator :
<<=、=>>、&=、^=、|=
Bitwise negation
- Grammar format :
~ expression
- Bitwise negation operator (~) The operation rules of : The value of the expression participating in the operation is bitwise “ Inverse ” operation . That is to say, on each binary bit of the expression value 1 Turn into 0,0 Turn into 1.
- According to the not (~) by Monocular operator , Its The priority is the same as that of other unary operators , And at the same level From right to left combination .
- for example :
int a=1;~anamely00000001-----------------------------------11111110, therefore~aIt's worth it -2 .
- Example
#include <stdio.h>
void main(){
short int a = 1, b;
b = ~a;
printf("a= %d\nb= %d\n",a, b);
}Bitwise and operation
- Grammar format :
expression 1 & expression 2
- Bitwise and operator (&) The operation rules of : Carry out the two expression values participating in the operation according to the corresponding hexadecimal digits “ And ” operation . The two corresponding binary bits are 1 when , The result of this bit is 1; Otherwise 0.
- for example :
3 & 5namely0000 00110000 0101-----------------0000 0001, therefore3&5It's worth it 1.
- Be careful : Negative numbers take part in the bitwise sum operation in the form of complement .
- Use “ Bitwise AND (&)” Operator , Pay attention to “ Logic and (&&)” Operator differences .
- Example
#include <stdio.h>
void main(){
short int a = 3, b=5,c;
c= a & b;
printf("c= %d\n",c);
}Bitwise OR operation
- Grammar format :
expression 1 | expression 2
- bitwise or operator (|) The operation rules of : Carry out the two expression values involved in the operation according to the corresponding binary “ or ” operation . When two corresponding binary bits have 1 Is it 1 when , The result of this bit is 1; Otherwise 0.
- for example :
3 | 5namely0000 00110000 0101-----------------0000 0111, therefore3|5It's worth it 7.
- Be careful : Negative numbers take part in the bitwise sum operation in the form of complement .
- Use “ Press bit or (|)” Operator , Pay attention to “ Logic or (||)” Operator differences .
- Example
#include <stdio.h>
void main(){
short int a = 3, b=5,c;
c= a | b;
printf("c= %d\n",c);
}Bitwise exclusive or operation
- Grammar format :
expression 1 ^ expression 2
- bitwise exclusive or operator (^) The operation rules of : Carry out the two expression values involved in the operation according to the corresponding binary bits “ Exclusive or ” operation . When two corresponding binary bit values are different, it is 1; Otherwise 0.
- for example :
3 ^ 5namely0000 00110000 0101-----------------0000 0110, therefore3^5It's worth it 6.
- Example
#include <stdio.h>
void main(){
short int a = 3, b=5,c;
c= a ^ b;
printf("c= %d\n",c);
}Move left
- Grammar format :
expression 1 << expression 2- expression 1 Is the moved operand
- expression 2 Is the number of bits moved
- Shift left operator (<<) The operation rules of : Expression 1 The binary bits of the value of each left shift expression 2 Number of digits of the value of . When the binary bit moves to the left , The removed high bits are discarded directly , The vacancy in the low position is filled 0.
- for example :
int a=1;a << 2namelya=0000 0001a=0000 0100thereforea << 2It's worth it 4.
- Example
#include <stdio.h>
void main() {
int a = 1,b;
b = a << 2;
printf("b= %d\n", b);
}Move right
- Grammar format :
expression 1 >> expression 2- expression 1 Is the moved operand
- expression 2 Is the number of bits moved
- Fill the space on the left after moving to the right 0 Or make up 1 And the expression 1 The type of and the machine used are related to the compilation environment
- Shift right operator (>>) The operation rules of : Expression 1 The binary bit of the value of each shift right expression 2 Number of digits of the value of . When the binary bit moves to the right , Positive left to fill 0, Negative numbers are usually left complemented 1, On the right side of the discarded ( That is, do not change the original numerical symbol ).
- If it is an unsigned type , When the binary bit moves to the right, the left complement 0, On the right side of the discarded .
- Move right n Bits are equal to dividing by 2n
- Be careful : The original symbol is 1( negative ), There is a vacancy on the left 0 still 1, Depends on the computer system used , Some systems supplement 0, Some systems supplement 1. repair 0 Of is called “ Logical shift right ”, Also known as simple shift right ; repair 1 It is called a “ Arithmetic shift right ”
- for example :
int a=4;a >> 2namelya=0000 0100a=0000 0001thereforea >> 2It's worth it 1.
- for example :
int a=-4;a >> 2namely
- Example
#include <stdio.h>
void main() {
int a = 4,b;
b = a >> 2;
printf("b= %d\n", b);
}Bit assignment operation
- Bit operation can be combined with assignment operation to form bit operation assignment operator .
&=example :a&=bamount toa=a&b|=example :a|=bamount toa=a|b>>=example :a>>=bamount toa=a>>b<<=example :a<<=bamount toa=a<<b^=example :a^=bamount toa=a^b
边栏推荐
- Sword finger offer 09 Implementing queues with two stacks
- Month selector disable data after the current month to include the current month
- 2022 edition of global and Chinese on silicon liquid crystal market supply and demand research and prospect Trend Forecast Report
- Lambda中间操作flatMap
- Invalid spacing setting before WPS Title segment solution
- Why are the values of ordereddict not equal- Why are the values of an OrderedDict not equal?
- Creating and running JMeter performance test scenarios
- Mysql database: introduction to database 𞓜 addition, deletion, modification and query
- Xiaomu's interesting PWN
- 深度学习100例 | 第41天:语音识别 - PyTorch实现
猜你喜欢

Exploration of qunar risk control safety products
![[academic related] it is suggested to collect. Which behaviors are academic misconduct?](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[academic related] it is suggested to collect. Which behaviors are academic misconduct?

统一认证中心 Oauth2 高可用坑
![[industry application] an article on power digital transformation](/img/4a/d2d2795992cac8fb76135a117c3091.jpg)
[industry application] an article on power digital transformation

win7 fps优化的方法

IP编址概述

System.CommandLine选项Option

Flink CDC + Hudi 海量数据入湖在顺丰的实践

Detailed explanation of merge sorting

How to package and send compressed files for mobile WPS
随机推荐
Lambda创建流
Lambda中间操作limit
Do you want to take the postgraduate entrance examination? Will you be able to find a good job after graduate school?
Cuiyunkai, CEO of Gewu titanium Intelligent Technology: data value jump, insight into the next generation of change forces
Started with trust and loyal to profession | datapipeline received a thank you letter from Shandong city commercial bank Alliance
How to send Apple phone WPS files to QQ mailbox
Win jar package setting boot auto start
gin解决跨域问题
Lambda中间操作sorted
Creating and running JMeter performance test scenarios
一、Flutter 入门学习写一简单客户端
DevOps落地实践点滴和踩坑记录-(1)
关于接口测试的那些“难言之隐”
Experiment 7 class construction and static member function
repeat_ L2-009 red envelope grabbing_ sort
C language preprocessing instructions - learning 21
Lambda中间操作distinct
Construction environnementale 2
wps表格怎么取消智能表格样式
C language exercise: esp32 ble Low Power Bluetooth server data packaging and client data analysis