当前位置:网站首页>按位逻辑运算符
按位逻辑运算符
2022-07-06 08:22:00 【踏破铁鞋无觅处,bug一写一上午】
一、按位取反:~
在计算机中,数据都是按照二进制进行存储的,即便有了原码、反码、补码。而二进制数在内存中是以补码的形式存储的。
按位取反是将补码的每一位都取反,即0变1,1变0;
#include <stdio.h>
int main()
{
char a = 5;
printf("%d", ~a);
return 0;
}
结果为-6;
正整数:5
正数的原码、反码、补码相同:00000101
按位取反:11111010 //此时取反的结果为原码,还需要将其转化为补码
反码:10000101
补码:10000110
结果:-6
负整数:-5
原码:10000101
反码:11111010
补码:11111011
按位取反:00000100 //此时取反的结果为原码,因为原码为正数,原反补相同
结果:4
正数按位取反等于其本身+1的负数
负数按位取反等于其本身+1的正数
0按位取反为-1
二、按位与:&
[规则]
0&0=0
0&1=0
1&0=0
1&1=1
#include <stdio.h>
int main()
{
char a = 5,b=9;
char c = a&b;
printf("%d", c);
return 0;
}
结果为1
5的补码:00000101
9的补码:00001001
按位与后结果:00000001 //此时的结果为原码,因为原码为正数,原反补相同
结果:1
三、按位或:|
[规则]
0|0=0
0|1=1
1|0=1
1|1=1
#include <stdio.h>
int main()
{
char a = 5,b=9;
char c = a|b;
printf("%d", c);
return 0;
}
结果为13
5的补码:00000101
9的补码:00001001
按位或后结果:00001101 //此时的结果为原码,因为原码为正数,原反补相同
结果:13
四、按位异或:^
[规则]
0^0=0
0^1=1
1^0=1
1^1=0
#include <stdio.h>
int main()
{
char a = 5,b=9;
char c = a^b;
printf("%d", c);
return 0;
}
结果为12
5的补码:00000101
9的补码:00001001
按位与或后结果:00001100 //此时的结果为原码,因为原码为正数,原反补相同
结果:12
边栏推荐
- [MySQL] log
- MySQL learning records 12jdbc operation transactions
- 从表中名称映射关系修改视频名称
- 你想知道的ArrayList知识都在这
- Analysis of Top1 accuracy and top5 accuracy examples
- Modify the video name from the name mapping relationship in the table
- [research materials] 2021 China online high growth white paper - Download attached
- Colorlog结合logging打印有颜色的日志
- 图像融合--挑战、机遇与对策
- [MySQL] database stored procedure and storage function clearance tutorial (full version)
猜你喜欢

leetcode刷题 (5.28) 哈希表

Golang DNS write casually
![[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]](/img/ae/4e616882f6d68acdf8e885843e68a3.jpg)
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers

Yyds dry goods inventory three JS source code interpretation eventdispatcher

Chinese Remainder Theorem (Sun Tzu theorem) principle and template code

Deep learning: derivation of shallow neural networks and deep neural networks

化不掉的钟薛高,逃不出网红产品的生命周期

NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
随机推荐
Migrate data from SQL files to tidb
Huawei cloud OBS file upload and download tool class
Résumé des diagrammes de description des broches de la série ESP
MySQL learning record 10getting started with JDBC
IOT -- interpreting the four tier architecture of the Internet of things
堆排序详解
【云原生】手把手教你搭建ferry开源工单系统
Online yaml to CSV tool
在 uniapp 中使用阿里图标
2022.02.13 - 238. Maximum number of "balloons"
Leetcode question brushing (5.31) string
wincc7.5下载安装教程(Win10系统)
All the ArrayList knowledge you want to know is here
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
华为云OBS文件上传下载工具类
Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
Leetcode question brushing (5.28) hash table
[2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
JS select all and tab bar switching, simple comments
LDAP應用篇(4)Jenkins接入