C/C ++ integer, define, represent, range, and then look at Integer

Source: Internet
Author: User

Today I will review the variables and basic types of C ++, and then look at the integer.

Generally, the data type that represents an integer is called an integer, but in fact, an integer in C ++ includes an arithmetic type that represents an integer, a character, and a Boolean value. It is easy to understand this. First, an integer is usually an integer, such as-655536,-200,-, 0, 50, and 65535. Char data can also be used in arithmetic operations, for example, if the character 'a' is equal to Integer 97, you can useIf ('A' = 97) cout <"yes" <Endl;Verify,ProgramThe output value is yes, indicating that the character 'a' is equal to the Integer 97. The Boolean bool type can also be used in arithmetic operations. For example,Char A = 'a'; bool B = true; printf ("% d \ n", A + B );The output result is 98, indicating a = 97, B = 1, and the output is (97 + 1) = 98. Therefore, strictly speaking, an integer is a combination of integers, characters, and boolean values. For C ++, integer types include integer, complex, and Boolean.

There are three integer types: Short, Int, and long. Int is the default integer, and Int Is a machine's word length, that is, the number of digits processed by your CPU, assume that your CPU is 32-bit, then an int is 4 bytes. The short type is half the word length of a machine, and the long type is half the word length of an int. The long type is usually the word length of one or two machines, but in a 32-bit machine, generally, int and long are the same, so sizeof (INT) = sizeof (long) = 4 is correct.

There are two types of characters: Char and wchar_t. Char is known as single-byte data type and 8-bit data. Wchar_t is defined by C ++ and is used to save extended character sets, such as Chinese characters. In fact, wchar_t uses the unsigned short type to define data.

Boolean Type: the bool keyword is used to declare that the values are true and false, that is, true and false. The Int type value of true is 1, and the int type value of false is 0. The arithmetic type of the 0 value represents false, and any non-0 value represents true. The boolean type is usually formally defined and initialized, such as bool B = true. Of course, bool b1 = 1, bool b2 = 'B', and so on. However, suppose there is a programBool B = 'B'; cout <B <Endl; printf ("% C \ n", B );The output of the program is 1 and @ face symbol @. Why is it not 98, true, or 'B'? This not only indicates that any non-0 value is true, it is also proved that the value of true in the bool type is 1. Therefore, the output of any Boolean value with the value of true is always 1, and the output of 1 as a character is @ face character @. This example also proves the IF ("XXXX ") is true.

 

However, in general, integer is mainly used to describe integers. This statement is more generic.Programming LanguageIt is also a friendly saying. For example, in Java, the conversion between arithmetic types must be strictly in accordance with the mandatory transformation.

INTEGER (except bool type) can be divided into signed and unsigned types. The signed type can represent positive and negative numbers and 0, while the unsigned number can only represent 0 and positive numbers. By default, it is a signed number, for example, int I; equivalent to signed int I. To define the unsigned number, it must be displayed with unsigned in front, for example, unsigned int I. However, for char, there are three types of char: normal char, signed Char, and unsigned char. Even so, there are only two types of char representation, namely signed Char and unsigned char.

The difference between the number of BITs and the number of unsigned bits is that the maximum bit in the data is used as the symbol bit, and the rest is the data bit, while the unsigned number uses all bits as the data bit. Assume that 8 bits are used as an example, all 8 bits are used to represent the data, and the data representation range is from 0 to 255. The highest bit (7th bits, starting from 0) is the symbol bit. If this bit is 1, it indicates a negative number. If it is 0, it indicates a positive number, and the remaining 7 bits are used to represent data, the data range is-128-127.

An integer has a range. If the value of an integer value exceeds the range indicated by the type, what will happen?

Let's take a look at the following programs. (Condition: the lab machine is 32-bit)

Question 1:

Short I = (short) 65535;
Printf ("% d \ n", I );

The program outputs:-1.

Question 2:

Short I = (short) 65538;
Printf ("% d \ n", I );

The program outputs: 2.

Question 3:

 Short I = (short)-32769;
Printf ("% d \ n", I );

The program outputs 32767

Why? Why is not the expected value output?

Of course, this is because the value is beyond the data representation range. So why do we get such a value?

For the first question, we will first convert the 65535 hexadecimal notation to 0 xFFFF and convert it to a binary value of 11111111 11111111, Which is exactly 16 bits, and the short type is half the machine's font length, therefore, it is 16 bits. At this time, I is defined as a signed number, the highest bit (15th bits are 1, negative), and 0xffff is the complement of-1 in the machine. Therefore, the output is-1. If the program is changed:

Unsigned short I = (short) 65535;
Printf ("% d \ n", I );

The output is exactly 65535;

For the second question, variable I is also defined as a signed variable. First, 65538 is converted to binary, and 1 00000000 00000010 is a 17-bit binary. Because the short type is only 16 bits, only the 16 digits after the screenshot are taken 00000000 00000010. As shown in the preceding figure, the number of digits with symbols is + 2. So what if I is defined as an unsigned number or output? Will it be + 2? In the same sense, in the 17-bit binary system, only part 00000000 is taken, while the unsigned number only counts the highest bit into the data bit. Of course, the total number of 00000010 bits is 2.

For the third question, we first use the binary complement code-32769 to indicate that 1 01111111 11111111 is 17 bits. Therefore, the number of the last 16 bits is 01111111, and the number of the last 16 bits is + 11111111. That's why we get such results.

I accidentally reviewed the integer type of C ++. Based on some of my own experience, let's look at the integer type of C/C ++. please correct me!

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.