当前位置:网站首页>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 Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
- Py之pdpbox:pdpbox的简介、安装、案例应用之详细攻略
- d使用among的问题
- 【无标题】
- “由于找不到MSVCP140.dll,无法继续执行代码,重新安装程序可能会解决此问题等”解决方案
- cnpm installation steps
- MySQL 8.0.29 设置和修改默认密码
- Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
- TCP 连接 三次握手 四次挥手
- 【Summary】机器人方法汇总
猜你喜欢

【Untitled】

The mysql time field is set to the current time by default

IJCAI2022教程 | 口语语言理解:最新进展和新领域

MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)

c语言进阶篇:指针(五)

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

解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead

PhpMetrics 使用

QT开发简介、命名规范、signal&slot信号槽

【微信小程序】小程序突破小程序二维码数量限制
随机推荐
MySQL 5.7 detailed download, installation and configuration tutorial
IJCAI2022教程 | 口语语言理解:最新进展和新领域
2022/07/30 学习笔记 (day20) 面试题积累
Rust编译报错:error: linker `cc` not found
MySQL Soul 16 Questions, How Many Questions Can You Last?
ClickHouse to create a database to create a table view dictionary SQL
代码越写越乱?那是因为你没用责任链
小心你的字典和样板代码
通过社交媒体建立个人IP的 5 种行之有效的策略
力扣题(3)—— 无重复字符的最长子串
DistSQL 深度解析:打造动态化的分布式数据库
mysql remove duplicate data
MySQL 用户授权
Solve the problem of centos8 MySQL password ERROR 1820 (HY000) You must reset your password using the ALTER USER
Go1.18升级功能 - 泛型 从零开始Go语言
The Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
mysql跨库关联查询(dblink)
MySQL进阶sql性能分析
openim支持十万超级大群
Installation and use of cnpm
