zoukankan      html  css  js  c++  java
  • java程序猿如何练习java版的易筋经?

    故事背景

      电视剧《天龙八部》中,阿朱易容后进入少林寺偷走了《易筋经》,她一直想把这本书送给乔峰。耿直的乔峰觉得此书来历不正,不肯接受。几番波折,这本书最后落到聚贤庄庄主游坦之手里。怪人游坦之靠着《易筋经》练就神功,后来甚至能和乔峰抗衡.

    java程序猿如何练习java版的易筋经?

     

      《易筋经》的功夫圜一身之脉络,系五脏之精神,周而不散,行而不断,气自内生,血从外润。练成此经后,心动而力发,一攒一放,自然而施,不觉其出而自出,如潮之涨,似雷之发。练那《易筋经》,便如一叶小舟于大海巨涛之中,怒浪澎湃之际,小舟自然抛高伏低,何尝用力?若要用力,又哪有力道可用?又从何处用起?

    java版的易筋经<The Java® Language Specification>

      《易筋经》练法古拙朴实,修聚而得的内力也是无可撼动,根基之稳,于「三大神功」中称得第一。修习java,内功首推jsl,招式也要练起,不然内功无法表现出来。我们举例来说,在计算机系统中,数值一律用补码来表示和存储。原因在于,使用补码,可以将符号位和数值域统一处理;同时,加法和减法也可以统一处理。此外,补码与原码相互转换,其运算过程是相同的,不需要额外的硬件电路。

    其中的术语如下:

    原码:将一个整数,转换成二进制,就是其原码。如单字节的5的原码为:0000 0101;-5的原码为1000 0101。

    反码:正数的反码就是其原码;负数的反码是将原码中,除符号位以外,每一位取反。如单字节的5的反码为:0000 0101;-5的反码为1111 1010。

    补码:正数的补码就是其原码;负数的反码+1就是补码。如单字节的5的补码00000101;-5的补码1111 1011。

    总结一句话,正数的原码=反码=补码,负数的原码!=反码,反码+1=补码

    我们来 看看JSL3中关于int的描述

    All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear. The decimal literal 2147483648 may appear only as the operand of the unary minus operator - (§15.15.4).
    It is a compile-time error if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator; or if a decimal literal of type int is larger than 2147483648 (231).
    The largest positive hexadecimal, octal, and binary literals of type int - each of which represents the decimal value 2147483647 (2^31-1) - are respectively:
    0x7fff_ffff,
    0177_7777_7777, and
    0b0111_1111_1111_1111_1111_1111_1111_1111
    The most negative hexadecimal, octal, and binary literals of type int - each of which represents the decimal value -2147483648 (-2^31) - are respectively:
    0x8000_0000,
    0200_0000_0000, and
    0b1000_0000_0000_0000_0000_0000_0000_0000
    The following hexadecimal, octal, and binary literals represent the decimal value -1:
    0xffff_ffff,
    0377_7777_7777, and
    0b1111_1111_1111_1111_1111_1111_1111_1111
    It is a compile-time error if a hexadecimal, octal, or binary int literal does not fit in 32 bits.

    注意,为了便于观察,java支持使用"_"分割2进制,8进制,16进制,还有10进制数,下面的程序编译不会报错哦

        public static void main(String[] args) {
            int i=0b1000_0000_0000_0000_0000_0000_0000_0000;
            int j=0200_0000_0000;
            int k=0x8000_0000;
            int m=500_000;        
        }

    回到原码,反码,补码来上来看:

    -1的原码:0b1000_0000_0000_0000_0000_0000_0000_0001
    
    -1的反码:0b1111_1111_1111_1111_1111_1111_1111_1110
    
    -1的补码:0b1111_1111_1111_1111_1111_1111_1111_1111

    我们来验证一下-1在计算机中是否以补码表示:

        public static void main(String[] args) {
            System.out.println(Integer.toBinaryString(-1));    
        }

    结果为:

    11111111111111111111111111111111

    同样,我们还可以验证各种类型的值,如下面程序所示:

        public static void main(String[] args) {
            System.out.println(Integer.toBinaryString((Byte.MAX_VALUE & 0xFF) + 0x100).substring(1));        
            System.out.println(Integer.toBinaryString((Byte.MIN_VALUE & 0xFF) + 0x100).substring(1));
            System.out.println(Integer.toBinaryString(((byte)5 & 0xFF) + 0x100).substring(1));
            System.out.println(Integer.toBinaryString(((byte)-5 & 0xFF) + 0x100).substring(1));
            System.out.println(Integer.toBinaryString((Character.MAX_VALUE&0xFFFF)+0x10000).substring(1));        
            System.out.println(Integer.toBinaryString((Character.MIN_VALUE&0xFFFF)+0x10000).substring(1));
            System.out.println(Integer.toBinaryString(((char)5&0xFFFF)+0x10000).substring(1));
            System.out.println(Integer.toBinaryString(((char)-5&0xFFFF)+0x10000).substring(1));
            
            System.out.println(Integer.toBinaryString((Short.MAX_VALUE&0xFFFF)+0x10000).substring(1));        
            System.out.println(Integer.toBinaryString((Short.MIN_VALUE&0xFFFF)+0x10000).substring(1));
            System.out.println(Integer.toBinaryString(((short)5&0xFFFF)+0x10000).substring(1));
            System.out.println(Integer.toBinaryString(((short)-5&0xFFFF)+0x10000).substring(1));
            
            System.out.println(Integer.toBinaryString(Integer.MAX_VALUE));        
            System.out.println(Integer.toBinaryString(Integer.MIN_VALUE));
            System.out.println(Integer.toBinaryString(5));
            System.out.println(Integer.toBinaryString(-5));
        }

    输出结果:

    01111111

    10000000

    00000101

    11111011

    1111111111111111

    0000000000000000

    0000000000000101

    1111111111111011

    0111111111111111

    1000000000000000

    0000000000000101

    1111111111111011

    1111111111111111111111111111111

    10000000000000000000000000000000

    101

    11111111111111111111111111111011

    也满足补码的规则。

    参考资料

    【1】https://baike.baidu.com/item/%E6%98%93%E7%AD%8B%E7%BB%8F/20234647?fr=aladdin

    【2】https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-HexNumeral

  • 相关阅读:
    nginx负载均衡集群
    pureftp 服务
    LVS集群之DR模式 实现
    LVS集群之NAT模式实现
    resin 安装配置
    nfs部署和优化
    电压和电流有什么关系
    自动循环播放 播放器
    有趣的匿名方法
    使用匿名委托,Lambda简化多线程代码
  • 原文地址:https://www.cnblogs.com/davidwang456/p/11630125.html
Copyright © 2011-2022 走看看