1

I saw in some C++ code the keyword "unsigned" in the following form:

const int HASH_MASK = unsigned(-1) >> 1;

and later:

unsigned hash = HASH_SEED;

(it is taken from the CS106B/X reader - of Stanford - by Eric S. Roberts - on the topic of "implementation of the hash code function for strings").

Can someone tell me please what does that keyword mean and when do I use it anyway?

Thanks!

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • Already answered here : http://stackoverflow.com/questions/621290/what-is-the-difference-between-signed-and-unsigned-variables – Mike B May 15 '13 at 14:55
  • `const int HASH_MASK = MAX_INT;` seems like a better bet to me. Or I guess maybe `std::numeric_limits::max()` in C++? – Carl Norum May 15 '13 at 15:15

6 Answers6

1

Take a look: https://stackoverflow.com/a/7176690/1758762

unsigned is a modifier which can apply to any integral type (char, short, int, long, etc.) but on its own it is identical to unsigned int.

Community
  • 1
  • 1
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
0

It's a short version of unsigned int. Syntactically, you can use it anywhere you would use any other datatype like float or short.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

Unsigned types are types that can't represent negative numbers; only zero and positive numbers. In C++, they use modular arithmetic; the modulus for an N-bit type is 2^N. It's a good idea to use unsigned rather than signed types when messing around with bit patterns (for example, when calculating hash codes), since C++ allows several different representations of negative numbers which could lead to portability issues.

unsigned can be used as a qualifier for any integer type (e.g. unsigned int or unsigned long long); or on its own as shorthand for unsigned int.

So the first converts -1 into unsigned int. Due to modular arithmetic, this gives the largest representable value. This could also be written (more clearly, in my opinion) as std::numeric_limits<unsigned>::max().

The second declares and initialises a variable of type unsigned int.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

Values are signed by default, which means they can be positive or negative. The unsigned keyword is used to specify that a value must be positive.

Signed variables use 1 bit to specify whether the value is positive or not. The unsigned keyword actualy makes this bit part of the value (thus allowing bigger numbers to be stored).

Lastly, unsigned hash is interpreted by compilers as unsigned int hash (int being the default type in C programming).

yoones
  • 2,394
  • 1
  • 16
  • 20
0

To get a good idea what unsigned means, one has to understand signed and unsigned integers. For a full explanation of twos-compliment, search Wikipedia, but in a nutshell, a computer stores negative numbers by subtracting negative numbers from 2^32 (for a 32-bit integer). In this way, -1 is stored as 2^32-1. This does mean that you only have 2^31 positive numbers, but that is by the by. This is known as signed integers (as it can have positive or negative sign)

Unsigned tells the compiler that you don't want twos compliment and are dealing only in positive numbers. When -1 is typecast (as it is in the code) to an unsigned int it becomes

2^32-1 = 0b111111111...

Thus that is an easy way of getting a whole lot of 1s in binary.

Use unsigned rarely. If you need to do bit operations, or for some reason need only positive integers bigger than 2^31. Otherwise, if you leave it out, c++ assumes signed integers.

rspencer
  • 2,651
  • 2
  • 21
  • 29
0

C allows chars to be signed or unsigned, depending on which is more efficient for the host computer. if you want to be sure your char is unsigned, you can declare your variable to be unsigned char. You can use signed char if you want the ensure signed interpretation.

Incidentally, the C and C++ compilers treatd char, signed char, and unsigned char as three distinct types, even though char is compiled into one of the other two.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29