当前位置:网站首页>悠漓带你玩转C语言(详解操作符1)
悠漓带你玩转C语言(详解操作符1)
2022-08-11 10:42:00 【陆悠漓】
1. 操作符分类:
算术操作符
移位操作符
位操作符
赋值操作符
单目操作符
关系操作符
逻辑操作符
条件操作符
逗号表达式
下标引用、函数调用和结构成员
2. 算术操作符
+ - * / %
3. 移位操作符
<< 左移操作符
>> 右移操作符
注:移位操作符的操作数只能是整数。
左移操作符
移位规则:
左边丢弃,右边补0
注:移位操作符的操作数只能是整数。
//符号位:
//0 - 正数
//1 - 负数
//二进制有三种表达方式
//原码
//反码(原码的符号不变,其他位置按位取反得到的就是反码)
//补码(反码+1就是补码)
int main()
{
//正整数的原码、反码、补码是相同的
int a = 5;//5
int b = a << 1;//10
//a(5)的原码、反码、补码
//原码: 00000000 00000000 00000000 00000101
//反码: 00000000 00000000 00000000 00000101
//补码: 00000000 00000000 00000000 00000101
//b(10)的原码、反码、补码
//原码: 00000000 00000000 00000000 00001010
//反码: 00000000 00000000 00000000 00001010
//补码: 00000000 00000000 00000000 00001010
//负数的原码、反码、补码是要计算的
int c = -5;//-5
int d = c << 1;//-10
//c(-5)的原码、反码、补码
//原码: 10000000 00000000 00000000 00000101
//反码: 11111111 11111111 11111111 11111010
//补码: 11111111 11111111 11111111 11111011
//如何得出d呢?
//将a的补码左边丢弃,右边补0,得到
// 11111111 11111111 11111111 11110110
//
//
//d(-10)的原码、反码、补码
//补码: 11111111 11111111 11111111 11110110
//反码: 11111111 11111111 11111111 11110101
//原码: 10000000 00000000 00000000 00001010
printf("%d\n", a);
printf("%d\n", b);
printf("%d\n", c);
printf("%d\n", d);
return 0;
}
右移操作符
移位规则:
- 逻辑右移:右边丢弃,左边补0
- 算数右移:右边丢弃,左边补原符号位
int main()
{
int a = 5;
int b = a >> 1;
//a(5)的原码、反码、补码
//原码: 00000000 00000000 00000000 00000101
//反码: 00000000 00000000 00000000 00000101
//补码: 00000000 00000000 00000000 00000101
//如何得出d呢?
//将a的补码右边丢弃,左边补0,得到
// 00000000 00000000 00000000 00000010
int c = -5;//-5
int d = c >> 1;//-3
//c(-5)的原码、反码、补码
//原码: 10000000 00000000 00000000 00000101
//反码: 11111111 11111111 11111111 11111010
//补码: 11111111 11111111 11111111 11111011
//在vs中采用算数右移,这个取决于编译器
//d(-3)的原码、反码、补码
//补码: 11111111 11111111 11111111 11111101
//反码: 11111111 11111111 11111111 11111100
//原码: 10000000 00000000 00000000 00000011
printf("%d\n", a);
printf("%d\n", b);
printf("%d\n", c);
printf("%d\n", d);
return 0;
}
警告 : 对于移位运算符,不要移动负数位,这个是标准未定义的。 例如:
int num = 10;
num >> -1;//error
4. 位操作符
位操作符有:
& //按(二进制)位与
| //按(二进制)位或
^ //按(二进制)位异或
注:他们的操作数必须是整数
& ------------ 按位与
& --> 01/10 --> 0
00 --> 0 11 --> 1
int main()
{
int a = 3;
int b = -5;
int c = a & b;
//a的补码 00000000 00000000 00000000 00000011
//b的原码 10000000 00000000 00000000 00000101
// 反码 11111111 11111111 11111111 11111010
// 补码 11111111 11111111 11111111 11111011
// 00000000 00000000 00000000 00000011
//& --> 01/10 --> 0
// 00 --> 0 11 --> 1
//a & b
//00000000 00000000 00000000 00000011(补码) --> 3
//正数原反补码相同
printf("c的结果是:%d\n", c);
return 0;
}
| --------------- 按位或
| --> 01/10 --> 1
00 --> 0 11 --> 1
int main()
{
int a = 3;
int b = -5;
int c = a | b;
//a的补码 00000000 00000000 00000000 00000011
//b的原码 10000000 00000000 00000000 00000101
// 反码 11111111 11111111 11111111 11111010
// 补码 11111111 11111111 11111111 11111011
// 00000000 00000000 00000000 00000011
//| --> 01/10 --> 1
// 00 --> 0 11 --> 1
// 11111111 11111111 11111111 11111011(补码)
// 11111111 11111111 11111111 11111010(反码)
//a | b --> 10000000 00000000 00000000 00000101(原码) --> -5
printf("c的结果是:%d\n", c);
return 0;
}
^ ----------------- 按位异或
^ --> 00/11 --> 0
01/10 --> 1
int main()
{
int a = 3;
int b = -5;
int c = a ^ b;
//a的补码 00000000 00000000 00000000 00000011
//b的原码 10000000 00000000 00000000 00000101
// 反码 11111111 11111111 11111111 11111010
// 补码 11111111 11111111 11111111 11111011
// 00000000 00000000 00000000 00000011
// ^ --> 00/11 --> 0
// 01/10 --> 1
// 11111111 11111111 11111111 11111000(补码)
// 11111111 11111111 11111111 11110111(反码)
//a ^ b --> 10000000 00000000 00000000 00001000(原码) --> -8
//
printf("%d\n", c);
return 0;
}
a ^ a = 0
0 ^ a = a
不能创建临时变量(第三个变量),实现两个数的交换
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
a = a^b;
b = a^b;
a = a^b;
printf("a = %d b = %d\n", a, b);
return 0; }
编写代码实现:求一个整数存储在内存中的二进制中1的个数
#include<stdio.h>
int main()
{
int num = 0;
scanf("%d", &num);
int i = 0;
int count = 0;
for (i = 0; i < 32; i++)
{
if (1 == ((num >> i) & 1))
{
count++;
}
}
printf("%d\n", count);
return 0;
}
边栏推荐
- 当科学家决定搞点“花里胡哨”的东西
- Qihua stores the future and interprets the origin of distributed
- Revelations!The former Huawei microservice expert wrote 500 pages of practical notes on the landing architecture, which has been open sourced
- 【每日一题】640. 求解方程
- 本地开发好的 SAP UI5 应用部署到 ABAP 服务器时,中文字符变成乱码的原因分析和解决方案
- What is the difference between the qspi interface and the ordinary four-wire SPI interface?
- 华为WLAN技术:AC/AP 实验
- Convolutional Neural Network System,Convolutional Neural Network Graduation Thesis
- 7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
- SAP Product Enhancement Technology Review
猜你喜欢
OAK-FFC Series Product Getting Started Guide
宝塔计划任务执行周期设置【秒】为定时单位【或者更小】
Cholesterol-PEG-FITC, Fluorescein-PEG-CLS, Cholesterol-PEG-Fluorescein water-soluble
解决 Pocess finished with exit code 1 Class not found 和 Command line is too long. Shorten the command
【Mask2Former】 解决代码中一些问题
和为s的连续正数序列
你觉得程序员是一个需要天赋的职业吗?
华为WLAN技术:AC/AP 实验
How to improve the efficiency of telecommuting during the current epidemic, sharing telecommuting tools
openresty概述及Lua语言的嵌入
随机推荐
【Prometheus】 Grafana数据与可视化
日志使用注意事项和建议
Gold Transfer(树上倍增)
使用.NET简单实现一个Redis的高性能克隆版(七-完结)
字符函数和字符串函数的进阶
我用这个操作,代码可读性提升一个档次
chrome设置为深色模式(包括整个网页)
困扰所有SAP顾问多年的问题终于解决了
人是怎么废掉的?人是怎么变强的?
同态加密简介HE
7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
如何开手续费低靠谱正规的期货账户呢?
数据库内核面试中我不会的问题(4)
OAK-FFC Series Product Getting Started Guide
[Ext JS]11.14 SimXhr.js?_dc=1659315492151:65 Uncaught TypeError problem analysis and solution
Neuropathic pain classification picture Daquan, neuropathic pain classification
中小企业如何实施MES管理系统
阿里二面:JVM调优你会吗?
Simple strokes on the Internet
保证金监控中心保证期货开户和交易记录