当前位置:网站首页>Operator depth anatomy
Operator depth anatomy
2022-07-28 01:10:00 【Wax gourd learning java】
Catalog
1、 Hexadecimal and original code 、 Inverse code 、 Complement code
2.2.1、 Shift left operator (<<)
2.2.2、 Shift right operator (>>)
2.8、 Access structure operator
2.9.1、 Plastic surgery improves
Preface
Contents of this study : Original code 、 Inverse code 、 Complement code 、 The operator 、 Storage of operators in memory
1、 Hexadecimal and original code 、 Inverse code 、 Complement code
We all know that integers are stored in binary form in memory , Do you know how to store it ? In fact! , It is a Complement code Stored in . Now let's have a look .
First 1 An integer takes up four bytes , Each byte takes up 8 A bit , So an integer accounts for 32 A bit , for instance :
Integers 3, its Binary system (0~1) Is it 00000000000000000000000000000011, common 32 A bit . meanwhile 3 It can also be used. octal (0~7) Express , Hexadecimal (0~15) Express , and 2 Itself is Decimal system (0~9) It means .
So what is the original code 、 Inverse code 、 Complement code , What's the relationship between them ?
The binary representation of integers should be divided into positive integers and negative integers
Positive integer : The original code is the same as the inverse code and the complement
Negtive integer : Original code : That is, the binary bit mentioned above
Inverse code : Keep the sign bits -> highest , Other positions are reversed by position
Be careful : The highest bit is the sign bit 0-> Positive numbers 1-> negative
Complement code : Inverse code +1, In the memory , Integers store complements in memory
Example :5
The original code, inverse code and complement code are :
00000000000000000000000000000101
Example :-5( The number of digits has not changed , Spacing problem )
Original code :10000000000000000000000000000101
Inverse code :11111111111111111111111111111010
Complement code :11111111111111111111111111111011
2、 Operator parsing
2.1、 Arithmetic operators
+ - * / %
Be careful :/ Fractional integer division and decimal division , To achieve decimal division ,/ There is at least one floating-point number around
% Both ends of the modulo operator must be integers .
2.2、 Shift operator
significance : Shift points to move the complement of the integer left to right , Decimal shift is not supported , Nor does it support shifting negative digits or decimal places
Such as :1.5<<1, in addition a<<-1 or a<<1.5 Also wrong
2.2.1、 Shift left operator (<<)
follow : Left discard , The right to repair 0
Example :


Small conclusion : Move left n position , amount to a*2^n
2.2.2、 Shift right operator (>>)
1、 Count right : On the right side of the discarded , Fill in the sign on the left
2、 Logical shift right : On the right side of the discarded , Left complement 0
Be careful : Generally speaking , Compilers use arithmetic shift right .
Example :
7->4
-7->-4
Moving right is similar to moving left
2.3、 Bit operators
significance : Bit refers to the binary bit
2.3.1、&-- Bitwise AND
follow : Yes 0 take 0, Same as 1 only 1
Example :

2.3.2、|-- Press bit or
follow : Yes 1 be 1, Same as 0 only 0

2.3.3、^-- Bitwise XOR
follow : Take the same 0, Different take 1

^ Exclusive or Conclusion :
a=3 b=-5
a^0=a a^a=0 also ^ Satisfying the commutative law
therefore a^b^a=b b^a^b=a
Example :
requirement : Do not create temporary variables to realize the exchange of two numbers
The code is as follows
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// Do not create temporary variables , Realize the exchange of two numbers
// If you can create temporary variables, it is the same as usual
int main()
{
int a = 10;
int b = 20;
printf(" Exchange before :%d %d\n", a, b);
Law 1 Want to consider a+b Possible overflow problems
//a = a + b;
//b = a - b;
//a = a - b;
//printf(" After exchanging :%d %d", a, b);
// Law two
a = a ^ b;
b = a ^ b; // At this time a=10^20
a = a ^ b;
printf(" After exchanging :%d %d", a, b);
return 0;
}2.3.4、 application
Write a code : Find a binary integer stored in memory 1 The number of

The code is as follows :
#include<stdio.h>
int main()
{
int a = 5;
int cnt = 0;
int i = 0;
for(i=0;i<32;i++)
{
if(a & 1)
{
cnt++;
}
a >>= 1;
}
printf("%d\n", cnt);
return 0;
}2.4、 Monocular operators
! Logical anti operation
- negative
+ Comes at a time
& Address fetch
sizeof Find the length ( Unit is byte )( and strlen() Is a function to find the length of a string )
~ Binary for a number ( Complement code ) According to the not
-- In front of 、 After
++ In front of 、 After
* Indirect access operators ( That is, the dereference operator , Use with pointer )
( type ) Cast
2.4.1、 application ~、&、|
Write a code : Change the binary value of a number
summary : The first i Bit changed to 1:a|=1<<i among (0<=i<=32)
The first i Bit changed to 0:a&=~(1<<i) among (0<=i<=32)

The code is as follows :
#include<stdio.h>
int main()
{
int a = 13;
printf(" Before change :%d\n", a);
a |= 1 << 1;
printf("0->1:%d\n", a);
a &= ~(1 << 1);
printf("1->0:%d\n", a);
return 0;
}demonstration :

2.5、 Logical operators
&& -- The left is false , Not on the right
|| -- True on the left , Not on the right
2.6、 Conditional operators
follow :
a>b?a:b
a>b It's true , The result is a(b Don't count ), Otherwise b(a Don't count )
2.7、 Comma expression
follow :',' The previous formula calculates in turn , The result is the result of the last expression
Such as :a=1,b=2 c=(a>b,a=b+10,a,b=a+1)--->c=13
2.8、 Access structure operator
Such as struct Stu* p=&a ,a It's a structure
.( spot ) -- Structure + Member name (*p).
-> -- Structure pointer + Member name p ->
2.9、 Expression evaluation
follow : Consider priorities and associativity , One more : Implicit type conversion
Implicit type conversion : When character or Short It is converted to shaping before use , This situation is called Plastic surgery improves ,
2.9.1、 Plastic surgery improves
Plastic surgery improves : High complement sign bit , The low order remains unchanged
Example :
Such as char a=1;
Variable a There are only... In the binary complement of 8 A bit
:00000001
because a For signed char, When shaping and lifting , High complement sign bit , The result is
:00000000000000000000000000000001
The code is as follows ( Proof of plastic improvement ):
// Proof of plastic improvement
#include<stdio.h>
int main()
{
char c = 5;
printf("c Size :%d\n", sizeof(c));
printf("c Size :%d\n", sizeof(+c));
return 0;
}demonstration :

The significance of plastic surgery Promotion :
The shaping operation of the expression should be in CPU In the corresponding computing device of ,CPU Inside the shaper (ALU) The byte length of the operands of is generally int Byte length of , It's also CPU The length of a general-purpose register .
Universal CPU It is difficult to realize two directly 8 Direct addition of bits and bytes , therefore , Various lengths in expressions may be less than int The integer value of the length , Must be converted to int or unsigned int, Then it can be sent in CPU To perform the operation
2.9.2、 Arithmetic conversion
As opposed to plastic lifting , Type size >=int when , Arithmetic conversion occurs
summary
That's what we're going to talk about today , This article introduces the shift operator , Bit operators , Bitwise negation operators and their applications , There are also plastic lifting and arithmetic conversion
边栏推荐
- Jmeter在性能测试中的应用实践样例
- 推荐系统-精排模型:xDeepFM
- Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore
- Swear, swear, swear
- 计算属性的基本使用
- 深度刨析数据在内存中的存储
- Recommended system model (III): fine scheduling model [LR, gbdt, wide & deep, DCN, DIN, Dien, MMOE, ple]
- [BuildRelease Management]Parabuild
- 网络设备硬核技术内幕 防火墙与安全网关篇 (五) 安全双修大法 中
- How to clearly understand and express IAAs, PAAS and SaaS?
猜你喜欢

Ford SUV "Mustang" officially went offline, safe and comfortable

Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm

One year anniversary of creation, Chongba young Lang

深度刨析数据在内存中的存储

代码随想录笔记_哈希_1002查找共用字符

Resolved Unicode decodeerror: 'UTF-8' codec can't decode byte 0xa1 in position 0: invalid start byte

Array related knowledge

C language programming | explanation and Simulation of offsetof macro

node-red与TDengine交互

What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention
随机推荐
LeetCode - 寻找两个正序数组的中位数
What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention
[BuildRelease Management]Parabuild
Recommended system model (III): fine scheduling model [LR, gbdt, wide & deep, DCN, DIN, Dien, MMOE, ple]
node-red与TDengine交互
C type use of reflection
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
Byte flybook Human Resource Kit three sides
分支和循环语句题目练习
学习笔记12:Eratosthenes筛选法求素数(100以内) 和 魔方阵
怎么清晰地理解、表达 IaaS 、 PaaS 、 SaaS ?
Recommended system - offline recall: u2tag2i, ICF
Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore
MySQL中的运算符
110. In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe
Ford SUV "Mustang" officially went offline, safe and comfortable
激活最大化
Network device hard core technology insider firewall and security gateway (10)
leetcode:1997. 访问完所有房间的第一天【跳跃dp】
IP address & subnet mask