Some people have mentioned the difference between char and unsigned char recently. Of course, if you have just learned a computer orProgramming LanguageIt is very simple for people. In my opinion, it is nothing more than the difference between signed and unsigned.
This question reminds me of the difference in complement, source code, and reverse code when I used to learn computer knowledge. Here is a referenceArticlePart of [1:
Note: '=' means equal. '=' Indicates a value.
In the machine world:
The highest digit of a positive number is sign bit 0, and the highest digit of a negative number is sign bit 1.
For positive numbers: reverse code = complement code = original code.
Negative number: the anti-code = gets the inverse of all characters except the symbol bit.
Complement = reverse code + 1.
Original code = reverse code after complement-1 = reverse code of complement + 1. (After reading this article, we should be able to intuitively understand the correctness of this formula)
You can easily find the following rules:
Natural computing: A-B = C.
Computer computing: the complement of a-B = a + B = D.
The complement code of C is D.
By this method, subtraction can be converted to addition.
Therefore, the purpose of code complementing is:
1. Make the symbol bit and the valid value part participate in the operation to simplify the operation rules.
2. The subtraction operation is converted to the addition operation, which further simplifies the line design of the calculator in the computer.
It's very clear, right. However, data type conversion is often performed in computers. When Char or unsigned char is converted to int, the difference between the two is obvious. Some articles [2]CodeThe conversion process is verified.
1) When I assign a value of-100 to both UCH and Sch, both UCH and Sch are hexadecimal 0x9c.
2) at this time, because one of the two is signed and the other is unsigned, we can see that in the decimal output, the unsigned is 156, while the signed, the first bit is interpreted as a negative value-100.
3) Then let's look at the UCH type conversion (INT) and then look at the true value, original code, back code and complement code.
4) Finally, let's look at the type conversion (INT) of Sch and then look at the true value, original code, back code, and complement code.
We can see that the biggest difference between UCH and Sch is the previous symbol bit. Just that bit, for our computer, the stored content (complement) will be absolutely different.
For the conversion codes of true values, original codes, reverse codes, and supplementary codes, see references [2]
[CPP] View plaincopyprint?
-
- /* Check uchar */
-
- VoidCheckuchar (unsignedCharUch)
- {
-
- IntX;
-
- CharB [Max + 1];
-
-
- X = uch;
-
- Printf ("Checkuchar decimal value: % d \ n", X );
-
-
- Truthvalue (B, X );// Obtain the true value
- Printf ("Truthvalue: \ t % s \ n", B );
-
-
- Trueform (B, X );// Obtain the original code
-
- Printf ("Trueform: \ t % s \ n", B );
-
-
- Radixminus (B, X );// Obtain the anti-code
-
- Printf ("Radixminus: \ t % s \ n", B );
-
- Complement (B, X );// Obtain the completion code
-
- Printf ("Complement: \ t % s \ n", B );
-
- }
-
-
- /* Check schar */
-
- VoidCheckschar (CharSch)
-
- {
-
- IntX;
- CharB [Max + 1];
-
-
- X = Sch;
-
- Printf ("Checkschar decimal value: % d \ n", X );
-
-
- Truthvalue (B, X );// Obtain the true value
-
- Printf ("Truthvalue: \ t % s \ n", B );
-
- Trueform (B, X );// Obtain the original code
-
- Printf ("Trueform: \ t % s \ n", B );
-
-
- Radixminus (B, X );// Obtain the anti-code
-
- Printf ("Radixminus: \ t % s \ n", B );
-
-
- Complement (B, X );// Obtain the completion code
- Printf ("Complement: \ t % s \ n", B );
-
- }
-
-
- IntMain ()
-
- {
-
- UnsignedCharUch =-100;
-
- CharCh =-100;
-
- Printf ("HEX: uch = 0x % x, Sch = 0x % x \ n", Uch, Sch );
- Printf ("Dec: uch = % d, Sch = % d \ n", Uch, Sch );
-
-
- Checkuchar (UCH );
-
- Checkschar (Sch );
-
-
- Return0;
-
- }
References:
[1] How is anti-code and complement technology proposed?
[2] idle original code, complement code, and reverse code