Difference between char and unsigned char after forced conversion to int)

Source: Internet
Author: User

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?

  1. /* Check uchar */
  2. VoidCheckuchar (unsignedCharUch)
  3. {
  4. IntX;
  5. CharB [Max + 1];
  6. X = uch;
  7. Printf ("Checkuchar decimal value: % d \ n", X );
  8. Truthvalue (B, X );// Obtain the true value
  9. Printf ("Truthvalue: \ t % s \ n", B );
  10. Trueform (B, X );// Obtain the original code
  11. Printf ("Trueform: \ t % s \ n", B );
  12. Radixminus (B, X );// Obtain the anti-code
  13. Printf ("Radixminus: \ t % s \ n", B );
  14. Complement (B, X );// Obtain the completion code
  15. Printf ("Complement: \ t % s \ n", B );
  16. }
  17. /* Check schar */
  18. VoidCheckschar (CharSch)
  19. {
  20. IntX;
  21. CharB [Max + 1];
  22. X = Sch;
  23. Printf ("Checkschar decimal value: % d \ n", X );
  24. Truthvalue (B, X );// Obtain the true value
  25. Printf ("Truthvalue: \ t % s \ n", B );
  26. Trueform (B, X );// Obtain the original code
  27. Printf ("Trueform: \ t % s \ n", B );
  28. Radixminus (B, X );// Obtain the anti-code
  29. Printf ("Radixminus: \ t % s \ n", B );
  30. Complement (B, X );// Obtain the completion code
  31. Printf ("Complement: \ t % s \ n", B );
  32. }
  33. IntMain ()
  34. {
  35. UnsignedCharUch =-100;
  36. CharCh =-100;
  37. Printf ("HEX: uch = 0x % x, Sch = 0x % x \ n", Uch, Sch );
  38. Printf ("Dec: uch = % d, Sch = % d \ n", Uch, Sch );
  39. Checkuchar (UCH );
  40. Checkschar (Sch );
  41. Return0;
  42. }

 

 

References:

[1] How is anti-code and complement technology proposed?

[2] idle original code, complement code, and reverse code

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.