-
Keywords unsigned and signed
- Data storage in computers
- Transformation of the original code and its complement in relation to the hardware.
- Principle of the original, inverse and complementary.
- The nature of integer storage
- The process of variable access
- Current role of the type
- Quick conversion between decimal and binary
- big-endian byte order
- Determine the byte order of the current machine
- Understanding of "negative zero" (-128)
- truncate
- It is recommended that values of unsigned types be followed by u, the
Keywords unsigned and signed
Data storage in computers
- Any data must be converted to binary in a computer because computers only recognize binary. The computer also has to distinguish whether the data is positive or negative, then the binary is divided into
Sign Bit + Data Bit
. - Integer types stored within a computer must be complementary
- Unsigned numbers and positive numbers have the same base and base complement and are stored directly in the computer. Negative numbers need to be converted to complementary code and then stored.
- The type determines how to interpret the binary sequences held inside the space
- Floating point numbers are double by default, if you want a float you need to add f to the number, such as
float f = 1.1f
;
Transformation of the original code and its complement in relation to the hardware.
Example: int b = -20; //20 = 16+4 = 2^4^ (10000)~2~+ 2^2^(100)~2~
//Signed and negative numbers, original to complement.
1000 0000 0000 0000 0000 0000 0000 0001 0100 Original Code
1111 1111 1111 1111 1111 1111 1111 1111 1111 1011 inverse = inverse of the original code
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1100 Complement = inverse + 1
//Complement to Original
Method 1: Principle
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1100 Complement
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1011 inverse = complement-1
1000 0000 0000 0000 0000 0000 0000 0001 0100 Original = Inverse of the inverse.
Method 2: The way of computer hardware, you can use a hardware circuit to complete the original code and complement of each other
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1100 Complement
1000 0000 0000 0000 0000 0000 0000 0000 0011 Complement Inversion
1000 0000 0000 0000 0000 0000 0000 0000 0100 +1
Principle: complement = mode-original <=> original = mode-complement <=> complement inverse +1
Principle of the original, inverse and complementary.
The concept of inverse complement is introduced by the clock, 8:00 + 2 = 10:00. And 8 - 10 is also equal to 10. That is, 2 is the complement of -10 modulo 12.
-10 is converted to 2 , which can be obtained by modulo -10, but the hardware median is fixed, modulo 1000... The highest bit will overflow and be discarded. That is, all 0. Can not do the difference.Introducing the inverse code conversion calculation: i.e. 2 == modulo -10 == modulo -1+1-10 == 1111... -10 + 1 == inverse +1; this 111... -10 is the inverse, i.e., the origin of inverse +1 == complement.
In binary, all 1 minus any number is directly remove the corresponding 1. Therefore, the inverse code is the original code sign bit remains unchanged, the rest of the bits are inverted.
After converting to complement, there is no need to consider the sign bit at the bottom, and then look at the sign bit after the operation.
The nature of integer storage
defineunsigned int b = -10;
Will it work correctly? The answer is yes.
The process of definition is to open up space, and space can only store binary, and does not care about the content of the data
In order to store data in space, it must first be converted to binary complement. When the data is written to space, it is already converted to complementary code.
The process of variable access
- Stored: Literal data must be converted to complement before being placed in space. The sign bit only depends on whether the data itself carries a +- sign, and has nothing to do with whether the variable is signed or not.
- Fetch: Always look at the type of the variable before deciding whether or not to look at the highest sign bit. If you don't need it, you can convert the data from binary to decimal. If it is needed, it needs to be converted to the original code, and then it can be recognized (also need to consider where the highest sign bit is, and consider the size of the end).
Current role of the type
- Decide how much space to open up before storing data
- How to interpret binary data when reading data
How many pieces of data a particular data type can represent depends on the number of permutations of all of its own bits
Quick conversion between decimal and binary
(Pre-knowledge: need to memorize decimal results from 2^0 to 2^10)
1 -> 2^0
10 -> 2^1
100 -> 2^2
1000 -> 2^3 //1 followed by 3 bits is 2^3
The rule: 1 followed by n zeros is 2^n, i.e., n equals how many zeros follow 1 --- decimal to binary.
The other way around is that 1 followed by a number of zeros is 2 to the nearest square of 2 --- binary to decimal.
So: 2^9 -> 10 0000 0000 // n
Example: 67 = 64+2+1 -> 2^6+2^1+2^0 -> 1000000 + 10 + 1
= 0000 0000 .... 0100 0011
Similarly, binary to decimal is the reverse process
big-endian byte order
Phenomenon: In the vs memory window, the address increases from top to bottom and from left to right.
- Big end: according to the unit of bytes, the low weighted bit data is stored in the high address, which is called the big end.
- Small segment: in accordance with the unit of bytes, the low weighted bit of data stored in the low address, it is called the small end (small small)
(Basically, the small end is the main focus, the big end is less (network))
The size of the end of the storage program, the essence of the data and space in accordance with the unit of byte a mapping relationship
(Consider the size end of the problem is more than 1 byte type. short, int, double ...)
Determine the byte order of the current machine
- Method 1: For
int a = 1
Take the first address, then(char*)&a
If the value obtained is 1, it is the small end, otherwise it is the big end. - Method 2: Open the Memory window to see the byte order of the address and data.
Understanding of "negative zero" (-128)
(The concept of negative zeros does not exist, it just happens to be similar.)
-The 128 is actually deposited into the computer as 1 1000 0000 (counting group operator). But there are only 8 bits of space, so the truncation occurs, and you get 1000 0000.
And [1111 1111,1000 0001] ~ [0000 0000,0111 1111]
i.e. [-127,-1]~[0,127] natural numbers have been used.
A computer cannot waste every space (the smallest possible cost to solve as many calculations as possible), and naturally 1000 0000 needs to have a corresponding meaning. Therefore the value -128 is assigned.
Since truncation is impossible to recover, this is a half-computational, half-prescriptive approach.
truncate
Truncation is the truncation of the higher bits when there is not enough space for the data.
Is it the high or the low bit that is truncated? Because assignments are always made from the low address (in descending order), the high bit is discarded when there is not enough space.
Note: The overflow generated by the rounding of an expression is not a truncation, the rounding is saved in the relevant registers. Truncation occurs only when the result is stored in a variable that can't fit because of the overflow and has to discard part of the data.
1 0000 0001 0100
1 1111 1110 1100
0 0000 0000 1010
1 1111 1111 0110
1 0000 0000 1010
It is recommended that values of unsigned types be followed by u, the
The default value is signed, adding u to the value is stricter and more readable.unsigned int a = 10u;