Complement operations in Java; Shift Operators

Source: Internet
Author: User

Java 15:06:06 read 256 comments 0 font size: large and small subscriptions
The complement operation in Java (INT in Java is 4 bytes 00000000 00000000 00000000) 32 bit:
Here we will first know the source code and the complement code.
The original code is the value changed from the absolute value, for example, 0101,-0101
The modification of the complement code on the original code: positive numbers are no different (positive numbers, back codes, and complement codes are the same)
There is a rule for negative numbers: the inverse of the absolute value of positive numbers + 1
For example, the-5 complement: 1111 1010 + 1 = 1111 1011

Request :~ (-1)
First, find the complement of-1:
...... 1
-Reverse: 11111 ...... 0 + 1 = 11111 ...... 11111
Reverse (~), Reverse by bit:
0000000... 000000000 (0)

Find ~ 5:
5:
0000 0101
~ 5 (reverse ):
1111 1010. From this data, we can see that it is a negative number. There is a rule for calculating the negative number (the absolute value of the original code is reversed + 1 ),
Then this number should be first-1 and then reverse:
Minus 1010-1 = 1111 1001
Return:
0000, 0110 = 6
Since the highest bit is 1, that is, a negative number:-6
So ~ 5:-6

Example 2:
Int A = 0x1234; // 0001 0010 0011
Int B = (~ A) & 0xff;
Int c = B ^ 12;
~ A:
0001 0010 0011 0100
Reverse:
1110 1101 1100 1011 (the highest bit is 1, that is, a negative number. The negative number rule [returns the inverse of the absolute value of the original code + 1]. Then the Inverse Calculation is performed here. First,-1 is used to obtain the inverse absolute value)
-1:
1110 1101 1100 1010
Reverse:
0001 0010 0011 0101
Value: 4661
Since the highest bit is 1, that is, a negative number, it should be-4661
-4661:
1110 1101 1100 1011
& Ff (1111 1111 ):
1110 1101 1100 1011
0000 0000 1111 1111
=
0000 0000 1100 1011 = 203
^ 12 (1100)
0000 0000 1100 1011
0000 0000 0000 1100
=
0000 0000 1100 0111 = 199

0 complement: 0000 0000
1 complement: 0000 0001
-1 Completion code: 1000... 1

Java uses the complement code to represent the binary number. In the complement representation, the highest bit is the sign bit, the positive number is 0, and the negative number is 1.
The following code is provided:
For a positive number, the highest bit is 0, and the rest represent the value itself (in binary format). For example, the complement code of + 42 is 00101010.
For a negative number, the complement code of the absolute value of the number is reversed by bit, and then 1 is added to the entire number, that is, the complement code of the number is obtained. For example, if the complement code of-42 is 11010110 (the bitwise result of 00101010 is reversed to 11010101 + 1 = 11010110)
The completion code of 0 is unique, and all are 00000000.
Analyze your
5. The complement code is 00000101.
As mentioned earlier, the highest bit represents the symbol. Now the highest bit is 1, which represents a negative number. How can this problem be solved? Returns the inverse of the absolute value (that is, first returns the absolute value ),
11111010-1 = 11111001, then reverse back, 00000110, so this absolute value is 6, so because it is a negative number, so it is-6, understand?

See: http://www.blogjava.net/rosen/archive/2005/08/12/9955.html
Please note! Reference, post this article should indicate the original author: Rosen Jiang and Source: http://www.blogjava.net/rosen
Shift Operator
Including:
"> Right shift"; "<left shift"; "> unsigned right shift"
Example:
-5> 3 =-1
1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1111
The result is exactly the same as math. Floor (double)-5/(2*2*2.
-5 <3 =-40
1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1101
The result is exactly the same as-5*2*2*2.
5> 3 = 0
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
The result is exactly the same as 5/(2*2*2.
5 <3 = 40
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0010
The result is exactly the same as 5*2*2*2.
-5> 3 = 536870911
1111 1111 1111 1111 1111 1111 1111
0001 1111 1111 1111 1111 1111 1111
Whether positive or negative, their right shift, left shift, and unsigned right shift are all themselves, for example,-5 <32 =-5,-5> 32 =-5,-5> 32 =-5.
An interesting phenomenon is that 1 is shifted to 31 places and 31 places to the right. The result is-1.
0000 0000 0000 0000 0000 0000 0000
1000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111

Bit logical operators
Including:
&; | Or ;~ Non (also called reverse); ^ XOR or
"&", "| Or ","~ A non-logical operation is a basic logical operation, which can evolve into a compound logical operation of "Non", "or", "and" not. "^ Exclusive or" is a special logical operation. If you reverse it, you can get "same or". Therefore, "same or" logic is also called "exclusive or non" logic.
Example:
5 & 3 = 1
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
-5 & 3 = 1
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
5 | 3 = 7
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
-5 | 3 =-5
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111
~ 5 =-6
0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111
~ -5 = 4
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000
5 ^ 3 = 6
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
-5 ^ 3 =-8
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.