当前位置:网站首页>C语言进制转换以及原补反码位运算介绍
C语言进制转换以及原补反码位运算介绍
2022-07-27 05:03:00 【我真不是二次元】
使用进制的原因
- 因为现在的CPU只能识别高低电平,只能对二进制的数据进行计算
- 虽然二进制的数据可以直接被CPU识别计算,但是不方便书写和记录,因为把二进制的数据转换成八进制是为了方便记录到文档中
- 由于CPU的位数发展不断增加,由8位逐渐发展到现在的64位,因此八进制就逐渐不能满足需求了,所以发展出了十六进制,但是由于历史原因八进制还不能完全淘汰
进制转换
1.十进制转二进制
求余法:用2对数据求余,然后继续对商求余,直到商为0结束,该过程中产生的余数就是该数据的二进制(逆序)
n%2==1\0
商%2 ==1\0
…
直到商为0
余数的逆序就是n的二进制求权法:从高位到低位,数据-2^(n-1)如果够减,那么第n位就是1,否则就是0,直到把数据减完为0
128 64 32 16 8 4 2 1
86(01010110)
72(01001000)
112(01110000)
176(10110000)
2.二进制转十进制
每位的2^(n-1)求和
10010010 145
3.二进制转八进制
从低位开始,每三位二进制位对应一位八进制
二进制: 1 001 010 101 110
八进制: 1 1 2 5 6
4.二进制转十六进制
从低位开始,每四位二进制位对应一位十六进制
二进制: 1 0010 1010 1110
十六进制 1 2 A E
在C语言中,以0开头的数是八进制数以0x开头的数是十六进制
%c 072
%o 以八进制显示数据
%x 以十六进制显示数据
%#o %#x 把数据对应的前缀显示出来
原码、反码、补码
原码
数据的二进制
反码
符号位1是负数0是正数
正数的反码就是它的原码
负数的反码就是它原码的除符号位外,其他位按位求反
补码
正数的补码就是它的原码
负数的补码是它的反码+1
注意:内存中所有数据的存储都是以补码形式存储的
1、负数转换成二进制
2、符号位不变,其余按位求反
3、反码+1得到补码
-127
原码:1111 1111
反码:1000 0000
补码:1000 0001
补码转数据
无符号补码直接转换成十进制
有符号最高位是0,说明是正数,也直接转换成十进制
有符号且最高位是1:
1、补码-1得到反码
2、符号位不变,其他位按位求反得到原码
3、原码转换成十进制数据
1111 1111 补码 有符号
1111 1110 反码
1000 0001 原码
-1 十进制
位运算符简介
A & B 按位相与 有假及假
00110010
11001010
--------
00000010 0x2
A | B 按位相或 有真及真
00110010
11001010
--------
11111010
~A 按位求反
00110010
11001101
A ^ B 按位异或 相同为0,相异为1
00110010 0x32
11001101 0xCA
--------
11111000 0xf8
A<<n 把A的补码左移n位,左边丢弃右边补0
0x32 << 3
00110010 0X32
10010000 0x90
A>>n 把A的补码右移n位,右边丢弃,左边补符号位(不是符号位补0,是符号位补1)
00110010 0X32
11110110
练习:输入一个整数,把它的4~7位设置为1010,其他位不能变
101101011 0111 0101 num
101101011 1010 0101 (num & ~(oxf<4)) | (0xA<<4)
000000000 1010 0000 0xA<<4
101101011 0000 0101 num & ~(oxf<4)
111111111 0000 1111 ~(oxf<4)
000000000 1111 0000 oxf<<4
000000000 0000 1111 oxf
边栏推荐
- Multiplication sorting in torch, * & torch. Mul () & torch. MV () & torch. Mm () & torch. Dot () & @ & torch. Mutmal ()
- 秒杀系统设计
- 简化JDBC的MyBits框架
- Redis lock
- Li Hongyi machine learning team learning punch in activity day03 --- error and gradient decline
- B1023 group minimum
- 后台订单管理
- Flask的传参以及返回的响应
- How to quickly and effectively solve the problem of database connection failure
- C WPF uses listbox to implement ruler control
猜你喜欢

Redis cluster

The receiver sets the concurrency and current limit

Hi3516dv300 environment setup

Pinball games

The concept of cloud native application and 15 characteristics of cloud native application

Carmaker quick start lesson 4 developing 48V P1 hybrid system

后台频道组管理功能实现
![[CSAPP] Application of bit vectors | encoding and byte ordering](/img/96/344936abad90ea156533ff49e74f59.gif)
[CSAPP] Application of bit vectors | encoding and byte ordering

李宏毅机器学习组队学习打卡活动day04---深度学习介绍和反向传播机制

Differences among bio, NiO and AIO
随机推荐
B1025 reverse linked list*******
B1023 group minimum
Sparse array → saving and continuation of Gobang
Database design - relational data theory (ultra detailed)
BIO、NIO、AIO区别
【codeforces round#800 B. Paranoid String】DP
C语言初阶——分支语句(if,switch)
Li Hongyi machine learning team learning punch in activity day02 --- return
[codeforces round 800 D question fake plastic trees] greedy on the tree
Numpy data type conversion
用户的管理-限制
用户-注册-登录
Flask登录实现
Bean's life cycle & dependency injection * dependency auto assembly
Pinball games
LeetCode之268.Missing number
The provision of operation and maintenance manager is significantly affected, and, for example, it is like an eep command
项目登录注册思路
C WPF uses listbox to implement ruler control
B1021 single digit statistics