0
Follow
0
View

# Some questions about integer data

dd5323400 注册会员
2023-02-25 20:07

These are mainly differences in the size range of the stored data

• short integer(short), length 2 bytes
• Basic(int), usually length 4 bytes
• long(long), usually length 8 bytes
• unsigned(unsigned) : The first digit is not a sign bit and cannot represent a negative number. The first three have corresponding unsigned types
yiqiu1979 注册会员
2023-02-25 20:07

Part of this answer references GPT, GPT_Pro for better problem solving
Integer data is one of the most common data types in programming. They can be used to store integers. We can use them in many ways, but different languages support different types of integer data, so you need to choose the appropriate type of integer data for your program.

First we should look at the common integer data types:

1. int: int is a commonly used signed integer that can represent positive, negative, and zero numbers.

2. unsigned int: An unsigned int is an unsigned integer that can only represent positive or zero numbers, not negative numbers.

3. long: long is also a signed integer that can represent a larger range of positive, negative, and zero numbers than int.

4. unsigned long: unsigned long is also an unsigned integer that can only represent a larger range of positive numbers or zeros than unsigned int.

5. short: The short type is a signed integer that can represent positive, negative, and zero numbers smaller than the range represented by int.

6. unsigned short: The unsigned short type is also an unsigned integer that can represent a smaller range of positive numbers or zeros than unsigned int.

When selecting the appropriate integer data, first of all, it needs to be selected according to the size of the number to be expressed. If you want to express large or small numbers, use the long or unsigned long type. If you want to express a positive, negative, or zero number of general size, int or unsigned int is used; If you want to express very small positive, negative, or zero numbers, use the short or unsigned short type.

Also need to consider whether to use signed or unsigned. Here we mainly consider two aspects: on the one hand, according to the need to express the positive, negative, zero numbers to decide whether to use the signed or unsigned expression; Another aspect to consider is the form that appears in the code. For example, when a variable appears in multiplication we try to express it unsigned.

Here is an example:

``````// 使用有符号 int 类型
int a = -20;
// 使用无符号 int 类型
unsigned int b = 20;
// 使用有符号 long 类型
long c = -20000;
// 使用无符号 long 类型
unsigned long d = 20000;
// 使用有符号 short 类型
short e = -100;
// 使用无符号short 类型
unsigned short f = 100;
``````

In a word, it is particularly important to choose the appropriate integer data when writing code, because different languages correspond to different terminal word size, whether or not the sign will affect the execution of the code. So when writing code should be based on the actual situation to choose the appropriate integer data.

Publish Time
2023-02-25 20:07
Update Time
2023-02-25 20:07