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

也满足补码的规则。
东莞网站建设www.zg886.cn

发布了0 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ting2909/article/details/102372529