Negative number,

Source: Internet
Author: User
Tags binary to decimal decimal to binary

Negative number,

This article focuses on the following knowledge points:

1. Some basic concepts related to negative numbers;

2. Convert the binary and decimal numbers of negative numbers;

3. Negative Numbers and data types;

 

I. Basic Concepts

Number of machines: the number of machines in the binary representation of a value in a computer. The number of machines is signed and the highest digit of a number is used for calculation. The positive digit is 0, the negative number is 1;

True Value: because the number of machines with negative numbers and the highest bit is the symbol bit, the form value of the number of machines is not equal to the real value. It is not as clear as a positive number ". For example, 1000 0011, if it is a negative number, bit7 is 1, and its real decimal value is-125, rather than the form value 131 (of course, if it is not a negative number, it is indeed 131); so, for the sake of distinction, the real value corresponding to the number of machines with signs is called the true value of the number of machines;

Complement: a negative number is expressed in the form of a complement of its absolute value in a computer. The complement code of the number of machines can be calculated by the original code (the number of binary machines in the value. If the value is a positive number, the original code is the same as the complement code. If the value is a negative number, the complement code is the original code corresponding to the value (except the symbol bit) and is obtained by adding one at the end.

 

Ii. Binary and decimal conversion of negative numbers

 

Decimal to binary of a negative number: Math. abs (number)-1, and then take the inverse. for short, "Subtract one to get the inverse ".

Negative binary to decimal: first get the inverse, then Math. abs (number) + 1, referred to as "Get the inverse plus one"

The bitwise is also called the anticode. The binary value of a negative number cannot be seen in its decimal format (that is, the true value). Therefore, the bitwise addition can be used to obtain the complement code, in the process of calculating the complement code, the symbol bit is not involved in the calculation.

Example:

1. convert values from-110 decimal to binary

1) minus 1: Math. abs (110)-1 = 109

2) decimal: the binary value of 109 is 0110 1101, And the decimal value is 1001 0010.

2. Convert numeric value-110 binary to decimal

1) reverse: 1001 0010; reverse: 0110 1101

2) Add 1: After adding 1, it is 0110 1110, which is equal to 110. Because the highest bit of the original code is 1, it is a negative number, that is,-110.

 

Iii. Negative Numbers and Data Types

 

1. Signed and unsigned Data Types

In programming languages, there are generally signed and unsigned data types. The value is of the unsigned type. For example, if the byte type is signed, the value range is-128 ~ 127, but the byte type, because the maximum bit does not need to store symbols, but can directly store the value, it is 0 ~ 255, of course, the negative number cannot be stored.

 

2. Conversion between signed and unsigned Data Types

Take the C # language as an example. byte is unsigned and sbyte is signed.

Byte b1 =-100;

Sbyte b2= 250;

Both lines of code cannot be compiled, and the reason is simple. the value assigned to the variable is beyond the storage range. In actual projects, we often encounter the need to use byte to store the sbyte data sent by the other party. In c #, the conversion between different symbol data types is processed as follows:

Byte b1 =-100 & 0xff; // 156

Sbyte b2 = (sbyte) Convert. ToInt32 (250); //-6

The code solves the conversion, but why can the conversion be correct and how the result come from? Let's analyze:-100 is converted to the unsigned method, and its binary form is 1001 1100 first, to convert it to an unsigned method, the highest bit is not a sign bit, but calculated as a specific value bit, which is 156; 250 is converted to a signed method, first, we obtain the binary form 1111 1010, and regard its highest bit as the symbol bit. Because it is a negative number, convert it to decimal by "take the inverse plus one", then it is-6.

 

Unsigned to signed data type: obtains the binary original code of a value. The highest bit is regarded as a signed bit and then converted to decimal. The obtained value is the converted value.

Signed to unsigned data type: obtains the binary original code of a value, regards the highest bit as the bit storing the value, and converts it to decimal. The obtained value is the converted value.

 

3. Numeric loss caused by forced type conversion

When there is a conversion between the signed and unsigned,If the symbols are the same and the number of bits of the target data type is greater than or equal to the current data typeFor example, sbyte, int32, byte, or uint32, there will be no "loss" (this is also called for the time being). Here we will discuss the other two situations:

1,Converts a data type to a bit smaller than its own data type, for example, int32 to sbyte or byte.

Obviously, if the value is a value greater than sbyte or byte, it will be lost after conversion. Here we mainly analyze how to lose the binary data? How does the converted value come from?

For example

Int32 a1 = 3000;

Sbyte a2 = (sbyte) a1; // output-72

Byte a3 = (byte) a3; // 184

Analysis: 3000 of binary data is 0000 0000 0000 0000 0000 1011 1011 1000, while sbyte can only store 0 ~ 255, so bit0 ~ Bit7, that is, 1011 1000, and sbyte is of the signed data type. Therefore, bit7 is the symbol bit. Press "Get inverse 1" to get 72, and then add the symbol to get-72.

A3, because it is a byte unsigned type, 1011 1000 can be obtained directly in decimal format. In this case, the original value cannot be obtained even if the data type is converted back because the data bit is intercepted.

2,Converts a data type to a bit that is greater than or equal to its own data type and has different symbols. For example, sbyte is converted to byte, and sbyte (Signed) is converted to uint32 (unsigned ), or byte (unsigned) to int32 (Signed ).

For example:

Sbyte a1 =-10;

Byte a2 =-10 & 0xff; // 246

Int32 a3 = a1; //-10

Uint32 a4 = (unit32) a1; // 4294967286

Analysis: the data type of the variable a3 is the same as that of A1. the number of digits of int32 is greater than that of sbyte, so the data will not be lost, or-10. A2, because it is necessary to convert-10 to unsigned, it directly ignores its symbol bit and returns 246. A4 here is a little more detailed. First, the binary value of-10 is 1111 0110. When it is converted to 32bit, the negative value of-10 is:

1111 1111 1111 1111 1111 1111 1111

To convert to the uint32 unsigned type, ignore the symbol bit and convert to decimal to 4294967286.

After conversion, although the value is different from the original value before conversion, the original value can still be obtained after conversion to the original type. Therefore, in this conversion case, an unexpected value is obtained, but the value is not lost. If the conversion is returned, the original value can still be retrieved.

 

Summary:

The text mainly describes some basic concepts about negative numbers, as well as the conversion between the binary and decimal numbers of negative numbers. The conversion between different symbols or different data types is also analyzed. Some possible "loss value" or "unexpected value" situations may occur. These are all very basic knowledge, just to record it. If you have any mistakes, please point them out. Thank you!

Related Article

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.