Ridiculous DWORD error (SOLVED - thanks)

Started by
7 comments, last by Zahlman 17 years ago
DWORD g_num = 0; g_num--; if (g_num<0) g_num=5; PRINT("gnum = ",g_num); OUTPUT!!!: gnum = -1 Visual Studio 2005 glitch? I can't understand why this doesn't work... Update: DWORD g_num = 0; g_num--; if (g_num<0) g_num=5; if (g_num==-1) g_num=5 PRINT("gnum = ",g_num); OUTPUT: gnum = 5 What the heck is going on here?????????? Last time I checked, -1 is less than 0..... [Edited by - renman29 on April 7, 2007 8:16:34 AM]
Advertisement
What is PRINT?
Isn't DWORD unsigned? Aren't you using a printf-style function in PRINT to print its value, possibly with the %d directive, which is for signed integers?
DWORD is typedef'd as an unsigned long. So 0 - 1 in unsigned evaluates to 4294967295. Which is certainly not less than 0, so the g_num=5; code is never executred.
NextWar: The Quest for Earth available now for Windows Phone 7.
also you should try

if ((int)g_num<0)

The DWORD needs to be an int (or some other signed type)for proper conparison and it seems the compiler doesnt do the conversion for you.
Simplicity is the ultimate sophistication. – Leonardo da Vinci
Oh man - I feel completely dumb now.(:p) Thanks very very much. I'll use the (int). Thanks again. (^-^)
Instead of casting it, why not just use an int variable in the first place?
A good idea - in this circumstance I need a DWORD var - so what I actually ended up doing was something like this:
if (g_num<direction_increment)   g_num=MY_MAX-(direction_increment-g_num)+direction_increment;gnum+=direction_increment;if (g_num>MY_MAX) g_num=(g_num-MY_MAX);


Which is basically this sort of anticipation-logic:
if (gnum==0) gnum=5+1;
gnum--; //gnum becomes 5

I can't believe I didn't realize DWORD's unsigned.. I guess what threw me off was the fact that my PRINT function was still saying -1.
I'll stick to int's as much as possible. Thanks. :)
Quote:Original post by renman29
I guess what threw me off was the fact that my PRINT function was still saying -1.


Then use a better one. (In particular, assuming C++, don't wrap printf-type stuff, because it's not typesafe. Instead, work with iostreams.)

This topic is closed to new replies.

Advertisement