当前位置:网站首页>Logical operation bit operation
Logical operation bit operation
2022-06-13 01:21:00 【Zhao_| adult】
List of articles
1、 Original code 、 Inverse code 、 Complement code 、 Shift the code
Original code 、 Inverse code 、 Complement code
- Positive numbers : The original code of a positive number 、 Inverse code 、 The complement is the same
- negative :
- Original code : The highest bit represents the sign bit , The negative sign bit is 1, The sign bit of a positive number is 0;
- Inverse code : The inverse of a negative number is based on its original code , The sign bits remain the same , The rest of the bits are reversed .
- Complement code : Complement code = Inverse code +1; Sign bits can be carried .
- Shift the code : On the basis of complement Sign bit inversion . Code shift can only be used to represent integers , Cannot represent decimal
- Replace the complement code with the original code : It is the same as converting the original code to the complement :–> Inverse code +1;
2、 Binary system 、 octal 、 Decimal system 、 Conversion between hexadecimals
Binary system 、 octal 、 Decimal system 、 Conversion between hexadecimals
3、 Floating point decimal conversion
Floating point decimal conversion
4、 Negative binary conversion
5. Logical operations
What is true ? : All nonzero values are true ;
What is false ?: 0 On behalf of false ;
1. Logic and &&
If and only if two operands are true ( Nonzero ) The result is true when ;
int x=100;
int y=50;
cout<<(x&&y)<<endl; //1
int x=100;
int y=0;
cout<<(x&&y)<<endl; //0
2. Logic or ||
As long as one of the two objects is true ( Nonzero ) That's true ;
int x=100;
int y=0;
cout<<(x||y)<<endl; //1
int x=0;
int y=0;
cout<<(x||y)<<endl; //0
3. Logic is not !
Reverses the value of the operand and returns ;
** Not 0 Take the opposite ⇒ 0; 0 Take the opposite ⇒ 1; **
int x=100;
cout<<(!x)<<endl; //0
int x=0;
cout<<(!x)<<endl; //1
The value here is negative And According to the not Is different , According to the not Need to be converted to Binary system , Then perform bit wise inversion ;
6. An operation
Convert it to Binary system It's going on An operation ;
1. Bit and &
35: 0 0 1 0 0 0 1 1
47: 0 0 1 0 1 1 1 1
&
————————————————————
35: 0 0 1 0 0 0 1 1
int x=35;
int y=47;
cout<<(x&y)<<endl; //35
2. Bit or |
35: 0 0 1 0 0 0 1 1
47: 0 0 1 0 1 1 1 1
|
————————————————————
47: 0 0 1 0 1 1 1 1
int x=35;
int y=47;
cout<<(x|y)<<endl; //47
3. Bit inversion ~
35: 0 0 1 0 0 0 1 1
~
————————————————————
92: 1 1 0 1 1 1 0 0 // The first bit is the sign bit
int x=35;
cout<<(!x)<<endl; // -36
As can be seen from the above 35 The output of the program is the same as The result of bitwise negation is inconsistent ?
Because negative numbers are usually used in calculations Complement code Express : So we should convert the inverse result into Complement code
35: 0 0 1 0 0 0 1 1
~
————————————————————
92: 1 1 0 1 1 1 0 0 // Original code
1 0 1 0 0 0 1 1 // Inverse code : The sign bits remain the same , The rest of the bits are reversed
+1
————————————————————
-36: 1 0 1 0 0 1 0 0 // The first bit is the sign bit
4. Bit exclusive or ^
Take the same 0 , The similarities and differences are 1;
35: 0 0 1 0 0 0 1 1
47: 0 0 1 0 1 1 1 1
^
————————————————————
12: 0 0 0 0 1 1 0 0
int x=35;
int y=47;
cout<<(x^y)<<endl; // 12
边栏推荐
- Page optimization - Notes
- MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
- Work and life
- Leetcode-12- integer to Roman numeral (medium)
- 关于#数据库#的问题,如何解决?
- FLIP动画实现思路
- Characteristics of transactions -- atomicity (implementation principle)
- 工作与生活
- How does Apple add QQ email?
- Golang learning essay
猜你喜欢
随机推荐
Idea installation tutorial
Go simple read database
Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
RSA encryption colloquial explanation
Introduction to convolutional neural network
leetcode. 349. intersection of two arrays
Several categories of software testing are clear at a glance
Et5.0 configuring Excel
Unity calls alertdialog
[Latex] 插入圖片
【斯坦福計網CS144項目】Lab1: StreamReassembler
Most elements leetcode
Golang learning essay
My crawler learning notes
Leetcode-18- sum of four numbers (medium)
Leetcode-11- container with the most water (medium)
Leetcode-16- sum of the nearest three numbers (medium)
Answer to matrix theory of Nanjing University of Aeronautics and Astronautics
Redis usage optimization summary learning
切线与切平面









