Design a 10 billion calculator

Source: Internet
Author: User

. complement (negative numbers are stored in the computer)

. Billions of calculators

Negative numbers are stored in the form of a complement in the computer.
The complement representation of negative numbers is expressed as a negative number as a binary source (the highest negative number is 1, the highest positive number is 0) and then the original code is reversed (1 to 0,0 1), that is, the inverse code, plus 1 (the last one on plus 1), that is, the conversion to complement. If you use eight-bit
Binary representation-5, first step, original code 10000101, anti-code 01111010, plus 1 becomes complement: 01111011


First of all to understand the question of the test point is what, first of all, we have to understand the underlying details of the computer principle, to know the bit operation principle of addition and subtraction, and know that the arithmetic operations in the computer will be out of bounds, the second is to have a certain object-oriented design ideas.

First, the computer in a fixed number of bytes to store the numeric value, so the computer can be represented by a certain range of values, in order to facilitate interpretation and understanding, we first use a byte type integer as an example, it is stored with 1 bytes, representing the maximum value range of 128 to +127. 1 the corresponding binary data in memory is 11111111, if two-1 add, do not consider the Java operation when the type promotion, the operation will produce a carry, the binary result is 1, 11111110, because the carry after more than the byte type of storage space, so the carry part is discarded, That is, the final result is 11111110, that is, 2, which is exactly the way of using overflow to achieve the operation of negative numbers. 128 the corresponding binary data in memory is 10000000, if two-128 add, do not consider the Java operation when the type promotion, the operation will produce a carry, the binary result is 1, 00000000, because the carry after more than the byte type of storage space, so the carry part is discarded, That is, the final result is 00000000, that is, 0, the result is obviously not what we expected, this shows that the arithmetic operation in the computer will be out of bounds, the results of two can not exceed the numerical range of the type in the computer. Since the types in Java that are involved in expression arithmetic are automatically promoted, we cannot use the byte type to do experiments that demonstrate this problem and phenomenon, and 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 ");



Regardless of the long type, since int has a positive range of 2 to 31, the maximum value represented is approximately equal to 2*1000*1000*1000, which is the size of 2 billion, so to implement a 10 billion calculator, we have to design a class that can be used to represent a large number of integers, and provides the function of subtraction with another integer, the approximate function is as follows:

(1) There are two member variables within this class, one representing the symbol and the other using a byte array to represent the binary number of the value

(2) There is a construction method that converts a string containing a multi-digit value into an internal symbol and byte array

(3) Provide the function of subtraction

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: To write this class of complete code, is very complex, if interested, you can see the JDK in the Java.math.BigInteger class source. The interviewer also knows no one can write this kind of complete code in a short time, he wants to be you have this aspect of concept and consciousness, his most important is to examine your ability, so, you do not because you cannot write the complete final result to give up this problem, you have to do is you write more than others, Prove that you are stronger than others, you have this aspect of the ideological consciousness can be, after all, others may even the meaning of the topic do not understand, what has not written, you have to dare to answer this question, even if only a part, that also with those who do not understand the difference out of the person, opened the distance, is the dwarf in the tall, In addition, the framework code in the answer is also very important, embodies a number of object-oriented design skills, especially the method named very professional, with the English word is very accurate, this is the ability, experience, professionalism, English level and other aspects of the embodiment, will give people a good impression, In the case of programming ability and other conditions, English is good except for you to get more opportunities, salary can be 1000 yuan higher.

Design a 10 billion calculator

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.