Comparison between C ++ and C #: The story of numbers

Source: Internet
Author: User

Hexadecimal story
 

When dealing with numbers, we were most exposed to decimal. We basically used decimal in mathematics classes from elementary school to university. however, there are various other hexadecimal formats. for example, the time is in 60 hexadecimal notation. In ancient China, the hexadecimal notation represents the weight, and the hexadecimal notation is a pound.

The commonly used computers and binary systems are inseparable. We can say that if there is no binary invention, there will be no computer today's popularity. Thanks to the cool people like laveniz, in fact, the real cool people in the computer field basically engage in mathematics. after all, the theoretical foundation of computers is mathematics. using binary, such a simple stuff, can present complex computer functions. It makes people think that the world is amazing. simple is beauty, which is the motto of many scientists. many great theories are just a simple mathematical formula, such as Newton's Three Laws of mechanics and Einstein's energy equations. in fact, the combination of human genes is also very simple, that is, a pair of base pairs, but the final combination results are complicated and scary.

 

Binary is not necessarily the best
The binary theory is very perfect. In fact, it is still not very good to implement it. It means that a lot of transistors need to be combined. however, due to current technical restrictions, the transistor can only represent two numbers 0 and 1 by switching two stable states. if the transistor can represent eight States, it would be nice. In this case, eight transistors were used to represent one byte. Now, only one is needed, and the cost is reduced by eight times. in this case, binary is useless. You can simply use octal. if a transistor can represent 10 States, it would be more perfect. directly apply the decimal we used to, without the use of annoying binary. unfortunately, the ideal is beautiful, and the reality is cruel. you cannot do that for the time being. however, many people are currently studying quantum computers. One quantum can represent many States. I don't know how much it is. It must be greater than 2. in addition to expressing multiple states, there are also some other features that can bring more powerful functions to computers. but now it's only a research phase. I don't know which month it will take to realize and push it to the market.

 

Why do we use octal and hexadecimal
It is easier to understand binary in a computer. Why are there still octal and hexadecimal systems? In fact, it is just for convenience. We look pleasing to the eye and facilitate communication. A series of 01 people are dizzy. no matter how much hexadecimal code you do, the computer finally recognizes binary data. some people say that it is not in decimal format. Why is the whole strange time. I think it is entirely possible to use decimal. however, I think octal has some advantages. for example, it is easier to convert. we know what division should be used to convert decimal lines into binary lines. If there are too many digits, it will be more troublesome. in octal mode, you only need to replace each bit with the corresponding three-digit binary, and then the sequence remains unchanged. in hexadecimal notation, each digit is replaced with the corresponding four-digit binary. in turn, the binary conversion to octal is to add 0 to each of the three groups from the right to the left, and then convert each group to octal values if there are less than three groups on the left. the same is true for binary to hexadecimal, but there are only four groups. in addition, the hexadecimal number is shorter than the decimal number. therefore, we usually use a hexadecimal system with more points, and the octal sequence is used less now.

In C ++, the nominal value of octal is 0 (number 0), and The hexadecimal value is 0x (number zero and letter x ). for example, int eight = 011; // decimal 9 int sixteen = ox10; // decimal 16

In C #, there is no octal nominal value indicating int eight = 011; // The decimal value is 11 rather than 9; The hexadecimal value is the same as that in C ++.

In Microsoft's official documents, this is the only literal representation in decimal and hexadecimal notation.

Integer literals are used to write values of types int, uint, long, and ulong. Integer literals havetwo possible forms: decimal and hexadecimal.

 

Binary representation of positive and negative numbers
We know how to convert a decimal number into a binary number, but how is it expressed in a computer? Here we will talk about the difference between memory representation during the program running and actual hard disk storage. what files are stored on the hard disk are basically all processed as characters. Each number corresponds to a character number. Now unicode is popular, and each character is represented by the corresponding number in unicode. finally, all these numbers are converted into 01 strings. if the content in it represents other special meanings, it means that the application reads the data and processes the data separately.

For example, int and long are used to represent data types in memory. let's give a simple example. example of a byte type: char in C ++, byte in C.

Assume that the unsigned char ch is 128; // because it is an unsigned number, you do not need to care about the plus and minus signs. All eight 01 strings represent numbers. so it is 10000000 in the memory. that is, 7 digits after 1.

Complement indicates a negative number

But what if there are symbols. the first digit in the 8-bit column is used to represent the symbol. 0 indicates a positive number, and 1 indicates a negative number. the remaining 7 digits indicate specific numbers. in this case, the char type can only represent numbers in the range of-128 to 127. according to this conversion rule, what is the number 10000000 in front of it? 1 indicates a negative number, and 0 indicates a negative number. Therefore, the value is-0? In fact, this is not necessarily the case. It depends on how the CPU instruction set is designed. Some computers may actually handle it like this. but most of them are not. most computers do not directly represent a negative number, but convert the negative number into its complement code. the advantage is that you don't have to worry about negative numbers and subtraction in the future. You only need to consider addition. The subtraction of two numbers is to add a negative number.

The complement conversion is like this. First, the absolute values of negative numbers are reversed, and then 1 is added.

For example,-128. its absolute value is 128,128, Which is 10000000 (there is no need to consider symbols here ). the inverse is 01111111, and then 1 is 10000000 (how can I change it back?-128, the same as 128. this is just a coincidence that 127 and-127 are different. therefore,-128 indicates that the memory size is 128, which is the same as that of 10000000. however, it is certainly not the same for applications to read it. the application also has other information about this number. If you know it is an unsigned number, it is treated as 128. If it is a signed number, it is-128.

So when two identical values are read in the memory, you can see what type you think of. Different types are converted to different results. Sometimes the results are the same, sometimes different.

You can verify that unsigned char num = 128; char ch = (char) num; // ch is-128. instead of the expected 128. therefore, we generally say that a certain type can only represent numbers in a certain range. If the value is exceeded, the system will get an error.

 

The expression of integers is relatively simple, and the representation of floating points is very complicated.

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.