1, the minimum unit of the computer system bit binary
In a binary computer system: 'bit (bits): the smallest unit of data storage. Abbreviated
b
, also referred to as bit (bit
), each binary number is a 1 or 0 bit (bit
), wherein each8bit = 1 byte
(bytes);
Recalling further data type in Java, such as int数据类型 = 4个byte(字节)
while 1 byte(字节) = 8 bit(位)
; we will often say int = 32位
(it plainly, is a bit as a data storage unit in the binary system). as follows
2, symbols and unsigned
Signed and unsigned numbers simply means that the positive and negative respectively, are 'bit (bit) in a binary system as a data storage unit, the most significant bit (first bit) is the sign bit, the sign bit positive number It is "0", a negative sign bit is "1."
example:
Suppose int number = 1
, then the number represented in a computer system is as follows:
00000000 00000000 00000000 00000001
Similarly available, number = -1
when expressed in binary as follows:
10000000 00000000 00000000 00000001
Note: The most significant bit (first bit) is the sign bit, a value of 1 because the number is a positive number, the most significant bit is 0;
3, the original binary code, anti-code complement
The original code of the original code is the number of machines, is to add a sign bit of a binary number (because the values have positive and negative points), the number of positive sign bit is 0, a sign bit is negative.
Inverted when the results of the original code correct multiplication and division with the sign bit, and in addition and subtraction when there is a problem, for example: decimal representation: 1 + (-1) = 0
but with binary representation:
00000001 + 10000001 = 10000010,
The result is converted to decimal number -2. So in the original code based on the inverted invention, to solve this problem.
Complement Despite the anti-code to solve the problem of addition and subtraction of positive and negative numbers, but let this figure 0 with two kinds of "form": "0" and "-0", but this is illogical, but there should be a 0, so there are a complement.
For signed numbers in terms of :
1, a positive number of the original code, the inverted, complementary codes are the same; 2, negative = inverted its original code symbol bits unchanged, the other bit is inverted (negated meaning: 0 replaced by 1, into 1 0); 3, negative complement its inverse = +1; 4,0 inverse code, complement is 0; [1] special attention in the computer computation time, are in a manner to complement operations. 2, binary to decimal, you must use the original binary code conversion.
example:
Here we use the "signed number" to simulate the look, how in the computer operation.
(1) adding a positive number:
For example: 1 + 1, in the computer are performed as follows:
1 is the original code:
00000000 00000000 00000000 00000001
Because the "positive original code, the inverted, complementary codes are the same", so that the 1's complement of the original code = 1, the complement of the complement 1 + 1 is equal to:
00000000 00000000 00000000 00000001 + 00000000 00000000 00000000 00000001
=
00000000 00000000 00000000 00000010
00000000 00000000 0,000,000,000,000,010 (converted to decimal) = 2
(2) positive phase, dropping:
For example: 1 - 2, in the computer are performed as follows:
In fact, the computer is a subtraction operation applied to the operation, so 1--2 = 1 + (-2)
The first step: to find out one's complement (a positive number because the original code, anti-code and complement the same, so we can get directly from the complement of the original code):
1 complement:
00000000 00000000 00000000 00000001
Step two: the 2 in the original code to find out:
-2 original code:
10000000 00000000 00000000 00000010
The third step: the anti-2 code to find out:
-2-minus:
11111111 11111111 11111111 11111101
The third step: the complement -2 find out:
-2 complement:
11111111 11111111 11111111 11111110
Step Four: 1's complement of the sum and -2:
00000000 00000000 00000000 00000001 + 11111111 11111111 11111111 11111110
=
11111111 11111111 11111111 11111111
Step 5: The results complement conversion of the original code, the opposite can (if you want to convert binary to decimal, binary must be the original code)
Complement: 1,111,111,111,111,111 1,111,111,111,111,111
=
Anti-code: 1,111,111,111,111,111 1,111,111,111,111,110
=
Original code: 1,000,000,000,000,000 0,000,000,000,000,001
Step Six: the calculation result of the original binary code to decimal
Original binary code: 1,000,000,000,000,000 0,000,000,000,000,001 * 2 ^ 0 = 1 = -1
4, thinking: java Why byte ranges from -128 to 127
java byte of one byte, i.e. 8bit (bits), where the highest bit is a sign bit, a numerical value for the remaining seven. When the sign bit is 0, it indicates a positive number in the range 00000000 to 01111111 (S code form), i.e. decimal 0-127. when the sign bit is 1, it indicates negative, in the range of 10000000 ~ 11111111 (supplementary code), -128 to -1, to convert the original code is 11111111 10000001, i.e. -1. In complement, in order to avoid the presence of "-0", a predetermined 10000000 -128, the byte explains why the range is -128 to 127.
5, the Java << and >> and >>>
>>> >> and << First and java are the bitwise operators, is operated for a binary. In addition to these there are &, |, ^, ~, a few bits operator. No matter what the initial value in accordance with the band, are converted into a binary bit operations. Here mainly on Java, << and >> and >>>.
<< represents a left shift, regardless of the number of positive and negative, low complement 0
Note: The following default data type byte is 8 bits, when the left whether positive or negative, low fill 0
Positive number: R = 20 << 2
Twos complement 20: 00010100
After the two moved to the left: 01,010,000
Results: r = 80
Negative: R & lt << 2 = -20
-20 binary original code: 10010100
-20 binary one: 11101011
-20 twos complement: 11101100
Left after two's complement: 10,110,000
Anti-code: 10101111
Original code: 11010000
Results: r = -80
'>>' denotes a right shift, if the number is positive, the high bit of 0, if it is negative, the high bit of 1;
Note: The following default data type byte is 8 bits
Positive number: R = 20 >> 2
Twos complement 20: 00010100
After the two moved to the right: 00,000,101
Results: r = 5
Negative: R & lt >> 2 = -20
-20 binary original code: 10010100
-20 binary one: 11101011
-20 twos complement: 11101100
Right after the two's complement: 11,111,011
Anti-code: 11111010
Original code: 10000101
Results: r = -5
'>>>' unsigned right shift, also called a logical right shift, i.e., if the number is positive, the high bit of 0, and if the number is negative, the same high right up 0
Note: The following default data type bit int 32
Positive number: R = 20 >>> 2
Results with the same r = 20 >> 2;
Negative: R & lt >>> 2 = -20
-20 original code: 1,000,000,000,000,000 0,000,000,000,010,100
Anti-code: 1,111,111,111,111,111 1,111,111,111,101,011
Complement: 1,111,111,111,111,111 1,111,111,111,101,100
Right: 0,011,111,111,111,111 1,111,111,111,111,011
Results: r = 1073741819
Finally, if there is insufficient or is not correct, please correct me criticism, grateful!
I welcome you to focus on the public number, there are some java learning materials and a large wave of java e-books, such as Zhou Zhiming teacher depth java virtual machine, java programming ideas, the core technology volume, Westward design patterns, java concurrent programming combat ... .. is a java Bible, do not say fast car on Tomcat, ye who go! The main thing is to explore technology, yearning technology, the pursuit of technology, said good pots Friends is coming Oh ...
Reference: www.cnblogs.com/summerdata/... www.cnblogs.com/chuijingjin...