当前位置:网站首页>Detailed operator
Detailed operator
2022-07-30 22:32:00 【~Xiao Ming learns programming】
作者:~小明学编程
文章专栏:C语言基础知识
目之所及皆为回忆,心之所向皆为过往
目录
Practical use of displacement operators and bitwise operators
两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同
算数操作符
位移操作符
<< 左移操作符
移位规则
Shift its bits to the left,然后右边补0
int main()
{
int a = 10; //00000000000000000000000000001010
int b = a << 1;//00000000000000000000000000010100
printf("a=%d,b=%d",a, b);
return 0;
}Here is our shift result,Its role is equivalent to willa乘以2.

It is worth noting that although we are rightaA shift operation is performed howeveraThere is no change in itself as we can seea的值还是10.
>> 右移操作符
Whether to move left or right depends on the specific machine,在我们的VS2022is used for arithmetic right shift.
int main()
{
int a = -10; //1111 1111 1111 1111 1111 1111 1111 0110
int b = a >> 1;//1111 1111 1111 1111 1111 1111 1111 1011
printf("a=%d,b=%d", a, b);
return 0;
}
Here we can see that the right shift operation is equivalent to division2的功能(When it is negative, it will divide2的基础上-1).
位操作符
& //按位与| //按位或^ //按位异或注:他们的操作数必须是整数.
& 按位与
运算规则:The algorithm by location is if one of the binary representations of two numbers is0那么其结果就是0,Only two corresponding bits are all1,其结果才是1.
| 按位或
运算规则:The algorithm by location is if one of the binary representations of two numbers is1那么其结果就是1,Only two corresponding bits are all0,其结果才是0.
^ 按位异或
运算规则:The algorithm by location is that in the binary representation of two numbers, if the two corresponding bits are the same0,对应位不同则为1.
int main()
{
int a = 1;//00000000000000000000000000000001
int b = 2;//00000000000000000000000000000010
int c = 0;
printf("%d\n", a & b); //00000000000000000000000000000000
printf("%d\n", a | b); //00000000000000000000000000000011
printf("%d\n", a ^ b); //00000000000000000000000000000011
return 0;
}
Practical use of displacement operators and bitwise operators
不能创建临时变量(第三个变量),实现两个数的交换
int main()
{
int a = 10;//00001010
int b = 20;//00010100
printf("交换前:a=%d,b=%d\n", a, b);
a = a ^ b;// 00011110
b = a ^ b;// 00001010
a = a ^ b;// 00010100
printf("交换后:a=%d,b=%d\n", a, b);
return 0;
}
统计二进制中1的个数
int main()
{
int a = 0;
int count = 0;
int flag = 1;
printf("请输入:");
scanf("%d", &a);
while (flag<=32)
{
if (((a >> flag) & 1) == 1)
count++;
flag++;
}
printf("%d的二进制形式中1的个数为%d", a, count);
return 0;
}
两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同
int main()
{
int a = 0;
int b = 0;
int count = 0;
int flag = 1;
printf("请输入:");
scanf("%d %d", &a, &b);
while (flag <= 32)
{
if (((a >> flag) & 1) != ((b >> flag) & 1))
count++;
flag++;
}
printf("共%d个bit位不同", count);
return 0;
}
赋值操作符
赋值操作符是一个很棒的操作符,他可以让你得到一个你之前不满意的值.也就是你可以给自己重新赋值.
int main()
{
int weight = 120;//体重
weight = 89;//不满意就赋值
double salary = 10000.0;
salary = 20000.0;
//赋值操作符可以连续使用,比如:
int a = 10;
int x = 0;
int y = 20; a = x = y+1;//连续赋值
//这样的代码感觉怎么样?
//那同样的语义,你看看:
x = y+1; a = x;
//这样的写法是不是更加清晰爽朗而且易于调试.
return 0;
}复合赋值符
int x = 10;x = x+10;x += 10;//复合赋值//其他运算符一样的道理.这样写更加简洁.
单目操作符
int main()
{
int a = -10;
int* p = NULL;
printf("%d\n", !2);
printf("%d\n", !0);
a = -a;
p = &a;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof a);//这样写行不行?
//printf("%d\n", sizeof int);//这样写行不行?
return 0;
}这里我们可以看到当sizeofWhen followed by a type, parentheses must be added.

int main()
{
int a = 10;
int n = 0;
scanf("%d", &n);
//把a的第n位置为1
a = a | (1 << (n-1));
printf("a=%d\n", a);
//把a的第n位置为0
a = a & ~(1 << (n - 1));
printf("a=%d\n", a);
//00000000000000000000000000001010
//00000000000000000000000000010000
//1<<2;
//00000000000000000000000000011010
//11111111111111111111111111101111
//00000000000000000000000000010000
//00000000000000000000000000001010
return 0;
}Here we just combine bitwise AND and bitwise OR to solve the problem.
The following is a brief introduction to an error-prone point
int main()
{
short s = 10;
int a = 2;
s = a + 5;
printf("%zu\n", sizeof(s = a + 5));
printf("%d\n", s);
return 0;
}What is the result of the above program?(%zuis the unsigned integer of the output)

我们可以看到是2 7,s是shortSo it takes two bytes buts不是+5why not12呢?
因为sizeof()Expressions in parentheses are not evaluated.
关系操作符
逻辑操作符
#include <stdio.h>
int main()
{
int i = 0,a=0,b=2,c =3,d=4;
i = a++ && ++b && d++;
//i = a++||++b||d++;
printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d);
return 0; }
//程序输出的结果是什么?
这里为什么只有a变了,Why haven't the others changed,Here is because when wea=0通过&&The judgment behind whether it is true or false,The overall result is false,Therefore, the subsequent auto-increment operation is not performed.
条件操作符
逗号表达式
//代码3 a = get_val();
count_val(a);
while (a > 0)
{
a = get_val();
count_val(a);
}Here we will feel that the code is a bit redundant
while (a = get_val(), count_val(a), a>0) {
//业务处理
}If you change it to this, you will find that the code is obviously simplified.
隐式类型转换
表达式的整型运算要在CPU的相应运算器件内执行,CPU内整型运算器(ALU)的操作数的字节长度 一般就是int的字节长度,同时也是CPU的通用寄存器的长度. 因此,即使两个char类型的相加,在CPU执行时实际上也要先转换为CPU内整型操作数的标准长度.通用CPU(general-purpose CPU)是难以直接实现两个8比特字节直接相加运算(虽然机器指令 中可能有这种字节相加指令).所以,表达式中各种长度可能小于int长度的整型值,都必须先转 换为int或unsigned int,然后才能送入CPU去执行运算.
//实例1
char a,b,c;
...
a = b + c;//负数的整形提升char c1 = -1;变量c1的二进制位(补码)中只有8个比特位:1111111因为 char 为有符号的 char所以整形提升的时候,高位补充符号位,即为1提升之后的结果是:11111111111111111111111111111111//正数的整形提升char c2 = 1;变量c2的二进制位(补码)中只有8个比特位:00000001因为 char 为有符号的 char所以整形提升的时候,高位补充符号位,即为0提升之后的结果是:00000000000000000000000000000001//无符号整形提升,高位补0
int main()
{
char a = 0xb6;
short b = 0xb600;
int c = 0xb6000000;
if(a==0xb6)
printf("a");
if(b==0xb600)
printf("b");
if(c==0xb6000000)
printf("c");
return 0; }
int main()
{
char c = 1;
printf("%u\n", sizeof(c));
printf("%u\n", sizeof(+c));
printf("%u\n", sizeof(-c));
return 0;
}
算术转换
long doubledoublefloatunsigned long intlong intunsigned intint
操作符的属性

int fun()
{
static int count = 1;
return ++count;
}
int main()
{
int answer;
answer = fun() - fun() * fun();
printf("%d\n", answer);//输出多少?
return 0;
}
int main()
{
int i = 1;
int ret = (++i) + (++i) + (++i);
printf("%d\n", ret);
printf("%d\n", i);
return 0; }边栏推荐
- The most complete Redis basic + advanced project combat summary notes in history
- Gxlcms audio novel system/novel listening system source code
- 2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】
- HCIP第十六天
- StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
- IDEA 连接 数据库
- 解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead
- CISP-PTE Zhenti Demonstration
- 力扣题(3)—— 无重复字符的最长子串
- 关于XML的学习(一)
猜你喜欢

MySQL进阶sql性能分析

navicat新建数据库

【2022-05-31】JS逆向之易企秀

Chapter 8 Intermediate Shell Tools II

MySQL 8.0.29 set and modify the default password

DistSQL 深度解析:打造动态化的分布式数据库

Go1.18升级功能 - 泛型 从零开始Go语言

MySQL压缩包方式安装,傻瓜式教学

2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】

Solve npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead
随机推荐
navicat无法连接mysql超详细处理方法
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题
MySQL compressed package installation, fool teaching
Installation and use of cnpm
MySQL user authorization
【MySQL】MySQL中对数据库及表的相关操作
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
Py之pdpbox:pdpbox的简介、安装、案例应用之详细攻略
Navicat cannot connect to mysql super detailed processing method
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
MySQL 灵魂 16 问,你能撑到第几问?
【Summary】机器人方法汇总
通过对抗性知识蒸馏压缩深度图神经网络
2022.7.28
小心你的字典和样板代码
PhpMetrics usage
ML之shap:基于FIFA 2018 Statistics(2018年俄罗斯世界杯足球赛)球队比赛之星分类预测数据集利用RF随机森林+计算SHAP值单样本力图/依赖关系贡献图可视化实现可解释性之攻略
openim支持十万超级大群
解决一个Mysql的utf8编码导致的问题
