Storage and operation of integers in calculation, and storage of Integers

Source: Internet
Author: User

Storage and operation of integers in calculation, and storage of Integers

The first digit of an integer indicates the symbol bit.

1 positive integer

A positive integer is stored in binary format in a computer. For example:

short s = 3;
3 = 2 ^ 1 + 2 ^ 0
A short integer is generally two bytes, that is, 16 bits. Its storage method is as follows:

0 000 0011

Therefore, the short integer is represented in the range of-2 ^ 15 ~ + 2 ^ 15

0 is a special case. It has two Representation Methods:

1 000 0000

0 000 0000

Therefore, the number of short integers is only 2 ^ 16-1.


Let's look at the integer type again. The storage of the integer type is 4 bytes, 32 bits

int i = 7;
7 = 2 ^ 2 + 2 ^ 1 + 2 ^ 0

That is:

0 000 0000 0000 0111


To make the statement simple, the short type is used in the following scenarios.


2 negative integer

Two concepts are introduced before negative numbers:

Original code: The symbol bit and binary;

Reverse code: positive number is itself, negative sign bit plus binary inverse (that is, 0 is changed to 0 );

Complement: the positive number is itself, and the negative number is not reversed and incremented;

For example:

Original code:

10 = 2 ^ 3 + 2 ^ 1:0 000 1100

-10:1 000 1100

Anti-code:

10: 0 000 1100

-10:1 111 0011

Complement:

10: 0 000 1100

-10:1 111 0100


In a computer, data is stored and expressed in a unified manner using a supplementary code.


Example:-5

Original code: 1 000 1001

Reverse code: 1 111 0110

Complement: 1 111 0111

Its storage method is: 1 111 0111

Example: 0

0 has two Representation Methods: + 0 and-0;

Original code:

1 000 0000

0 000 0000

Anti-code:

1 000 0000

0 111 1111

Complement:

1 000 0000

1 000 0000

Therefore, the expression of 0 in calculation is also unique. + 0 and-0 are both 0000 and.


3 Integer Operation

In fact, the computer only performs one operation: Addition

Subtraction: addition, plus a negative number

Multiplication: addition, repeated addition

Division: subtraction, repeated Subtraction


So let's look at the addition operation. The data in the computer is represented and stored in the form of a complement,

During computation, the computation is also performed using the complement code, and the result is also the complement code. In this mode, the symbol bit is not involved in the operation, and the symbol bit is also included in the operation.

First look at the positive number, for example: 1 + 2

1:0 000 0001

2: 0 000 0010

--------------------

0 000 0011

Result: 0 000 0011, decimal 3

Let's look at the positive and negative numbers, which are also subtraction operations, for example: 1-2 = 1 + (-2)

1: 0 000 0001

-2:1 111 1110

---------------------

1 111 1111

Result: 1 111 1111

Negative number is used to obtain the original code by completing the code: minus one to obtain the inverse

For example, the preceding result:

Complement: 1 111 1111

Reverse code: 1 111 1110

Original code: 1 000 0001 in decimal format-1


Now, let's take a look at the floating point number and go to work.


# Remove all high-level 0 values



Storage and operation of large integers in C Language

Big integer calculation is a complex problem. If you need big integer computing in scientific computing, you can use the GNU Multiple Precision Library (GMP ). This is a big integer/real number library copyrighted by LGPL. For details, refer
Gmplib.org

Compile mathematical operations in C Language

This is my lab report, which is the same as yours. If you only need the code, let's look at the last one. I think it should be comprehensive. Let's take a look at the specific requirements.
I. Requirement Analysis
1. function: sparse expression like a line. If the expression is incorrect, the output "the expression is wrong"; otherwise, the value of the expression is calculated and output. Operators include addition, subtraction, multiplication, division, multiplication, and subtraction. Parentheses are Parentheses, but they can be nested. The operand can be a floating point number or a variable consisting of multiple letters.
2. Enter an expression and press enter to finish. The range of input values cannot exceed the range of floating point numbers. Contains variables. The variable name consists of letters and is case-insensitive.
3. If the calculation result is an integer, an integer is output. If the calculation result contains decimals, a floating point number is output.
Ii. Outline Design
1. The general idea is to read a row of expressions and store them in an array of characters. Then read each character in sequence for judgment. While reading and calculating. Two stacks, one character stack and one digital Stack are used in the program to store operators and numbers, and computing is performed based on the priority of operators. The final output result.

2. The program includes several modules, the main function and several basic functions.
Several functions are described as follows:
Bool stackempty (save1 s) is used to determine whether the operand stack s is empty.
Void push (save1 & s, char e) If the stack is full, the output "Stack is full"; otherwise, Element e is imported into the stack.
Void pop (save1 & s, char & e) if the stack is empty, the output "stack is empty"; otherwise, the top element of the stack is assigned to e
Bool stackempty2 (save2 s) is used to determine whether the operator stack s is empty.
Void push2 (save2 & s, char e) If the operator stack is full, the output "Stack is full"; otherwise, Element e is imported into the stack.
Void pop2 (save2 & s, char & e) if the stack is empty, the output "stack is empty"; otherwise, the top element of the stack is assigned to e
Int in (char e) returns the priority of operator e in the stack.
Int out (char e) returns the priority of operator e outside the stack.
Void count (char a, char ope, char B) calculates a and B and adds the result to the stack.
3. Procedure:
1. Read a line of expression and store it with a character array line [].
2. Read and process each character in sequence. The expression is incorrect:
1. In case of a number, continue to judge the next character until the next character is not a number and not a decimal point. If the number contains more than two characters, the input is incorrect. Otherwise, you can ensure that the operand is a complete floating point number and then import the number to the operand stack.

If the number is not the last digit of the expression, and the number is not followed by "+,-, *,/, ^,)", the expression is incorrect.

2. In the case of operators, there are two situations:
1. If the operator is a negative sign (this operator is a symbol in two cases: one is a negative sign at the beginning, and the other is a sign before "("), the first 0 is added to the operand stack, then, add the negative sign to the operator stack.
2. Compare the operator with the stack top element of the operator stack if it is not a negative sign:
(1) If the top element of the stack has a low priority, the newly entered operator is added to the stack.
(2) If the top element of the stack has a high priority,
1) An operator pops up from the symbol stack,
2) one or two operands are displayed from the object stack,
3) The operation result is pushed into the object stack.
(3) If the priority is equal, the top element of the stack goes out of the stack and is eliminated from the input element.

If "(, +,-, *,/, ^" is placed at the end of the expression, the expression is incorrect.
If "+,-, *,/, ^" is not followed by a number or variable, the expression is incorrect.

3. In case of a letter variable, continue to judge the next character, until the next character is not a letter variable, you can ensure that the variable is complete, and then output "Enter the value of the variable ", then, the input variable value is added to the operand stack.
If the variable is not followed by "+,-, *,/, ^,)", the expression is incorrect.
4. If the read character is not one of the above conditions, the expression is incorrect.

3. When all the characters are included

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.