Handmade Hero»Forums»Code
17 posts
Day 049 - Getting sign of unsigned numbers...
Edited by mallardz on
I don't quite understand the code at line 418 in handmade.cpp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    uint32 StartTileX = OldPlayerP.AbsTileX;
    uint32 StartTileY = OldPlayerP.AbsTileY;
    uint32 EndTileX = NewPlayerP.AbsTileX;
    uint32 EndTileY = NewPlayerP.AbsTileY;

    if(EndTileY > StartTileY)
    {
        int x = 4;
    }
    
    int32 DeltaX = SignOf(EndTileX - StartTileX);
    int32 DeltaY = SignOf(EndTileY - StartTileY);



We seem to be testing the sign of the result of an operation between two unsigned values...
Surely this will always be positive?
Are we just missing a cast?
Simon Anciaux
1341 posts
Day 049 - Getting sign of unsigned numbers...
Edited by Simon Anciaux on Reason: Typos
You can subtract 2 unsigned values and get a signed value.
1
2
3
uint32 a = 1;
uint32 b = 2;
int32 c = a - b; // c = -1
17 posts
Day 049 - Getting sign of unsigned numbers...
Ah ok, so as the SignOf function parameter is defined as a signed int it all works. I get it, thanks!