| |

VerySource

 Forgot password?
 Register
Search
View: 1001|Reply: 7

In C / C ++, how do signed conversions and unsigned numbers compare and convert?

[Copy link]

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2019-12-27 17:33:57
| Show all posts |Read mode
The impression is that the two sides of == are unified as an unsigned number for comparison, but today I encountered a problem. I wrote the code and tested it, and I don't understand it :(

The following code part1 is a comparison of signed and unsigned numbers of different lengths. It seems that the contents of their respective memories are directly compared after expanding to the same length, but from the part2, the result is not the case.

How do you understand it? Thank you very much! !! !! !! !!



#include <iostream>
using namespace std;

int main ()
{
  / * part 1 * /

  cout << "(bool) ((unsigned long) -5 (" << hex << (unsigned long) -5 << ")"
       << "== (unsigned short) -5) (" << hex << (unsigned short) -5 << "):"
       << (((unsigned long) -5 == (unsigned short) -5)? "true": "false") << endl;

  cout << "(bool) ((signed long) -5 (" << hex << (signed long) -5 << ")"
       << "== (signed short) -5) (" << hex << (signed short) -5 << "):"
       << (((signed long) -5 == (signed short) -5)? "true": "false") << endl;
  
  cout << "(bool) ((unsigned long) -5 (" << hex << (signed long) -5 << ")"
       << "== (unsigned short) -5) (" << hex << (signed short) -5 << "):"
       << (((unsigned long) -5 == (signed short) -5)? "true": "false") << endl;
  
  cout << "(bool) ((signed long) -5 (" << hex << (unsigned long) -5 << ")"
       << "== (signed short) -5) (" << hex << (unsigned short) -5 << "):"
       << (((signed long) -5 == (unsigned short) -5)? "true": "false") << endl;


  / ************************************************* ************ /
  / * part 2 * /

  signed short x = -1;
  unsigned short y = 65535;
  cout << "(bool) (x (" << hex << x << ") == y (" << hex << y << ") ="
       << (x == y? "true": "false") << endl;
       
  x = 0xffff;
  y = 0xffff;
  cout << "(bool) (x (" << hex << x << ") == y (" << hex << y << ") ="
       << (x == y? "true": "false") << endl;

  cin.get ();
}
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2019-12-27 17:34:35
| Show all posts
By the way, attach the results of Dev-cpp

(bool) ((unsigned long) -5 (fffffffb) == (unsigned short) -5) (fffb): false
(bool) ((signed long) -5 (fffffffb) == (signed short) -5) (fffb): true
(bool) ((unsigned long) -5 (fffffffb) == (unsigned short) -5) (fffb): true
(bool) ((signed long) -5 (fffffffb) == (signed short) -5) (fffb): false
(bool) (x (ffff) == y (ffff) = false
(bool) (x (ffff) == y (ffff) = false
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-5 12:06:01
| Show all posts
Top it yourself. . .
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-7 09:51:01
| Show all posts
In this case, the programmer should give a clear processing method, and cannot rely on the behavior of the compiler. You must know that there are many compilers, and their processing may be disrespectful, but the programmer needs only one result.
We can ask the compiler to help us find these error-prone places that are hard to find:
1. Make the compiler warning level the highest.
2. Let the compiler see "warnings as errors".
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-2-17 00:15:01
| Show all posts
All operations are automatically upgraded to int at least to start calculation.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-1 12:45:01
| Show all posts
Yes, from int to int, after int are signed-> unsigned
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-25 10:00:01
| Show all posts
12) The eight queens problem
On an 8 * 8 chess board, there are eight queens, one for each queen. It is required that there should be no "attack" between queens. Neither two queens can be in the same row, same row or On the same diagonal. Ask how many different methods are available.
Each student chooses 1-2 topics for programming and debugging.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Taiwan

Post time: 2020-9-6 13:15:01
| Show all posts
[Quote=Quote the reply of genius_hb on the 5th floor:]
Yes, less than int to int, after int is signed -> unsigned
[/Quote]

learned.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list