Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Integer Literals and Binary Numbers of C++
Integer Literals and Binary Numbers of C++
integer literals

can be expressed in a very direct way. Here are some examples:

_123 +123 123 22333

Among them, the "+"and "_" in the first two examples are the unary operators mentioned above. In the second example, you can omit "+"because it is the default, but to make the meaning of this value clearer, adding "+"will not be a problem. Literal quantity +123 is the same as 123. The fourth example is generally written as 22,333, but commas cannot be used in integer literals. If a comma is used, the compiler will treat the value as two values separated by commas.

old integer values cannot be used either. To take an extreme example, the system does not accept integers with 1 bits. Integer literals have upper and lower limits, which are determined by the amount of memory that stores each type of integer value. This chapter will discuss this problem when introducing integer variables later.

Of course, integer literals can be written as decimal values, and these values will be stored as binary values in the computer. It is very important to understand binary when programming. In order to prevent readers from misunderstanding how binary numbers work, here is 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, and 911 means 911. More specifically, these two numbers represent:

324 is: 3×12+2×11+4×1, that is, 3×1×1+2×1+4

911 is: 9×12+1×11+1×1.

It's very convenient to represent numerical values in this way, because people have 1 fingers or toes or 1 appendages of any kind. However, this is not convenient for PC, because PC is mainly based on switches, that is, on and off, which add up to only 2, not 1. This is the main reason why computers use radix 2 to represent numerical values instead of radix 1. This is called a binary counting system. Numbers can only be or 1, which is ideal when only on/off is used to represent numbers. According to the method of radix 1 counting system, the binary number 111 can be decomposed into:

1×23+1×22+×21+1×2, that is, 1× 2× 2+1× 2+× 2+1

to calculate 13 (decimal) In Table 2-1, the corresponding decimal values represented by 8 binary digits are listed (binary digits are often called bits).

table 2-1

note that using the first 7 digits can represent numbers from to 127, and using all 8 digits can represent numbers of 256 (i.e. 28). Generally speaking, if there are n bits, 2n integers can be represented, and their values range from to 2n-1.

in a computer, it is very easy to add binary numbers, because the carry of the corresponding numbers can only be or 1, so the processing process will be very simple. The example in Figure 2-1 demonstrates the process of adding two 8-bit binary numbers.