ChenYilong / ChenYilong/iOSBlog

计算机进制的基础知识

Open
#24 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Objective-C
Stars
1.1k
Forks
184
PR merge metrics
No merged PRs in 30d

Description

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1s6euyxoj31hc0u0gm1.jpg)

开篇先给大家讲个冷笑话, 暖场一下:

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1tvw7r8cj30pt0nv76y.jpg)

下面正式开始吧.

## 进制基础

在网上找到一个讲进制讲得比较清楚的视频, 分享给大家.
[视频地址 ]( https://mp.weixin.qq.com/s/Ocgwn9T7bqJeuWT8NZtd6g "")

分几个章节:

```Java
./
├── 01_进制基础知识
├── 02_R进制到十进制的转换
├── 03_十进制到R进制的转换
├── 04_进制间的快速转换
├── 05_Java内置的进制转换
├── 06_有符号数据表示法
├── 07_强制转换之数据溢出
├── 08_浮点数的进制转换
├── 09_浮点数存储
└── 10_浮点数的运算
```

里面的代码演示部分用的 `Java`. 也是较为简单的 `Java` 语法, 如果你对 `Java` 不太熟悉也能看懂.

## PPT

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qt0dewdj31hc0u0q36.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qqsdvgcj31hc0u0wep.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qt80jggj31hc0u00sy.jpg)

![06](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qtu6uh0j31hc0u00sx.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qucfhjjj31hc0u0t93.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qukijajj31hc0u0q36.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qusumvqj31hc0u0aan.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qv4jbo9j31hc0u0t98.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qvdhae1j31hc0u03yv.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qvwa6g3j31hc0u0wew.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qw1zsmoj31hc0u0dg2.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qwd5toij31hc0u0aah.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qwsckvsj31hc0u0q3a.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qx066bgj31hc0u0t92.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qxbcpfuj31hc0u00t5.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1qxje1bkj31hc0u0mxl.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r04ykzvj31hc0u074w.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r0bzbkgj31hc0u0q3b.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r0nmcz1j31hc0u074t.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r0vncahj31hc0u0755.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r147plvj31hc0u00th.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r1c8tb9j31hc0u03z2.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r1kdu2sj31hc0u0t8q.jpg)

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r1sw1jyj31hc0u03yj.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r26nxkuj31hc0u0dg7.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r2lbhazj31hc0u0aab.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r2ro7yhj31hc0u0ab0.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r2y3whtj31hc0u00su.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r34vtiqj31hc0u0myf.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r399v3sj31hc0u0q2y.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r3fcdsmj31hc0u0gmy.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r3le7n4j31hc0u0wf2.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r45kk06j31hc0u0mxq.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r4ak8arj31hc0u00su.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r4gt25qj31hc0u0glv.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r4kb4drj31hc0u00ss.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r4pqnbrj31hc0u00tj.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r4uql99j31hc0u0wf8.jpg)

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r4ywt2mj31hc0u03zk.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r54l3q1j31hc0u074d.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r5d1rgtj31hc0u0dgs.jpg)

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r5htbnpj31hc0u0wg3.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r5n50xbj31hc0u00t2.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ge1r5w0li7j31hc0u0gm5.jpg)

## 代码

请点击“查看原文”查看

### Demo1
```Java

/*
Demo1
练习:在控制台输出下面的数据,看结果是多少
0b10000000000
02000
0x400
*/
public class Demo01 {
public static void main(String[] args) {
System.out.println(0b10000000000);
System.out.println(02000);
System.out.println(0x400);
}
}
```

### Demo2

```Java
/*
java.lang.Integer 类中的静态方法:

public static String toBinaryString(int i):在基数2中返回整数参数的字符串表示形式为无符号整数
public static String toOctalString(int i):在基数8中返回整数参数的字符串表示形式为无符号整数
public static String toHexString(int i):返回整数参数的字符串表示形式,作为16位中的无符号整数

public static String toString(int i,int radix):返回由第二个参数指定的基数中的第一个参数的字符串表示形式
*/
public class Demo02 {
public static void main(String[] args) {
// public static String toBinaryString(int i):在基数2中返回整数参数的字符串表示形式为无符号整数
System.out.println(Integer.toBinaryString(42));
// public static String toOctalString(int i):在基数8中返回整数参数的字符串表示形式为无符号整数
System.out.println(Integer.toOctalString(42));
// public static String toHexString(int i):返回整数参数的字符串表示形式,作为16位中的无符号整数
System.out.println(Integer.toHexString(42));
System.out.println("-------------------------------");

// public static String toString(int i,int radix):返回由第二个参数指定的基数中的第一个参数的字符串表示形式
System.out.println(Integer.toString(0b101010,10));
System.out.println(Integer.toString(052,10));
System.out.println(Integer.toString(0x2a,10));
System.out.println(Integer.toString(0x2A,10));
}
}

```

### Demo3

```Java
/*
请问下列代码是否有问题?如果有问题,如何解决呢?解决完毕之后,结果是多少呢?
byte b = 130;
*/
public class Demo03 {
public static void main(String[] args) {
byte b = (byte)130;
System.out.println(b);

/*
130默认是int类型的
十进制的数据转为二进制数据:
原码:00000000 00000000 00000000 10000010
反码:00000000 00000000 00000000 10000010
补码:00000000 00000000 00000000 10000010

截取操作:
补码:1 0000010
反码:1 0000001
原码:1 1111110

64 + 32 + 16 + 8 + 4 + 2 = 126
-126
*/
}
}

```

### Demo 4

```Java

import java.math.BigDecimal;

/*
浮点数运算
*/
public class Demo04 {
public static void main(String[] args) {
System.out.println(2.0f - 1.5f);
/*
十进制:2.0
二进制:10.0
规范化表示:1.00000000000000000000000 * 2 ^ 1
存储:
符号:0
M:00000000000000000000000
E:1 + 127 = 128 = 10000000

0 10000000 00000000000000000000000

十进制:1.5
0.5 * 2 = 1.0 1
二进制:1.1
规范化表示:1.10000000000000000000000 * 2 ^ 0
存储:
符号:0
M:10000000000000000000000
E:0 + 127 = 127 = 01111111

0 01111111 10000000000000000000000

计算:
1.000000000000000000000000
0.110000000000000000000000
0.010000000000000000000000 * 2 ^ 1

规范化表示:1.00000000000000000000000 * 2 ^ -1

0.5的存储?
十进制:0.5
0.5 * 2 = 1.0 1
二进制:0.1
规范化表示:1.00000000000000000000000 * 2 ^ -1

规范化表示:1.00000000000000000000000 * 2 ^ -1
*/
System.out.println(2.0f - 1.3f);
/*
十进制:2.0
二进制:10.0
规范化表示:1.00000000000000000000000 * 2 ^ 1
存储:
符号:0
M:00000000000000000000000
E:1 + 127 = 128 = 10000000

0 10000000 00000000000000000000000

十进制:1.3
0.3 * 2 = 0.6 0
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
...
二进制:1.0100110011001100110011001
规范化表示:1.01001100110011001100110 * 2 ^ 0
存储:
符号:0
M:01001100110011001100110
E:0 + 127 = 127 = 01111111

0 01111111 01001100110011001100110

计算:
1.000000000000000000000000
0.101001100110011001100110
0.010110011001100110011010 * 2 ^ 1

规范化表示:1.01100110011001100110100 * 2 ^ -1

0.7的存储?
十进制:0.7
0.7 * 2 = 1.4 1
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
...
二进制:0.1011001100110011001100110
规范化表示:1.01100110011001100110011 * 2 ^ -1

规范化表示:1.01100110011001100110100 * 2 ^ -1
*/
System.out.println(2.0f - 1.4f);
/*
十进制:2.0
二进制:10.0
规范化表示:1.00000000000000000000000 * 2 ^ 1
存储:
符号:0
M:00000000000000000000000
E:1 + 127 = 128 = 10000000

0 10000000 00000000000000000000000

十进制:1.4
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
...
二进制:1.011001100110011001100110
规范化表示:1.01100110011001100110011 * 2 ^ 0
存储:
符号:0
M:01100110011001100110011
E:0 + 127 = 127 = 01111111

0 01111111 01100110011001100110011

计算:
1.000000000000000000000000
0.101100110011001100110011
0.010011001100110011001101 * 2 ^ 1

规范化表示:1.00110011001100110011010 * 2 ^ -1

0.6的存储?
十进制:0.6
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
0.6 * 2 = 1.2 1
0.2 * 2 = 0.4 0
0.4 * 2 = 0.8 0
0.8 * 2 = 1.6 1
...
二进制:0.1001100110011001100110011001
规范化表示:1.00110011001100110011010 * 2 ^ -1 0舍1入

规范化表示:1.00110011001100110011010 * 2 ^ -1
*/
/*
0.5
0.70000005
0.6
*/

BigDecimal bd1 = new BigDecimal("2.0");
BigDecimal db2 = new BigDecimal("1.3");
BigDecimal bd = bd1.subtract(db2);
System.out.println(bd);
}

```




Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.