当前位置:网站首页>Introduction of C language base conversion and original complement inverse code point operation
Introduction of C language base conversion and original complement inverse code point operation
2022-07-27 05:37:00 【I'm really not a second dimension】
Hexadecimal conversion
Reason for using base
- Because of the present CPU Only high and low levels can be recognized , Only binary data can be calculated
- Although binary data can be directly CPU Identify calculations , But it is not convenient to write and record , Because converting binary data into octal is to facilitate recording in documents
- because CPU The number of digits is increasing , from 8 Bit gradually developed to the present 64 position , Therefore, octal cannot meet the demand gradually , So we developed hexadecimal , However, due to historical reasons, octal cannot be completely eliminated
Hexadecimal conversion
1. Decimal to binary
Remainder method : use 2 Balance the data , Then continue to seek surplus from the business , Until Shang Wei 0 end , The remainder generated in this process is the binary of the data ( The reverse )
n%2==1\0
merchant %2 ==1\0
…
Until Shang Wei 0
The reverse order of the remainder is n Binary systemRight seeking method : From high to low , data -2^(n-1) If it's enough , So the first n Bit is 1, Otherwise, it would be 0, Until the data is reduced to 0
128 64 32 16 8 4 2 1
86(01010110)
72(01001000)
112(01110000)
176(10110000)
2. Binary to decimal
Everyone's 2^(n-1) Sum up
10010010 145
3. Binary to octal
Starting from the low , Every three binary bits corresponds to one octal
Binary system : 1 001 010 101 110
octal : 1 1 2 5 6
4. Binary to hexadecimal
Starting from the low , Every four binary bits corresponds to one hexadecimal
Binary system : 1 0010 1010 1110
Hexadecimal 1 2 A E
stay C In language , With 0 The first number is octal number 0x The first number is hexadecimal
%c 072
%o Display data in octal
%x Display data in hexadecimal
%#o %#x Display the prefix corresponding to the data
Original code 、 Inverse code 、 Complement code
Original code
Binary of data
Inverse code
Sign bit 1 It's a negative number 0 Positive number
The inverse code of a positive number is its original code
The inverse code of a negative number is its original code except for the sign bit , Other bits are inversed bit by bit
Complement code
The complement of a positive number is its original code
The complement of a negative number is its inverse +1
Be careful : All data in memory is stored in the form of complement
1、 Negative numbers are converted to binary
2、 The sign bits remain the same , The rest are inversed by position
3、 Inverse code +1 Get the complement
-127
Original code :1111 1111
Inverse code :1000 0000
Complement code :1000 0001
Complement to data
Unsigned complement is directly converted to decimal
The highest sign is 0, The description is positive , It is also directly converted to decimal
Signed and the highest bit is 1:
1、 Complement code -1 Get the inverse
2、 The sign bits remain the same , Other bits are inversed bit by bit to get the original code
3、 Convert the original code into decimal data
1111 1111 Complement code A signed
1111 1110 Inverse code
1000 0001 Original code
-1 Decimal system
Introduction to bitwise operators
A & B Press phase and There are false and false
00110010
11001010
--------
00000010 0x2
A | B Press phase or True and true
00110010
11001010
--------
11111010
~A Reverse by position
00110010
11001101
A ^ B Bitwise XOR Same as 0, Dissimilarity is 1
00110010 0x32
11001101 0xCA
--------
11111000 0xf8
A<<n hold A The complement of moves left n position , Discard the left and fill the right 0
0x32 << 3
00110010 0X32
10010000 0x90
A>>n hold A To the right n position , On the right side of the discarded , Fill in the sign on the left ( Not sign bit complement 0, It is a sign complement 1)
00110010 0X32
11110110
practice : Enter an integer , It's 4~7 Bit is set to 1010, Other bits cannot be changed
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
边栏推荐
- 使用vertical-align不能让图片和文字垂直居中对齐
- 强制登录,七牛云上传图片
- qsort — c语言中自带的排序函数(附带void*、回调函数知识点
- 分享一道关于程序编译过程的选择题(内含编译过程浅谈,符号表的形成合并过程)
- 分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
- 2021 Niuke multi school training camp 5 (question b)
- Cenos7更新MariaDB
- 李宏毅机器学习组队学习打卡活动day01---机器学习介绍
- 程序环境和预处理(上):一个程序是怎么成功运行的?
- Common commands in CONDA and pip environments
猜你喜欢

Flask的使用

The receiver sets the concurrency and current limit

Sharing force buckle-189. Three solutions of rotation array

C language string function: StrCmp, strncpy, strncat, strncmp, strstr, strtok, strError

SQL database → constraint → design → multi table query → transaction

DNSmasq使用总结

如何将Excel表格中的多列内容合并到一列

初识C语言——常量、变量

程序环境和预处理(上):一个程序是怎么成功运行的?

用户的管理-限制
随机推荐
Trying to evolve_ My first CSDN blog
Flask请求数据获取与响应
Share a multiple-choice question about variables (including global variables, local variables, the scope of variables, and life cycle knowledge points)
Pinball games
C语言函数入门介绍
小米商城项目_注册
分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
后台优惠券管理
Differences among bio, NiO and AIO
Pytorch installation new pit
Notes series k8s orchestration MySQL container - stateful container creation process
迭代器和生成器
Flask对模型类的操作
Common commands in CONDA and pip environments
js进阶知识—函数
JWT认证及登录功能实现,退出登录
用户登录-以及创建、验证短信验证码
Li Hongyi machine learning team learning punch in activity day02 --- return
Enumeration class implements singleton mode
C语言做一个小迷宫