Source code, reverse code, and supplementary code

Source: Internet
Author: User

Source code, reverse code, and supplementary code

The original code, reverse code, and supplementary code are three concepts about integers in the computer.

In the computer, integers are stored in this way. In all binary bits, one (highest bit) is used as the symbol bit. 0 indicates the positive number, 1 indicates the negative number, and the other is the numerical bit. It must be noted that the original codes, backcodes, and supplementary codes of positive numbers are the same. If sizeof (INT) is 4,

The binary value of 1 is 0000 0000 0000 0000 0000 0000 0000 0001 (original code)

-The binary value of 1 is 1000 0000 0000 0000 0000 0000 0000 0001 (original code)

Reverse code

The anticode is specified to be the same as the original code symbol bit, and the value bit is reversed.

1111 1111 1111 1111 1111 1111 1111 1110 (-1 anti-code)

Complement

The Supplementary Code is set to add a value relative to the anticode

1111 1111 1111 1111 1111 1111 1111 1111 (-1 complement)

It should be pointed out that integers exist in the memory in the form of complement code, so the operation is involved in the form of complement code.

The complement method simplifies the computation. Its significance is that the symbol bit and the value bit are treated in the same way during the computation. That is to say, all binary bits are involved in the operation.

For example

-1 + 2 = 1

-1 Completion code

1111 1111 1111 1111 1111 1111 1111

2's Complement

0000 0000 0000 0000 0000 0000 0000

Complement Addition

0000 0000 0000 0000 0000 0000 0000 0001 this is the complement of 1.

If there is no complement concept, you must first obtain the respective symbols when-1 + 2. If it is a different sign, the final symbol must be determined based on the absolute values of the two numbers.


Write a function below to print the original code, reverse code, and complement of the integer.

# Include <stdio. h> # include <stdlib. h> // print the original code void print1 (INT num) {unsigned int I; I = 1 <(sizeof (INT) * 8-1); If (Num <0) {num = num-1; num = ~ Num; num | = I; // set the symbol bit} while (I) {static int K = 1; printf ("% C", num & I? '1': '0'); If (K % 4 = 0) printf (""); I >>= 1; k ++ ;} printf ("\ n");} // print the back code void print2 (INT num) {unsigned int I; I = 1 <(sizeof (INT) * 8-1 ); if (Num <0) {num = num-1; num | = I; // set the symbol bit} while (I) {static int K = 1; printf ("% C", num & I? '1': '0'); If (K % 4 = 0) printf (""); I >>= 1; k ++ ;} printf ("\ n");} // print the completion code void print3 (INT num) {unsigned int I; I = 1 <(sizeof (INT) * 8-1 ); while (I) {static int K = 1; printf ("% C", num & I? '1': '0'); If (K % 4 = 0) printf (""); I >>= 1; k ++ ;} printf ("\ n");} void main () {int num =-1; printf ("num = % d \ n", num ); printf ("original code \ n"); print1 (Num); printf ("Back Code \ n"); print2 (Num); printf ("complement Code \ n "); print3 (Num); System ("pause ");}
Run





The three print () functions are mostly repeated and are written as one function:

/* Print the integer's original code, reverse code, and supplemental code. flag indicates that 0 is the original code, 1 is the reverse code, and other values are the Supplementary Code */void print (INT num, int flag) {unsigned int I; I = 1 <(sizeof (INT) * 8-1); If (Num <0) {Switch (FLAG) {Case 0: // process the original code num = num-1; num = ~ Num; num | = I; // set the symbol bit break; Case 1: // process the reverse code num = num-1; num | = I; // set the symbol bit break; default: Break ;}while (I) {static int K = 1; printf ("% C", num & I? '1': '0'); If (K % 4 = 0) printf (""); I >>= 1; k ++ ;} printf ("\ n ");}





Source code, reverse code, and supplementary code

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.