First of all, you must understand the test points of this question, first, you must first understand the underlying details of computer principles, the bitwise operation principle of addition and subtraction, and the situation that arithmetic operations in computers will cross the border, second, we must have certain object-oriented design ideas.
First, the computer uses a fixed number of several bytes to store the value, so the value that can be expressed in the computer has a certain range, in order to facilitate the explanation and understanding, take an integer of the byte type as an example. It is stored in 1 byte, and the maximum value range is-128 to + 127. -1 indicates that the binary data corresponding to the memory is 11111111. If two-1 values are added together, the type upgrade during the Java operation is not considered. After the operation, a carry is generated and the binary result is 1,11111110, because the carry part is discarded because it exceeds the byte storage space, the final result is 11111110, that is,-2, this uses the overflow method to calculate the negative number. -128 the binary data corresponding to the memory is 10000000. If two-128 values are added together, the type upgrade during the Java operation is not considered. After the operation, the binary result is 1, because the carry part is discarded because it exceeds the byte storage space, the final result is 00000000, that is, 0. This result is obviously not expected, this indicates that arithmetic operations in the computer may cross the border. The calculation results of two values cannot exceed the value range of this type in the computer. Because the types involved in expression operations in Java are automatically upgraded, we cannot use the byte type to demonstrate this problem and phenomenon. You can use the following example program to experiment with integers:
Int A = integer. max_value;
Int B = integer. max_value;
Int sum = A + B;
System. Out. println ("A =" + A + ", B =" + B + ", sum =" + sum );
The long type is not considered first. Because the positive number range of the int value is the power of 31, the maximum value is approximately 2*1000*1000*1000, that is, the size of 2 billion, to implement a 10 billion calculator, we have to design a class that can be used to represent a large integer and provide the function of addition, subtraction, multiplication, and division with another integer. The approximate function is as follows:
() This class contains two member variables, one representing the symbol, and the other representing the binary number of the value using the byte array.
() There is a constructor that converts a string containing multiple digits into an internal symbol and byte array.
() Provides addition, subtraction, multiplication, division
Public class biginteger {
Int sign;
Byte [] Val;
Public biginteger (string Val ){
Sign =;
Val =;
}
Public biginteger add (biginteger other ){
}
Public biginteger subtract (biginteger other ){
}
Public biginteger multiply (biginteger other ){
}
Public biginteger divide (biginteger other ){
}
}
Note: It is very complicated to write the complete code for this class. If you are interested, you can refer to the source code of the Java. Math. biginteger class in JDK. The interviewer also knows that no one can write the complete code of this class in a short time. What he wants is whether you have the concept and Consciousness in this aspect, the most important thing is to examine your abilities. Therefore, you should not give up answering this question because you cannot write the complete final result. What you need to do is to write more than others, it proves that you are better than others, and you can have the ideology in this aspect. After all, others may not even understand the meaning of the question and have not written anything. You must dare to answer this question, even if I only answer a part of the answer, it is different from those who do not understand anything. It is a very tall man. Of course, the opportunity belongs to you. In addition, the Framework Code in the answer is also very important, reflecting some of the knowledge of object-oriented design, especially the method naming is very professional and the English words used are very accurate, this is also an embodiment of capabilities, experience, professionalism, and English proficiency. It will leave a good impression that programming skills are similar to other conditions, in addition to giving you more opportunities, your salary can be higher than RMB one thousand.