Type conversion between basic types (numeric type), type conversion numeric type

Source: Internet
Author: User

Type conversion between basic types (numeric type), type conversion numeric type

As soon as I opened my blog today, I saw the 5-year-old campus in the upper left corner, and my eyes were a bit blank. The scenes of my previous class as a student have gradually become blurred. Yes. I have been graduating for more than three years, so I will not be here again...

 

I. source code and Supplementary Code

Before entering the text type conversion, make a small foreshadowing to learn about the source code and complement code.

[Note: because the same number is represented by the original code or complement code of different digits and the result is different, if there is no special description, all the original codes and supplementary codes displayed in this section are 8 digits]

 

Original code(True form) is a binary fixed-point representation of numbers in a computer. The original code notation adds a symbol bit before the value (that is, the highest bit is the symbol bit ):

Positive Value: 0; negative value: 1 (0 indicates + 0 and-0); other digits indicate the value size.

1. Advantages of the original code:Simple and intuitive. For example, we use 8-bit binary to represent a number. The original code of + 11 is 00001011, and the original code of-11 is 10001011.

2. disadvantages of the original code:The original Code cannot be directly involved in the operation, and an error may occur. For example, in mathematics, 1 + (-1) = 0, while in binary, 00000001 + 10000001 =

10000010, which is converted to-2 in decimal format. Apparently, an error occurred.

 

Complement(Two's complement) in computer systems, values are all expressed and stored using a complement code. The complement code can be directly involved in the operation. Both the original and supplemental representation methods are available.

The sign bit and the value bit are both 0 to indicate "positive" and 1 to indicate "negative", while the value bit to indicate the method is not phase.

1. Obtain the source code and complete the code

Positive complement: the positive integer is the same as the original code.

[Example] The Supplementary Code of + 9 is 00001001.

Calculate the complement of a negative number: Calculate the complement of a negative integer. the symbol bit remains unchanged on the basis of the original code. The value bit is reversed, and the entire number is added to 1.

[Example] calculate the-5 complement.
-5 original code (10000101) → symbol bit unchanged (10000101) → value bit reverse (11111010) → Add 1 (11111011)
Therefore, the complement code of-5 is 11111011.
[Example] a complement of 0 indicates that it is unique.
[+ 0] fill = [+ 0] original = 00000000
[-0] add = 11111111 + 1 = 00000000


2. Obtain the source code with the known complement code

If you know the number of complement codes, the operation to obtain the source code is to re-obtain the complement code:
1) if the sign bit of the complement code is "0", it indicates a positive number, and its original code is the complement code.
2) If the sign bit of the complement code is "1", it indicates a negative number, then the complement code for the given complement code is the required original code.
[Example] If the source code is 11111001 (-7 ).
Because the symbol bit is "1", it indicates a negative number, so the bit remains unchanged and is still "1 ".
For the other seven digits, the reverse value is 1111001;
Add 1, so it is 10000111.

3. complement operation

Fill: http://baike.baidu.com/view/377340.htm

 

Ii. type conversion between Integers

Class Program {static void Main (string [] args) {Console. writeLine ("Note: all examples in this demo are non-precision loss type conversions between integer types, to facilitate reading and printing is byte rather than bit"); Console. writeLine ("1. complement conversion"); Console. writeLine ("\ r \ n ### 32-Bit Signed to 64-bit unsigned ###"); Console. writeLine ("\ r \ n Example 1"); int I = int. maxValue; ulong ul = (ulong) I; Print (BitConverter. getBytes (I), I. toString (), "before conversion"); Print (BitConverter. getBytes (ul), ul. toString (), "converted"); Console. writeLine ("\ r \ n Example 2"); I = int. minValue; ul = (ulong) I; Print (BitConverter. getBytes (I), I. toString (), "before conversion"); Print (BitConverter. getBytes (ul), ul. toString (), "converted"); Console. writeLine ("\ r \ n ### 32-Bit Signed to 64-Bit Signed ###"); Console. writeLine ("\ r \ n Example 3"); I = int. minValue; long l = (long) I; Print (BitConverter. getBytes (I), I. toString (), "before conversion"); Print (BitConverter. getBytes (l), l. toString (), "converted"); Console. writeLine ("\ r \ n ### 32-bit unsigned to 64-Bit Signed ###"); Console. writeLine ("\ r \ n Example 4"); uint ui = uint. maxValue; l = (long) ui; Print (BitConverter. getBytes (ui), ui. toString (), "before conversion"); Print (BitConverter. getBytes (l), l. toString (), "converted"); Console. writeLine ("2. bitwise conversion"); Console. writeLine ("\ r \ n ### 64-Bit Signed to 32-Bit Signed ###"); Console. writeLine ("\ r \ n Example 5"); l = long. maxValue; I = (int) l; Print (BitConverter. getBytes (l), l. toString (), "before conversion"); Print (BitConverter. getBytes (I), I. toString (), "converted"); Console. writeLine ("\ r \ n ### 64-bit unsigned to 32-Bit Signed ###"); Console. writeLine ("\ r \ n Example 6"); ul = ulong. maxValue; I = (int) ul; Print (BitConverter. getBytes (ul), ul. toString (), "before conversion"); Print (BitConverter. getBytes (I), I. toString (), "converted"); Console. writeLine ("3. Convert signed-bit conversion"); Console. writeLine ("\ r \ n ### 32-bit unsigned to 32-Bit Signed ###"); Console. writeLine ("\ r \ n Example 7"); ui = uint. maxValue; I = (int) ui; Print (BitConverter. getBytes (ui), ui. toString (), "before conversion"); Print (BitConverter. getBytes (I), I. toString (), "converted"); Console. writeLine ("\ r \ n ### 32-Bit Signed to 32-bit unsigned ###"); Console. writeLine ("\ r \ n Example 8"); I = int. minValue; ui = (uint) I; Print (BitConverter. getBytes (I), I. toString (), "before conversion"); Print (BitConverter. getBytes (ui), ui. toString (), "converted"); Console. read ();} private static void Print (byte [] buffer, string result, string tag = "") {Console. writeLine (tag); Console. writeLine ("value:" + result); Console. write ("byte array (complement):"); foreach (byte B in buffer) {Console. write (string. format ("{0},", B. toString ();} Console. writeLine ("");}}

  

Based on the eight examples above, the following results can be obtained:

 1. bitwise conversion, that is, the conversion from a few-bit data type to a multi-digit Data Type(Example 1, example 2, Example 3, and Example 4)

  The completed bits are related to the Data Type of the operand and the target data type.

If the operand is a signed data type, all the completed bits are the signed bits of the operand. If the operand is an unsigned data type, all the completed bits are 0.

 2. Intercept conversion, that is, converting the Data Type of multiple digits to the Data Type of few digits(Example 5 and Example 6)

  The conversion method is to simply intercept valid digits (that is, discard the high position), and has nothing to do with the type of the operand.

 3. Convert the Data Types of the same number of digits with or without symbols(Example 7 and Example 8)

When this type conversion occurs, the meaning of the highest bit changes, and the result may change.

 

Iii. type conversion between integer and floating point

With the above foundation, it is much easier to understand the conversion between integer and floating point types.

When the integer type is converted to the floating point type, if the value of the integer data is too large or too small, some of the lowest valid bits may be lost, resulting in loss of precision (float precision is only 7 bits, double15 ~ 16 digits ).

 

Iv. References:

Original: http://baike.baidu.com/view/60480.htm

Fill: http://baike.baidu.com/view/377340.htm

 

I wish you a happy Mid-Autumn Festival ).

 


Oracle character type and numeric type conversion

Well, what I understand above is that you must avoid implicit conversion every time you filter. If you use a function on the index field or other conversions will cause the index to be unavailable, and the conversion from character type to value type is preferred, if the field you are filtering is of the character type, oracle will never convert you to the numeric type. Your sentence should be relative to other types, I think, such as the date type. So I think both of them should be correct, but it is about two different rules.

I have worked on a project and joined two tables with tens of millions of tables. The results showed that the speed was too slow. The indexes added for the join were read using the execution plan, if the index is not used, the problem lies in implicit conversion. Therefore, the first sentence is correct, and the second sentence is actually not very specific. On the surface, it cannot be seen. I need to understand the internal conversion mechanism of Oracle... I will discuss it with you in a small way...

Conversion Between the char data type and the numeric value type; what is the data type? What is the numerical type?

Data types include int, char, short, bool, long, float, double, and other simple types. numeric types include int, double, float, and so on. Char, and numeric type conversion is 'A' to numeric type is 97 a is 65

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.