zoukankan      html  css  js  c++  java
  • 负数、原码、反码、补码

        @Test
        public void testName() throws Exception {
            /*
             * 负整数的二进制是怎么存的?
             * 在计算机中,负数以其正值的补码形式表达。
             * 什么叫补码呢?这得从原码,反码说起。
             * -----------------------------------------------
             * 5的原码(概念:一个整数,按照绝对值大小转换成的二进制数,称为原码。)
             * 00000000 00000000 00000000 00000101    5
             * 
             * 5的反码(概念:将二进制数按位取反,所得的新二进制数称为原二进制数的反码。取反操作指:原为1,得0;原为0,得1。(1变0; 0变1)。反码是相互的。)
             * 11111111 11111111 11111111 11111010
             * 
             * 5的补码(概念:反码加1称为补码。)
             * 11111111 11111111 11111111 11111011    -5
             */
            System.out.println(Integer.toBinaryString(5));      // 5的原码:101
            System.out.println(Integer.toBinaryString(~5));     // 5的反码:11111111111111111111111111111010
            System.out.println(Integer.toBinaryString(~5 + 1)); // 5的补码:11111111111111111111111111111011
            System.out.println(Integer.toBinaryString(-5));     // -5的二进制,和5的补码相同:11111111111111111111111111111011
        }
  • 相关阅读:
    《分布式系统关注点——数据一致性(上篇)》阅读笔记
    2.23寒假学习记录
    2.22寒假学习记录
    2.21寒假学习记录
    2.20寒假学习记录
    2.19寒假学习记录
    2.18寒假学习记录
    2.17寒假学习记录
    2.17周一毕设改进计划
    2.16寒假学习记录
  • 原文地址:https://www.cnblogs.com/zj0208/p/8032876.html
Copyright © 2011-2022 走看看