What good are unsigned ints?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
albinopapa
Posts: 4262
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

What good are unsigned ints?

Post by albinopapa » June 25th, 2016, 6:46 am

It has been my experience that unsigned ints are a major pain in the ass to use. Though logically I want to use them for things like size (width and height) which are never negative, but you end casting them all to signed ints anyway because they are safer to use in subtraction operations, which I seem to use a lot.

I think I've read somewhere that unsigned ints should really only be used for memory addresses or something like that...casting pointers to unsigned ints.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: What good are unsigned ints?

Post by chili » June 25th, 2016, 11:13 am

I use unsigned types a lot, and I have never run into the problem of underflowing, but I can see where it might cause problems. Sometimes the opposite is true though, where an underflow would raise alarms but a negative result will silently fail, causing hard to track down bugs.

Most of my problems with unsigned int are keeping them consistent, and getting rid of warnings. Maybe more trouble then they're worth, but I like my data types to be what's written on the box. Mostly just a matter of style I guess.
Chili

albinopapa
Posts: 4262
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: What good are unsigned ints?

Post by albinopapa » June 26th, 2016, 5:34 pm

Here's my problem, it's the way I'm thinking about the problems, I'm use to testing for negative numbers to clamp to 0, when using unsigned numbers I guess I could be testing if their would be an underflow,

if A < B, A = 0 instead of if A - B < 0 A = 0.

I am working on a platformer and with unsigned variables, my character kept teleporting to the end of the map just by going left at the beginning of the map. It's way easier to say if value is less than 0, clamp to 0.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

JDB
Posts: 41
Joined: August 5th, 2015, 9:50 am
Location: Australia

Re: What good are unsigned ints?

Post by JDB » June 27th, 2016, 12:12 pm

The only real advantage to using an unsigned int is probably in situations where you only need to store positive numbers but an unsigned int doesn't go high enough for what you need, or you're not sure how high it might need to go so you need as much room as possible. For example a 32bit integer goes up to about 2 billion but an unsigned int will go up to about 4 billion, so you double the maximum possible value by not having negative values. It comes down to efficient use of memory I guess.

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: What good are unsigned ints?

Post by cyboryxmen » June 27th, 2016, 6:00 pm

Unsigned ints are very useful in memory management because they can represent the full range of standard containers. I faintly remember that there is a bug in a Java sorting algorithm where they add two ints together and divide the result in order to get the middle index and that caused a memory leak because ints could not fully represent the full range of the array being sorted and it returned a negative value. Really, it's not like signed ints can't cause bugs themselves.
-Zekilk
Zekilk

albinopapa
Posts: 4262
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: What good are unsigned ints?

Post by albinopapa » June 27th, 2016, 6:53 pm

@JDB: I've rarely needed the 'room' of an unsigned int, but yeah I see what you mean. When indexing an array you need them, when addressing memory addresses in general you need them, as cyboryxmen says, going from 0x7FFFFFFF to 0x80000000 could be a bad thing if the CPU handles it as signed int, might be off by 1 or 2 because of 2's complement.

One thing I forgot about is using unsigned ints as flags and bit manipulation. Not that you couldn't just use ints, but you aren't likely to use them in math operations if you are using them as flags.

I guess bottom line is I need to remember not to check for solutions less than 0, but rather check that the right side is less or equal to the left side if I'm going to subtract. In bounds checking I run into this BS a lot. Sometimes I feel like it's easier to just use floats for everything, but then there is the rounding errors, grrr!

I'm trying to make a platformer and when I was using unsigned ints for things, everything was drawing correctly, just kept running into problems with bounds checking. Ditched the unsigned ints for floats, now my rects aren't being drawn correctly, these are the things I'm referring to. I will probably go back to unsigned ints and be more diligent with the bounds checking.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

JDB
Posts: 41
Joined: August 5th, 2015, 9:50 am
Location: Australia

Re: What good are unsigned ints?

Post by JDB » June 28th, 2016, 10:28 am

Yeah array indexing is a good example. In my ray tracing engine I used unsigned integers for quite a few things. One thing I used them for was to count the number of vertices in a mesh. There's no need for a negative range in that case and I can index twice as many vertices by using an unsigned integer, but also limit it to 4 billion per object since any more than that would be unreasonable for a game mesh. Another thing I believe I used them for was iterating the line number when reading a text file so I could read twice as many lines. Technically you could use the full range of a signed int to do those things but it wouldn't be as intuitive.

User avatar
chili
Site Admin
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: What good are unsigned ints?

Post by chili » June 28th, 2016, 1:49 pm

I suppose I have used ints to make error checking easier as well. PutPixel uses ints for the coordinates even though strictly on unsigned values make sense. In the bigger picture of a world/camera transform negatives come into play though.

Though come to think of it, it should be easier even to check if out of bounds for unsigned then for signed

unsigned: x >= screen_width -> ur fucked
signed: x < 0 || x >= screen_width -> ur fucked

Could this not apply in most situations in general?
Chili

albinopapa
Posts: 4262
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: What good are unsigned ints?

Post by albinopapa » November 7th, 2016, 6:23 am

Hey, glad i'm not the only one who gets confused about unsigned promotion:
CppCon 2016: Jon Kalb “unsigned: A Guideline for Better Code"
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply