integer literal and binary number of C + +

Source: Internet
Author: User
Tags integer

The literal amount of an integer

You can represent an integer literal in a very straightforward way. Here are some examples:

–123 +123 123 22333

Among them, the "+" and "–" numbers in the first two examples are the unary operators mentioned earlier. In the second example, you can omit "+" because this is the default, but in order to make the value clearer, adding "+" will not be a problem. Literal +123 is the same as 123. The 4th example is typically written as 22,333, but commas cannot be used in integer literals. If a comma is used, the compiler uses the value as a comma-separated two value.

You cannot use an old-fashioned integer value. To give an extreme example, the system does not accept integers with 100 digits. An integer literal has a upper and lower bound, which is determined by the amount of memory stored for each type of integer value. This topic is discussed later in this chapter when you describe an integer variable.

Of course, you can write an integer literal as a decimal value that will be stored as a binary value on your computer. It is important to understand binary in programming, to prevent readers from misunderstanding the way binary numbers work, here's a brief introduction.

Binary number

First consider what you will do when you represent a common decimal number, such as 324 or 911. Obviously, 324 means 324, 911 means 911. More specifically, these two numbers indicate:

324 is: 3x102+2x101+4x100, 3x10x10+2x10+4

911 is: 9x102+1x101+1x100, 9x10x10+1x10+1

This is called the decimal notation, because this is based on the power of 10 (derived from the Latin word decimalis, which means the meaning of the tax, that is, 10% of the tax).

It is convenient to represent values in this way, because a person has 10 fingers or 10 toes or 10 any type of appendage. However, this is not convenient for PCs, because the PC is mainly based on the switch, that is, open and close, add up to only 2, rather than 10. This is the main reason why the computer uses cardinality two to represent values instead of Cardinal 10. This is called a binary counting system. The number can only be 0 or 1, which is ideal when you are using only open/Guan Lai to represent numbers. According to the counting system of cardinality 10, binary number 1101 can be decomposed into:

1x23+1x22+0x21+1x20, which is 1x2x2x2+1x2x2+0x2+1.

Computed 13 (decimal system). In table 2-1, the corresponding decimal values represented by 8 binary digits are listed (the binary numbers are often called bits).

Table 2-1

Note using the top 7 digits can represent the number from 0 to 127, a total of 27 digits, and all 8 bits can represent 256 (or 28). In general, if you have n bits, you can represent 2n integers with values ranging from 0 to 2n-1.

In a computer, the addition of binary numbers is very easy, since the corresponding digits can only be 0 or 1, so the process is very simple. The example in Figure 2-1 illustrates the process of adding two 8-bit binary numbers.

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.