Look at C language code conversion-the binary representation of negative numbers

Source: Internet
Author: User

Today, when I was reading C language code conversion, since I had forgotten the negative Binary Expression, I checked the online information and they said it was a p! Mistaken! I have discussed it with you and posted it as I have forgotten it here:

Suppose there is a number of the int type with the value of 5, then we know that it is represented in the computer:

00000000 00000000 00000000 00000101

5 is converted to a binary system of 101, but the number of int types occupies 4 bytes (32 bits), so a bunch of 0 values are filled in front.

Now I want to know how-5 is represented in a computer?

 

In a computer, a negative number is expressed as a complement of the original code.

What is a supplemental code? This should start with the original code and the reverse code.

 

Original code: a positive number, which is converted to a binary number based on the absolute value. A negative number is converted to a binary number based on the absolute value, and the highest bit is supplemented by 1, which is called the original code.

For example, 00000000 00000000 00000000 00000101 is the source code of 5.

10000000 00000000 00000000 00000101 is the original code of-5.

 

Anticode: the anticode of a positive number is the same as that of the original code. The anticode of a negative number is the inverse of the original code of this number except the symbol bit.

The reverse operation indicates that the original value is 1, and the value is 0. The original value is 0, and the value is 1. (1 to 0; 0 to 1)

For example, if the positive number is 00000000, 00000000, 00000000, 00000101, 00000000, or 00000000

Negative number 10000000 00000000 00000000 each digit is reversed (except the symbol bit), 00000101 11111111 11111111 11111111.

Said: 11111111 11111111 11111111 11111010 10000000 is an anti-code of 00000000 00000000 00000101.

The anti-code is mutual, so it can also be called:

10000000, 00000000, 00000000, 00000101, 11111111, 11111111, 11111111, 11111010, and are mutually inverse codes.

 

Complement: the positive value of the complement code is the same as the original code. The negative value of the complement code is used to reverse the original code of the number except the symbol bit, and then add 1 in the last bit.

For example, the 10000000 00000000 00000000 00000101 anti-code is: 11111111 11111111 11111111 11111010.

Then, the complement code is:

11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111

Therefore,-5 is expressed in the computer as 11111111 11111111 11111111 11111011. Convert to hexadecimal: 0 xfffffffb.

Let's look at how integer-1 is represented in a computer.

Suppose this is also an int type, then:

1. First retrieve the original code of-1: 10000000 00000000 00000000

2. Get the reverse code: 11111111 11111111 11111111 11111110 (except for the bitwise inversion of the symbol)

3. Supplemental code: 11111111 11111111 11111111 11111111

It can be seen that the Binary Expression of-1 in a computer is full 1. Hexadecimal: 0 xffffff

 

Key knowledge points:

The reverse and complement codes of positive numbers are the same as the original codes. The negative number is the inverse of the original code of the number except the symbol bit. The complement code of the negative number is the inverse of the original code of the number except the symbol bit. Then, add 1 to the last digit.

Below is the original article in the book:

Original code Notation: The symbol bit and value are used to represent the number of symbols. The positive sign bit is expressed as "0", the negative sign bit is expressed as "1", and the numerical value is expressed in binary format. The anticode notation specifies that the anticode of a positive number is the same as the original code, and the anticode of a negative number is the inverse of the original code of this number except the symbol bit. The complement expression specifies that the positive value is the same as the original code. The negative value is used to reverse the original code of the number except the symbol bit, and then add 1 to the last digit. the complement codes of positive and negative zeros are the same. [+ 0] fill = [-0] fill = 0000 0000b

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.