Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

计算机进制的基础知识 #24

Open
ChenYilong opened this issue Apr 21, 2020 · 1 comment
Open

计算机进制的基础知识 #24

ChenYilong opened this issue Apr 21, 2020 · 1 comment

Comments

@ChenYilong
Copy link
Owner

ChenYilong commented Apr 21, 2020

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

下面正式开始吧.

进制基础

在网上找到一个讲进制讲得比较清楚的视频, 分享给大家.
视频地址

分几个章节:

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

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

PPT



06




































代码

请点击“查看原文”查看

Demo1

/*
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.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

/*
   请问下列代码是否有问题?如果有问题,如何解决呢?解决完毕之后,结果是多少呢?
   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

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);
   }


@ChenYilong
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant