zno2

java 2 8 10 16

An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).

     int decimal = 10;
        int hexadecimal = 0x10;
        int octal = 010;
        String binary = "10";

        System.out.println(decimal);                       // 10
        System.out.println(hexadecimal);                  // 16
        System.out.println(octal);                       // 8
        System.out.println(Integer.valueOf(binary, 2)); // 2

十六进制以 0x开头

八进制以0开头

无二进制数字

值以10进制表示

十六进制数如何读

0 1 2 3 4 5 6 7 8 9 A B C D E F

小样1

0x10 读作零叉一零;不能读作零叉十(十进制的读法不能应用到十六进制会导致混乱);不能读作零叉十六(16是其对应的10进制的值)

小样2

16进制的11表示成十进制就是17

十六进制的一一表示成十进制就是十七

人类易懂的进制只有十进制,表示值时都用十进制。

 

测试

public class Test {
    public static void main(String[] args) {
//        for (long i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
//            B.v(Byte.valueOf(i + ""));
//        }
//        N.v(Byte.MAX_VALUE);
//        N.v(Byte.valueOf("1"));
//        N.v(Byte.valueOf("0"));
//        N.v(Byte.valueOf("-1"));
//        N.v(Byte.valueOf(Byte.MIN_VALUE + 1 + ""));
//        N.v(Byte.MIN_VALUE);
//
//        N.v(Short.MAX_VALUE);
//        N.v(Short.valueOf("1"));
//        N.v(Short.valueOf("0"));
//        N.v(Short.valueOf("-1"));
//        N.v(Short.valueOf(Short.MIN_VALUE + 1 + ""));
//        N.v(Short.MIN_VALUE);
//
//        N.v(Integer.MAX_VALUE);
//        N.v(Integer.valueOf("1"));
//        N.v(Integer.valueOf("0"));
//        N.v(Integer.valueOf("-1"));
//        N.v(Integer.valueOf(Integer.MIN_VALUE + 1 + ""));
//        N.v(Integer.MIN_VALUE);
//
//        N.v(Long.MAX_VALUE);
//        N.v(Long.valueOf("1"));
//        N.v(Long.valueOf("0"));
//        N.v(Long.valueOf("-1"));
//        N.v(Long.valueOf(Long.MIN_VALUE + 1 + ""));
//        N.v(Long.MIN_VALUE);
        
//        N.v(Long.valueOf(Byte.MAX_VALUE+1));
//        N.v(Long.valueOf(Byte.MIN_VALUE));
//        N.v(Long.valueOf(Short.MAX_VALUE+1));
//        N.v(Long.valueOf(Short.MIN_VALUE));
//        N.v(Long.valueOf(Integer.MAX_VALUE+1));
//        N.v(Long.valueOf(Integer.MIN_VALUE));
//        N.v(Long.valueOf(Long.MAX_VALUE+1));
//        N.v(Long.valueOf(Long.MIN_VALUE));
    }

    public static class N {
        private String b;
        private long l;
        private Class<?> c;

        private N(String b, Class<?> c) {
            this.b = b;
            this.l = Long.valueOf(b, 2);
            this.c = c;
        }

        private N(long l, Class<?> c) {
            this.l = l;
            this.b = Long.toBinaryString(l);
            this.c = c;
        }

        @Override
        public String toString() {
            int l = 0;
            if (c == Byte.class) {
                l = 8;
            } else if (c == Short.class) {
                l = 16;
            } else if (c == Integer.class) {
                l = 32;
            } else if (c == Long.class) {
                l = 64;
            }
            String _b = leftPad(b, '0', l);
            _b = _b.substring(_b.length() - l);
            return "二进制(补码):" + _b + ", 十进制:" + leftPad(this.l + "", ' ', 20);
        }

        public long getL() {
            return l;
        }

        public static N v(Object obj) {
            N b;
            if (obj instanceof Byte) {
                b = new N((Byte) obj, Byte.class);
            } else if (obj instanceof Short) {
                b = new N((Short) obj, Short.class);
            } else if (obj instanceof Integer) {
                b = new N((Integer) obj, Integer.class);
            } else if (obj instanceof Long) {
                b = new N((Long) obj, Long.class);
            } else if (obj instanceof String) {
                String s = (String) obj;
                if (s.length() <= 8) {
                    b = new N(s, Byte.class);
                } else if (s.length() <= 16) {
                    b = new N(s, Short.class);
                } else if (s.length() <= 32) {
                    b = new N(s, Integer.class);
                } else if (s.length() <= 64) {
                    b = new N(s, Long.class);
                } else {
                    b = null;
                }
            } else {
                b = null;
            }

            if (b != null) {
                System.out.println(b.toString());
            }
            return b;
        }

        private static String leftPad(String s, char c, int l) {
            if (s == null) {
                return null;
            }
            l = l - s.length();
            for (int i = 0; i < l; i++) {
                s = c + s;
            }
            return s;
        }

    }
}

 

 

 

 

 

结合资料思考:

如果只有正数和加法那么就不需要反码和补码

反码就是所有位1、0互换,x + 反[x] = 0

遗留问题是两负数相加符号位无法处理

引入补码是可以让符号位参与计算并且只用加法

 

How to work with negative numbers in binary? - 2's complement representation
In the binary system, all numbers are a combination of two digits, 0 or 1. Each digit corresponds to a successive power of 2, starting on the right.

For example, 12 in binary is 1100, as 12 = 8 + 4 = 1*2³ + 1*2² + 0*2¹ + 0*2⁰ (using scientific notation). An extended version of the binary system is the hexadecimal system (which uses base 16 instead of base 2). The latter is frequently used in many computer softwares and systems.

Learning about binary leads to many natural questions arising - what about negative numbers in the binary system? Or how do I subtract binary numbers? As we can only use 1 to show that something is present, or 0 to mean that there is a lack of that thing, there are two main approaches:

Two's complement representation, or, in other words, signed notation - the first bit tells about the sign. The convention is that a number with a leading 1 is negative, while a leading 0 denotes a positive value. In an 8-bit representation, we can write any number from -128 to 127. The name comes from the fact that a negative number is a two's complement of a positive one.

Unsigned notation - a representation that supports only positive values. Its advantage over the signed one is that, within the same 8-bit system, we can get any number from 0 up to 255.

As long as we need to add or multiply positive numbers, the unsigned notation is good enough. But, usually, the more practical solution is to work with negative numbers as well. A useful thing about the 2's complement representation is that subtraction is equivalent to an addition of a negative number, which we can handle.

How to use two's complement calculator? Two's complement converter in practice
Whenever you want to convert a decimal number into a binary value in two's complement representation, follow these steps:

Choose the number of bits in your notation. The higher value, the broader range of numbers you can input.

Write any whole decimal within the range that appears under the Decimal to binary section.

... and that's it - the 2's complement calculator will do the rest of the work! It shows the equivalent binary number, as well as its two's complement.

Do you want to estimate the outcome by hand? This is how two's complement calculator does it:

Choose the number of bits in the binaries representation. Let's assume we want values in the 8-bit system.

Write down your number, let's say 16. 16 in binary is 1 0000.

Add some leading 0's, so that the number has eight digits, 0001 0000. That's 16 in the two's complement notation.

And what about its counterpart, -16?

Switch all the digits to their opposite (0→1 and 1→0). In our case 0001 0000 → 1110 1111.

Add 1 to this value, 1110 1111 + 1 = 1111 0000.

1111 0000 in the two's complement representation is -16 in decimal notation, and is the 2's complement of 0001 0000.

Look, as long as you are proficient in switching digits and adding unity to a binary value, evaluating negative numbers in binary is not a big deal!

Turning two's complement to decimal
Our 2's complement calculator can also work the other way around - converting any two's complement to its decimal value. Let's try to convert 1011 1011, a signed binary, to decimal. There are two useful methods that help you find the outcome:

Convert this signed binary into a decimal, like normal, but multiply the leading digit by -1 instead of 1. Starting from the right:

decimal = 1*2⁰ + 1*2¹ + 0*2² + 1*2³ + 1*2⁴ + 1*2⁵ + 0*2⁶ - 1*2⁷

decimal = 1 + 2 + 8 + 16 + 32 - 128 = -69

We can see that the first digit is 1, so our number is negative. First, find its two's complement, then convert the value to a decimal, and come back to the original value:

Reverse digits, 1011 1011 → 0100 0100.
Add a unity, 0100 0100 + 1 = 0100 0101.
Convert to a decimal (starting from the right), decimal = 1*2⁰ + 0*2¹ + 1*2² + 0*2³ + 0*2⁴ + 0*2⁵ + 1*2⁶ + 0*2⁷.
decimal = 1 + 4 + 64 = 69.
As 69 is the absolute value of our initial (negative) binary, add a minus sign in front of it.
1011 1011 is -69 in two's complement binary notation.
Signed binary to decimal table
If you want to find any whole number in the two's complement eight-bit representation, you may find this table handy. You can see both the value and its two's complement in the same row.

If you are interested in working with the values of a different number of bits, just use our two's complement calculator to save yourself time and effort!

decimal
binary
decimal
binary
0
0000 0000
1
0000 0001
-1
1111 1111
2
0000 0010
-2
1111 1110
3
0000 0011
-3
1111 1101
4
0000 0100
-4
1111 1100
5
0000 0101
-5
1111 1011
6
0000 0110
-6
1111 1010
7
0000 0111
-7
1111 1001
8
0000 1000
-8
1111 1000
9
0000 1001
-9
1111 0111
10
0000 1010
-10
1111 0110
11
0000 1011
-11
1111 0101
12
0000 1100
-12
1111 0100
13
0000 1101
-13
1111 0011
14
0000 1110
-14
1111 0010
15
0000 1111
-15
1111 0001
16
0001 0000
-16
1111 0000
17
0001 0001
-17
1110 1111
18
0001 0010
-18
1110 1110
19
0001 0011
-19
1110 1101
20
0001 0100
-20
1110 1100
21
0001 0101
-21
1110 1011
22
0001 0110
-22
1110 1010
23
0001 0111
-23
1110 1001
24
0001 1000
-24
1110 1000
25
0001 1001
-25
1110 0111
26
0001 1010
-26
1110 0110
27
0001 1011
-27
1110 0101
28
0001 1100
-28
1110 0100
29
0001 1101
-29
1110 0011
30
0001 1110
-30
1110 0010
31
0001 1111
-31
1110 0001
32
0010 0000
-32
1110 0000
33
0010 0001
-33
1101 1111
34
0010 0010
-34
1101 1110
35
0010 0011
-35
1101 1101
36
0010 0100
-36
1101 1100
37
0010 0101
-37
1101 1011
38
0010 0110
-38
1101 1010
39
0010 0111
-39
1101 1001
40
0010 1000
-40
1101 1000
41
0010 1001
-41
1101 0111
42
0010 1010
-42
1101 0110
43
0010 1011
-43
1101 0101
44
0010 1100
-44
1101 0100
45
0010 1101
-45
1101 0011
46
0010 1110
-46
1101 0010
47
0010 1111
-47
1101 0001
48
0011 0000
-48
1101 0000
49
0011 0001
-49
1100 1111
50
0011 0010
-50
1100 1110
51
0011 0011
-51
1100 1101
52
0011 0100
-52
1100 1100
53
0011 0101
-53
1100 1011
54
0011 0110
-54
1100 1010
55
0011 0111
-55
1100 1001
56
0011 1000
-56
1100 1000
57
0011 1001
-57
1100 0111
58
0011 1010
-58
1100 0110
59
0011 1011
-59
1100 0101
60
0011 1100
-60
1100 0100
61
0011 1101
-61
1100 0011
62
0011 1110
-62
1100 0010
63
0011 1111
-63
1100 0001
64
0100 0000
-64
1100 0000
65
0100 0001
-65
1011 1111
66
0100 0010
-66
1011 1110
67
0100 0011
-67
1011 1101
68
0100 0100
-68
1011 1100
69
0100 0101
-69
1011 1011
70
0100 0110
-70
1011 1010
71
0100 0111
-71
1011 1001
72
0100 1000
-72
1011 1000
73
0100 1001
-73
1011 0111
74
0100 1010
-74
1011 0110
75
0100 1011
-75
1011 0101
76
0100 1100
-76
1011 0100
77
0100 1101
-77
1011 0011
78
0100 1110
-78
1011 0010
79
0100 1111
-79
1011 0001
80
0101 0000
-80
1011 0000
81
0101 0001
-81
1010 1111
82
0101 0010
-82
1010 1110
83
0101 0011
-83
1010 1101
84
0101 0100
-84
1010 1100
85
0101 0101
-85
1010 1011
86
0101 0110
-86
1010 1010
87
0101 0111
-87
1010 1001
88
0101 1000
-88
1010 1000
89
0101 1001
-89
1010 0111
90
0101 1010
-90
1010 0110
91
0101 1011
-91
1010 0101
92
0101 1100
-92
1010 0100
93
0101 1101
-93
1010 0011
94
0101 1110
-94
1010 0010
95
0101 1111
-95
1010 0001
96
0110 0000
-96
1010 0000
97
0110 0001
-97
1001 1111
98
0110 0010
-98
1001 1110
99
0110 0011
-99
1001 1101
100
0110 0100
-100
1001 1100
101
0110 0101
-101
1001 1011
102
0110 0110
-102
1001 1010
103
0110 0111
-103
1001 1001
104
0110 1000
-104
1001 1000
105
0110 1001
-105
1001 0111
106
0110 1010
-106
1001 0110
107
0110 1011
-107
1001 0101
108
0110 1100
-108
1001 0100
109
0110 1101
-109
1001 0011
110
0110 1110
-110
1001 0010
111
0110 1111
-111
1001 0001
112
0111 0000
-112
1001 0000
113
0111 0001
-113
1000 1111
114
0111 0010
-114
1000 1110
115
0111 0011
-115
1000 1101
116
0111 0100
-116
1000 1100
117
0111 0101
-117
1000 1011
118
0111 0110
-118
1000 1010
119
0111 0111
-119
1000 1001
120
0111 1000
-120
1000 1000
121
0111 1001
-121
1000 0111
122
0111 1010
-122
1000 0110
123
0111 1011
-123
1000 0101
124
0111 1100
-124
1000 0100
125
0111 1101
-125
1000 0011
126
0111 1110
-126
1000 0010
127
0111 1111
-127
1000 0001
-128
1000 0000
Wojciech Sas, PhD candidate
扩展阅读

 

posted on 2016-08-05 18:39  zno2  阅读(161)  评论(0编辑  收藏  举报

导航