Sign in to follow this  
phusnikn

Why are unsigned numbers usefull ?

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this