C + + signed and unsigned number conversions

Source: Internet
Author: User

This article transferred from: http://www.94cto.com/index/Article/content/id/59973.html

1. Example:

Today we are doing a question about the conversion of signed numbers and unsigned numbers to their left/right shifts, and the principle of transformation between them and the principle of displacement is getting a big head. I really regret that I did not seriously study the principle of computer composition/computer operating system and other basic computer courses. The following is I based on the relevant knowledge review and collation of materials, if there are and so on the similarities of articles, please do not be offended. Also hope to see this article comrades can have something to gain it.

1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 intMain ()6 {7Unsigned Short intUI;8Signed Short intsi;9UI = (unsigned Short int)0x8000u;TenSi = (Signed Short int)0x8000; Oneprintf"UI =%u\n", UI); Aprintf"si =%d\n", si); -UI = Ui>>1; -Si = si>>1; theprintf"UI =%u\n", UI); -printf"si =%d\n", si); -cout<<"------------------------------"<<Endl; -  +UI = (unsigned Short int)0x8000u; -Si = (Signed Short int)0x8000; +printf"%u\n", UI); Aprintf"%d\n", si); atUI = ((Signed Short int) ui>>1);//Shift first, then type conversion -Si = ((unsigned Short int) si>>1); -printf"%u\n", UI); -printf"%d\n", si); -cout<<"------------------------------"<<Endl; -  inUI = (unsigned Short int)0x8000u; -Si = (Signed Short int)0x8000; toprintf"%u\n", UI); +printf"%d\n", si); -UI = ui<<1; theSi = si<<1; *printf"%u\n", UI); $printf"%d\n", si);Panax Notoginsengcout<<"-------------------------------"<<Endl; -  theUI = (unsigned Short int)0x8000u; +Si = (Signed Short int)0x8000; Aprintf"%u\n", UI); theprintf"%d\n", si); +UI = ((Signed Short int) ui<<1); -Si = ((unsigned Short int) si<<1); $printf"%u\n", UI); $printf"%d\n", si); -     return 0; -}

Run results

2. Concept
In a computer, you can differentiate the number of positive and negative types, become a signed number (signed), a number with no positive or negative type (only an integer type), and become an unsigned number (unsigned). To be concise, unsigned means that all of its digits are used to denote the size of a numeric value, with the number of symbols in addition to the highest digits representing the positive and negative values (0 for positive numbers; 1 for negative numbers) and others to indicate the size of the values. For example: Ten mechanism number positive 2,552 binary expression form: 1111 1111

Ten mechanism number negative-12 binary expression form: 1111 1111

Visible-1 of the binary's highest bit is red 1, but why is it expressed in 1111 1111 instead of 1000 0001? This is about any number that is stored in the computer in the form of a complement. The following will be introduced, not in detail in this first.

3. Storage Range

As can be seen from the previous introduction, since the highest bit of the signed number is used as the sign bit, it is able to express a maximum value smaller than the maximum number of unsigned numbers can be expressed. Let me give you an example to illustrate this:

Unsigned number: 1111 11,110 binary Value: 255

Number of symbols: 0111 11,110 binary Value: 127

This is a question that one might ask: Can the number of symbols be expressed in a range less than the range of values that an unsigned number can express?

Oh, the answer is negative! Although the ability of the signed number to express the maximum is weakened, it can express negative values. The number of negative numbers can compensate for their shortcomings. Let's compare:

The expression value range of the unsigned number of a byte is: [0,255]

The value range of the symbolic number of a byte is: [ -128,0], [0,127]

It can be seen that they all represent 256 numbers.

4. Various codes (original code/inverse code/complement)

Some people may think that the "1" (double-byte) expression in the computer is 1000 0000 0000 0001, but it is not. The computer is expressed in its complement form, that is, "1" (double-byte) expression is 1111 1111 1111 1111.

Let's talk about the concept of various codes.

Original code: An integer, converted to a binary number according to the absolute value, the highest bit is the sign bit.

Anti-code: the original code in addition to the highest bit (sign bit), the rest of you bitwise reverse, the resulting binary code. The inverse code of the positive number is the original code.

Complement: Counter code the lowest bit plus 1 is the complement.

The complement of the negative number of the method to explain, first get its anti-code, and then the anti-code plus 1 can. Some of the great gods according to their original code, close eyes to get, this ability needs to cultivate a bit ah.

Then some people may say, why should we introduce the form of complement? Storing directly in the original code is not much easier? Hey, remember, some things are not you want to save the hassle of convenient. Well, let's enjoy the strength of the complement.

The advantages of a computer with the symbol number in the complement representation:

The conversion between the complement of 1 negative numbers and the complement of the corresponding positive numbers can be accomplished by using the same method-the complement operation, which simplifies the hardware. 2 You can turn subtraction into addition, so that subtraction can be calculated with the adder. If the highest bit (sign bit) has a carry, the carry is discarded when the number of 32 complements is summed.

Mental arithmetic to complement (the big God to find the algorithm):

From the lowest bit to the first found 1 is the same, the sign bit is unchanged, between the "negation" (0 change 1;1 0).

Original code: 1010 1001 Complement: 1101 0111.

5. The mutual conversion between signed number and unsigned number

The bit pattern does not change when casting between unsigned integers and signed integers.

When a signed number is converted to an unsigned number, negative numbers are converted to large positive numbers, which is equivalent to adding 2 N to the original value, while positive numbers remain unchanged.

When an unsigned number is converted to a signed number, the original value is maintained for the small number, and the large number is converted to negative numbers, which is equal to the original value minus 2 of the n-th square.

When there are signed and unsigned number types in an expression, all operations are automatically converted to unsigned types. The operation precedence of the visible unsigned number is higher than the signed number.

unsignedint=;  Signedint=-;         

The result is b>a.

6. Convert your meal

The conversion of a signed number

Original type Target type Conversion method
Char

Short

Symbol bit extension
Char Long Symbol bit extension
Char unsigned char The highest sign bit loses its bit meaning and becomes a data bit
Char unsigned short The sign bit expands to short, then goes from short to unsigned short
Char unsigned long The sign bit extends to long, and then from long to unsigned long
Char Float The sign bit extends to long, then goes from long to float
Char Double The sign bit extends to long, and then from long to double
Char Long double The sign bit extends to long, and then from long to long double
Short Char Keep Low byte
Short Long Symbol bit extension
Short unsigned char Keep Low byte
Short unsigned short

Up to loss of meaning, to data bits

Short unsigned long The sign bit extends to long, and then goes from long to unsigned long
Short Float The sign bit extends to long, then goes from long to float
Short Double The sign bit extends to long, and then goes from long to double
Short Long double The sign bit extends to long, and then from long to long double
Long Char Keep Low byte
Long Short Keep Low byte
Long unsigned char Keep Low byte
Long unsigned short Keep Low byte
Long unsigned long Up to loss of meaning, to data bits
Long Float Using a single-precision floating-point number indicates a possible loss of precision
Long Double Using a single-precision floating-point number indicates a possible loss of precision
Long Long double Using a single-precision floating-point number indicates a possible loss of precision

The conversion of unsigned numbers

Original type Target type Conversion method
unsigned char Char Highest as symbol bit
unsigned char Short 0 extensions
unsigned char Long 0 extensions
unsigned char unsigned short 0 extensions
unsigned char unsigned long 0 extensions
unsigned char Float Convert to long, then convert from long to float
unsigned char Double Convert to long, and then convert from long to double
unsigned char Long double Convert to long, and then convert from long to long double
unsigned short Char Keep Low byte
unsigned short Short Highest as symbol bit
unsigned short Long 0 extensions
unsigned short unsigned char Keep Low byte
unsigned short unsigned long 0 extensions
unsigned short Float Convert to long, then convert from long to float
unsigned short Double Convert to long, and then convert from long to double
unsigned long Long double Convert to long, and then convert from long to long double
unsigned long Char Keep Low byte
unsigned long Short Keep Low byte
unsigned long Long Highest bit as sign bit
unsigned long unsigned char Keep Low byte
unsigned long unsigned short Keep Low byte
unsigned long Float Convert to long, then convert from long to float
unsigned long Double Convert directly to Double
unsigned long Long double Convert to long, and then convert from long to long double

7. Bytes of various data types

Under 32-bit platforms:

PS: If the article is inappropriate, please point out!

C + + signed and unsigned number conversions

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.