Chapter 2 Basic Built-in Types

    In the first chapter, we have a general understanding of what a computer language is, and we have also seen a small C++ introductory program, so in order to better learn C++, let us start with the most basic definition.

    As we all know, in addition to C++, there are many other programming languages ​​in computer languages, such as C, Java, C#, Go, Object-C, and even scripting languages ​​such as php. These languages ​​seem to be different, but in fact they have the following very similar characteristics:

  •     built-in data types
  •     variable
  •     expressions and statements
  •     Control structures (if else, while loops, etc.)
  •     function
  •     note

    So today we will start with the most basic basic built-in types.

a counting method

    Before learning, first we need to figure out the following questions, that is, what is base? And what are bit and byte?

1. Binary and hexadecimal

(1)  Binary

        The so-called binary, that is, every binary counting method. What does this mean? Maybe some students are not very clear, so just think about the numbers we usually use.

        The usual counting method we are used to is the decimal system, that is, when the ones digit exceeds 9, it becomes 10, that is, the ones digit returns to 0, and the tens digit increases by 1, and if 1 is added , then the ones digit continues to increase until it reaches 9 again, then add 1 to the tens digit again to become 20, and so on, we can expand the digits from tens to hundreds and thousands as the number increases , ten thousand bits and so on.

        The same is true for binary, except that when it reaches 2, it needs to be carried, and the number becomes 10. If expressed in decimal, it is 2, and then continue to increase, and 3 in decimal is expressed as binary 11 in decimal, 4 in decimal becomes 100 in binary, 5 in decimal becomes 101 in binary, and so on.

        In the C++ language, binary is not used directly, but octal (starting with 0), decimal (no beginning) and hexadecimal (starting with 0x or 0X) are used to represent.

(2) Hexadecimal

        The principle of hexadecimal is the same as that of binary, but since its number exceeds 9, how to express it from 10 to 15? It's easy, just use the letters a through f. In order to distinguish the difference between numbers, in C++, hexadecimal needs to add a 0x before the number. Let's use 20 as an example this time, and 0x14 is its hexadecimal value.

2. bit and byte

    After learning binary, it is easy to talk about bit and byte.

(1)bit

        Bit is also a binary bit, the smallest unit of computer storage information. For example, for example, 4 in decimal, which is 100 in binary, occupies a total of 3 bits.

(2)byte

        A byte is called a byte, usually composed of 8 bits, and is a unit of measurement for computer computing storage capacity.

Two basic built-in types

1. Arithmetic type

    C++ collectively refers to integers, floating-point numbers, single characters and Boolean values ​​as arithmetic types. The size of its storage space depends on the machine. The size here refers to the number of binary bits (binary bits called bits) used to represent the type . The C++ standard stipulates the minimum storage space for each arithmetic type, but it does not limit the compiler to use a larger space.

    

Type

Meaning

Minimum size

bool

Boolean

THAT

char

Character (character type)

8 bits

wchar_t

Wide character

16 bits

short

Short integer

16 bits

int

Integer

16 bits

long

Long integer

32 bits

float

Single-precision floating-point (single-precision floating-point type)

6 significant digits (significant digits)

double

double-precision floating-point (double-precision floating-point type)

10 significant digits (significant digits)

long double

Extended-precision floating-point (extended precision floating-point type)

10 significant digits (significant digits)

                                                                        figure 1


    As shown in Figure 1 , it is the minimum storage space corresponding to the built-in arithmetic type, and the figure below is the storage space obtained by using the g++ compiler on 64-bit Mac OSX.

Type

Meaning

Minimum size

bool

Boolean

8 bits

char

Character

8 bits

wchar_t

Wide character

32 bits

short

Short integer

16 bits

int

Integer

32 bits

long

Long integer

64 bits

float

Single-precision floating-point

32 bits

double

double-precision floating-point

64bits 

long double

Extended-precision floating-point

128bits

                                                                           figure 2

    In addition, there is a long long super long integer, and the result obtained through sizeof is 64bits binary digits.

    Notice:

    1. These internal keywords are all lowercase;

    2. Test the data on the 64-bit Mac OSX Apple system, if there is any difference, please use sizeof to check it on other systems;

    It will not be specifically mentioned in the future.

(1) Integer

    All integer types can be divided into two types: signed (signed) and unsigned (unsigned), where the signed type can represent positive numbers or negative numbers (including 0), while the unsigned type can only represent greater than or equal to The number of 0, if unsigned is not written, the default is a signed type, such as unsigned int, which means an unsigned integer.

    So how to distinguish between positive and negative signed values? As shown in Figure 3 below:


signed:

                        image 3

     The first binary bit of a signed number in memory is the sign bit, 0 represents a positive number, and 1 represents a negative number. The number of binary bits may vary with different compilers and machines. The number of bits in this article is currently 64 bits. A more common storage method for computers.

    A. Short integer

        Defined as short, the binary bits required in the memory are 16 bits (note the difference between the compiler and the machine), that is, 2 bytes, the range of numbers that can be expressed by signed short integers is from -32768 to 32767, unsigned The range is then 0 to 65535.

    Let's see how this is calculated. First of all, the signed short integer type is 16-bit binary, and the highest bit is the sign bit, so there are only 15 bits left that can be expressed as numbers, which means that if the highest bit is 0, that is, a positive number, the maximum value The value is that all 15 bits are 1.

    Because it is binary, 15 bits, each bit can only be 0 or 1, according to the calculation principle of permutation and combination, the maximum value should be: 2 to the 15th power, minus 1 ( 2 15 - 1 ) . Similarly, the maximum value of an unsigned number is: 2 to the 16th power, minus 1 ( 2 16 - 1).

    Maybe some students don't quite understand why this calculation is done, so let's briefly talk about it.

    For example, if we don’t ask for 15 digits, let’s take a simple 3 digits as an example. The three bits, 000, 001, 010, 011, 100, 101, 110, and 111, have a total of eight arrangements, and the corresponding decimal numbers are respectively It is 0, 1, 2, 3, 4, 5, 6, 7. From this, we can see that each arrangement is unique, so we only need to subtract this arrangement that is all 0 to get It is the maximum value of 3 binary digits.

    At this time, some students may ask, the three are easy to handle, if there are too many digits, do we have to sort them out one by one? Of course it's impossible. The above method is just to let you understand a simple truth, what is the truth?

    That is, if we want to ask for the maximum value of n-bit binary, we only need to ask for the number of all permutations and combinations of these n-bits, and then subtracting 1 is the result we need. So how to find the number of all permutations and combinations of n bits?

    It’s very simple. Let’s take three bits as an example. First, we only look at one bit. There are two ways of 0 and 1 in total. At this time, add one bit, and this bit can only be 0 and 1. Whether it is 0 or 1, there are only two ways for the first digit, but because of the difference of the second digit, the combination method at this time becomes 2 times 2, a total of 4 ways, the same reason, plus one There are still only two ways of 0 and 1 for this bit, but there are 4 different ways for the first two bits, whether it is 0 or 1, combined with the third bit, then there are 4 times 2 in total, a total of 8 types way out.

    Now, everyone should understand it. To sum it up, to get the total number of permutations and combinations, multiply the number of modes of each bit, and the resulting product is the result we need. Here , because there are only two ways of 0 and 1, that is to say, the number of permutations and combinations of n bits is equal to 2 to the nth power.

    Closer to home, the number of bits stored in a short integer is 16 bits. If it is a signed number, the highest bit is used as a flag, and there may be two types of positive 0 and negative 0, and negative 0 here is artificially specified as the minimum value -32768 , if you want to know the details, please refer to the next chapter about the original code, inverse code and complement code.

    B. Integer

      The principle is the same as above, so I won’t say more. It is defined as int. The binary bits in the memory are 32 bits and 4 bytes . 0 to 4294967295.

    C. Long integer

       Defined as long, the binary bits in the memory are 64 bits, 8 bytes, the range of signed short integers is from -9223372036854775808 to 9223372036854775807, and the unsigned range is 0 to 18446744073709551615 .

    D. Long integer

        Defined as long long, the binary bits in the memory are 64 bits, 8 bytes, and the range is the same as long integer .

(2) Character type

    A. Character type

        Defined as char, the binary bit in the memory is 8 bits, 1 byte, this character type is basically stored in all machines. Also divided into signed and unsigned two.
      B. The wide character type
        is defined as wchar_t, 32 binary bits in memory, 4 bytes.

(3) Boolean type

      Defined as bool, the binary bit in the memory is 8 bits, 1 byte, and the value is true and false. However, other arithmetic types can also be used to represent true and 0 to represent false.

(4) Floating point type

     Floating-point and integer are stored differently in memory. In order to record data information more completely, it uses binary scientific notation to store data. So what is binary scientific notation?

    In order to let everyone understand better, let's first explain it with decimal scientific notation.

    The so-called scientific notation, for example, 735.2, expressed in scientific notation is 7.352 * 10 n , that is, to record a number as a number with a single digit greater than 0 multiplied by 10 to the nth power value, this The value is equal to the original number, and this method of counting is called scientific notation.

    The binary scientific notation, as the name suggests, is to record a number as a binary number with a single digit greater than 0 multiplied by the nth power of 2. This value must also be equal to the original number. Expressed in an expression is : 1.xxx* 2n .

    In order to facilitate everyone's understanding, use 75.25 as an example, convert it into binary to 1001011.01, and express it in binary scientific notation as 1.00101101 * 2 6 .

    How is this calculated?

    首先将75.25分成整数部分和小数部分,两部分分开计算成二进制,先来看看整数部分,要想得到二进制,只需要将75除以2,得到结果为37余1,之后再用37除以2,得到结果18余1,以此类推,直到结果为0,最后将所有的余数按从后往前取,得到的二进制数就是结果,见下图所示:


                图4

    75的二进制就是1001011。

    再来提小数部分,0.25,其提取方式与整数部分正好相反,使用乘法,取结果的整数值,最终结果的取值顺序按从前往后,如下:

    0.25 * 2 = 0.5    取整数部分0

    0.5 * 2 = 1         取整数部分1

    小数部分的最终结果就是01,加上小数点就是0.01。

    好了,现在小数部分和整数部分都已经有了,整合起来就是1001011.01,表达成二进制科学计数法即是:1.00101101 * 26。也许有些同学还想知道为什么会这么计算呢?其实想想短整型里面提到的原理,逆转一下思维,就能够明白其中的道理了。

    说到这里,大家应该都已经明白了二进制的科学计数法,下面就来继续说说浮点型数据在内存中的存储方式吧。

    从下图5我们可以看出,浮点型在内存中主要分为三个部分,其中符号位很好理解,而指数部分,看看上面的1.00101101 * 26,这个6就是指数,它将被存储在指数位,而0.00101101将被存储于尾数部分。


                                图5

  •     符号位
        占1bit,0表示正数,1表示负数。          
  •     指数位   

        用于存储二进制科学计数法的指数值,因为指数也可以有正负,因此指数位的最高位还是不计算在范围内,但是它的方式又与符号位不同,它的值是从0到最大值,但是,实际表达的值却是将它均分成正负。

  •     尾数部分

        用于存储二进制科学计数法当中的实际数据的小数部分,因为方式都是1.xxx,所有那个小数点前面的1就没了记录的必要。另外,浮点型都没有带符号与无符号的区别。

    A. 单精度浮点型

        定义为float,内存中占32位,4个字节,数据范围为-2128~2128,也即是-3.40E38~3.40E38,其存储方式遵循IEEE754 R32.24标准,其中最高位是符号位,0为正,1为负,指数位占8个bit,尾数部分占23个bit,如下图6:


                                图6

        且不提符号位,在这里说说指数位的存储方式,因为指数也分正负,在这里二进制是从00000000到11111111,如果按照以往的最高位作为符号位的存储,那么就会有正0与负0的问题,为了更好地将数据按照顺序记录,也就是不会在中途来解决正0和负0的问题,这里就采用了一种移位存储的方式。

        提到移位存储,首先我们需要一个基数,这个基数的确定方法就是将最高位设定为0,其余位设定为1,在这里,8位指数的基数就是01111111(127),然后将所需要存储的数据加上这个基数,那么就可以按照顺序唯一表示从-128到127的范围,而不需要再单独处理正0和负0的问题了。

    详细看下图7:



                                图7

        以上便是指数部分的存储了,至于尾数部分,就更简单了,直接将上面提到的尾数放入即可,不足补零,下面来看一个完整的例子吧。

        还是以75.25(1.00101101 * 26)为例,存储的数据中,符号位为0,指数位6,存储为6 + 127 = 133,即是10000101,尾数部分00101101,不足23位,其后补零,如图8:


                                                                    图8

        另外,还需要注意一点,尾数部分也决定了精度和有效数字,首先说说精度,比如75.2,其小数部分计算出来就是001100110011......,这数字与实际的十进制所说的数字有部分出入,这就是所说的精度问题。

        而关于有效数字,这尾数部分最大值可为223 -1 = 8388607,一共7位数字,这就是前面提到的有效数字。

    B. 双精度浮点型

        定义为double,内存中占64位,8个字节,数据范围为-21024~21024,也即是-1.79E308~1.79E308,遵循IEEE754 R64.53标准,最高位是符号位,0为正,1为负,指数位占11个bit,尾数部分占52个bit,如下图9:

                                    图9

        其设计原理与单精度浮点型相同,不同的是存储的长度不同,这导致了数据范围的不同,还有基数也变为了0111111111(1023),有效数字增大到252 -1 = 4503599627370495,一共16位数字。

    C. 扩展精度浮点型

        定义为long double,内存中占64位,8个字节,同双精度浮点型。

2. void空类型

    void类型没有对应的值,是一种特殊的类型,通常用做无返回值函数,以及通用的指针使用,在此不做详谈,等以后接触到的时候,再提及。

Guess you like

Origin blog.csdn.net/rsp19801226/article/details/80653885