Public Group

Why are unsigned numbers usefull ?

This topic is 5021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 ?

Share on other sites
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

Share on other sites
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.

Share on other sites
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.

Share on other sites
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...

Share on other sites
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

Share on other sites
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.

Share on other sites
Quote:
 Original post by WashuYou 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])]

? :)

• 16
• 11
• 9
• 9
• 49
• Forum Statistics

• Total Topics
631392
• Total Posts
2999740
×