In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "detailed introduction of Java source code, complement and inverse code". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed introduction of Java source code, complement and anti-code".
1. Original code, inverse code and complement code
As you all know, data exists in binary form in computers, such as byte a = 6; byte b =-6
It is divided into two cases, one is positive, the other is negative; for positive 6, the original code is 0000 0110, and the inverse and complement are also this; for-6, the original code is 1000 0110, which is why? Because the * * bit (leftmost) is a symbol, 0 is positive and 1 is negative.
Whether it is byte,short,int or other numbers, the * * bit is used to represent symbols, so the original code of-6 is 1000 0110, the negative inverse code is the same symbol, and the other inverse is 1111 1001; the negative complement is to add 1 to the inverse code, because it is binary, it is 1 every 2, so the complement is: 1111 1010
Note: the addition and subtraction operation in the computer (only addition in the computer, which can be expressed by addition and subtraction, that is, in the form of complements, see chestnut below) refers to the operation between complements! And negative numbers exist in the form of complement in the computer and participate in the operation. If you want to change to decimal, you need to change to the original code first and then to decimal or other decimal numbers.
So how do you say zero? We can simply look at: 0 = 6-6 = 6 + (- 6) = [0000 0110] complement + [1111 1010] complement = [0000 0000] complement = [0000 0000] Yuan, then someone wants to ask, how much does the complement 1000 0000 mean? And look at a simple-1-127 = (- 1)
+ (- 0001) = [1000 0000] original + [1111 1111] original = [1111 1111] complement + [1000 0001] complement = [1000 0000] complement = [00000000] original, if both use the original code, the same original code [00000000] could have represented two numbers 0 and-128. but with a complement, you can have a complement corresponding to a separate number, obviously, a complement corresponds to a number more in line with our needs!
In addition, for negative numbers, the source code and the inverse code are transformed into each other, try to understand the memory:
Source code-> inverse code: symbol bits remain unchanged, other bits are inverted; for example, [1000 0001] original = [1111 1110] reverse
Source code-> complement: symbol bits remain unchanged, other bits are inverted, and then + 1; for example, [1000 0001] original = [1111 1111] complement
Inverse code-> original code: symbol bits remain unchanged, other bits are inverted; for example, [1111 1110] inverse = [1000 0001] original
Inverse code-> complement: + 1; for example, [1111 1110] inverse = [1111 1111] complement
Complement-> original code: symbol bits remain unchanged, other bits are inverted, and then + 1; for example, [1111 1111] complement = [1000 0001] original
Complement-> inverse code: first change the original code, and then change the inverse code; for example, [1111 1111] complement = [1111 1110] inverse
These are all basic things, as long as we remember that the operation in the computer is in the form of complement, and here it will involve a process of drawing a simple diagram, and the operation in the computer is carried out with complement; while the intermediate transformation process can be transformed very quickly by the computer, we don't have to worry about this; let's take a look at what operations that operation includes.
two。 Basic operation
When it comes to operations in reality, they are nothing more than four operations, addition, subtraction, multiplication and division, and corresponding to the computer, there is also addition, subtraction, multiplication and division. As mentioned above, addition and subtraction can be achieved by complement addition, but what about multiplication and division? Here we will talk about the simplest multiplication and division, ah, multiply by 2 and divide by 2, if it is multiplication and division by other numbers is still more complex, I am not interested in research, interested partners can check the realization of multiplication and division in the computer, you will be addicted to it!
Take the byte type as an example, the byte type * * bits are symbol bits, so the range is between 1111 and 01111 1111, which becomes decimal, that is, from-127to 127. plus the source code of 0000 0000 corresponds to two numbers 0 and-128. so the whole range is from-128to 12712.
Multiplication and division is also divided into two parts, positive and negative.
2.1 positive number multiplication 2 (move to the left > > 1); System.out.println (b); / /-3}
Why is the printed result-3? there is a small detail operation here. When you move to the right, you will first change the number of the byte type into the int type, change the original code of the int type, then change the complement, after the shift operation, change the last 8 bits into the byte type, and then change the original code to decimal.. Is it a thief's trouble? Or take the above-6 as the chestnut, and-6 should be moved to the right unsigned, so the original code of-6 should be 32 bits.
10000000 00000000 00000000 00000110 / / the original code 111111111111111111111010ax / complement 0111111111111111111111111111111101Universe right shift one bit 11111101max / the last eight digits is the complement 10000011//byte type source code of byte type, the corresponding decimal code is-3
3. A brief summary.
Because we are using a byte type as an example, this is also to facilitate the example, otherwise use an int type, any number to write out the source code is a long string, look dazzled. In fact, the byte type of shift operation is clear, other types are the same, read so much, I do not know if you have summed up a little rule, I will talk about my understanding!
First of all, we need to know what type the current number is, and whether it will go beyond the range of this type after the shift operation. If it is out of range, we cannot directly come to the simple conclusion of multiplying by 2 or dividing by 2. We will come up with an unexpected number.
Then, if the shift operation does not exceed the range of the current type, then boldly say that moving one bit to the left is multiplied by 2, and moving one bit to the right is rounded down by dividing by 2!
Then, for a positive number, moving one bit to the left is the removal of the * bit and adding 0 to the bit; moving one bit to the right adds the same number as the symbol bit, and the bit is removed; for the negative number, it is the same, so I won't say much.
*, that is, unsigned right shift. Here, we should pay attention to change the binary source code of int type, change the complement code, and then perform the shift operation. The last 8 bits are intercepted as the byte type complement code we need, and then the original code is changed to decimal code.
4. "OR", "and", "non", "XOR"
Please note the "or", "and", "not" and "java" |, & &,! Don't get confused, these in java are used to make logical judgments, while our "OR" and "XOR" here are used to calculate binaries, which have nothing to do with each other, although the words are somewhat similar, "or" with a vertical bar to express |, and with a & to represent, not with ~, "XOR" with ^, let's briefly talk about their role:
Or: in binary, two operands perform or operate, and as long as one is 1, the result is 1, otherwise it is 0; for example,-6 | 3, first turn each into a complement, that is, (1111 1011) | (0000 0011). According to the figure below, the complement calculated by * * is 1111 1011, the original code is 1000 0101, corresponding to decimal-5, so-6 | the result of 3 is-5! It's very simple, you should know what the Operand is by now!
Public void num () {byte a =-6; System.out.println (~ a); / / 5}
And: if both operands are 1, the result is 1, otherwise it is 0
XOR: just look at the name. If the two operands are different, the result is 1, otherwise it is 0.
No: even if you reverse yourself (the symbol bits should also be reversed), the usage is as follows, because the complement of-6 is 1111 1010, the complement after inversion is 0000 0101, corresponding to 5 of the decimal system.
Public void num () {byte a =-6; System.out.println (~ a); / / 5} Thank you for your reading. The above is the content of "detailed introduction of Java source code, complement and inverse code". After the study of this article, I believe you have a deeper understanding of the detailed introduction of Java source code, complement and inverse code, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.