Why are unsigned numbers usefull ?

Started by
6 comments, last by Zahlman 19 years, 7 months ago
Why do we use unsigned numbers please provide detail example thanks. a char for example can have a range of -128 to 127 when unsigned it can be from 0 to 255 when would I really need to have an unsigned value ?
http://www.phusnikn.net
Advertisement
There are number of places where unsigned rumbers are very usefull. I use them everywhere I can. Just a couple of examples:
-Array index is always unsigned, since array starts at 0, so you have no use for negative part. This also includes all vertex array/buffer stuff.
-Whenever you have a value that can't ever be negative.
-When using unsigned number to hold a bitset.
-...

edit: beaten by nobodynews
You should never let your fears become the boundaries of your dreams.
Quote:Original post by _DarkWIng_
There are number of places where unsigned rumbers are very usefull. I use them everywhere I can. Just a couple of examples:
-Array index is always unsigned, since array starts at 0, so you have no use for negative part. This also includes all vertex array/buffer stuff.

Not true, you can have negative indices in C and C++. Of course, this does not apply to the standard library, and it's usefulness is questionable, but possible none the less.
#include <iostream>int main() {	char* p = new char[10]();	char* p2 = p + 5;	p2[-5] = 'A';	std::cout<<p<<std::endl;	delete [] p;}

is a simple example.
Quote:
-Whenever you have a value that can't ever be negative.
-When using unsigned number to hold a bitset.

These are excellent reasons here.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Washu: I know that is possible, but I belive it is far from good praktice. In C/C++ you can do alot of rally really nasty things.
You should never let your fears become the boundaries of your dreams.
Quote:Original post by _DarkWIng_
Washu: I know that is possible, but I belive it is far from good praktice. In C/C++ you can do alot of rally really nasty things.

You can do a lot of really nasty things in any language. Some might say C/C++ makes it easier. They might be right, but then again...

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Another good reason I can think of to use unsigned types is when you plan to do bit-shifting and you want to make sure there is no sign extension in the shifts.

shmoove
Unsigned numbers are useful for two things:

1. It is a bad idea to use signed types when doing bit-wise operations -- primarily because of shift operations.
2. They have one more bit of range in the positive numbers.

Other than those, there are really no other good reasons. Some people use unsigned because the value is never supposed to be less than 0. However, that doesn't really do much because it doesn't prevent you from assigning a negative value to an unsigned type.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by Washu
You can do a lot of really nasty things in any language.


You mean like:

def p(*l): return [[f] + r for f in l[:1] and l[0] for r in p(*l[1:])] or [[[]],[]][bool(l[:1])]


? :)

This topic is closed to new replies.

Advertisement