hash<<5

Dear All!

Please, can someone make me clear this line:
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

meaning:
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

What exactly means the expressin (hash<<5)?
MANY THANKS!!!


1
2
3
4
5
6
7
8
9
10
11
12
unsigned long
    hash(unsigned char *str)
    {
        unsigned long hash = 5381;
        int c;

        while (c = *str++)
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

        return hash;
    }
Hello!
Please, what is happening here:

1100 1100 >> 1
1110 0110


Looks we moved all 8 signs to the right, took away the last 0 and ADDED one 1 on the beginning???

Where came this 1 from?
Did we loose 0 just because we moved the signs right?


Many thanks!!!
Last edited on
When you do bitwise shift, digits which gone outside of "bounds" gets discarded. When you do right shif on signed numbers it might (implementation defined) fill empty slot which appears after we shifted digit from it with sign bit.
Ok, that was an unclear example.

Bitwise right shift of signed number. You are now in the realm of how implementations represent negative values.
http://msdn.microsoft.com/en-us/library/336xbhcz.aspx

You don't need that in your left-shift of unsigned values.
.
Last edited on
Hello!

This:

0001 1111 >> 3
0000 0011


mowed the whole sequence to the right, by adding 3 zeros from the left, and discarding first 3 right members of the sequence.

But this:
1100 1100 >> 2
1111 0011


and this:
1100 1100 >> 1
1110 0110


added 1s instead of zeros.

What is the point?

many thanks!!!
Last edited on
MiiNiPaa wrote:
When you do right shift on signed numbers it might (implementation defined) fill empty slot which appears after we shifted digit from it with sign bit.
1
2
3
  1100 1100
//↑
//Sign bit 
Hello, sorry for studpi q but just could not resist: is 0 also a sign bit?

Many thanks!

p.d. I understood, 1 or 0 is standing for SIGN (+ OR -), 1 for negative numbers and 0 for positive.

If I understood it right, if we want the reslt to be positive, we add 0, and if not, then 1.

How about adding odd number of any (1 or 0), does the odd number influence?

Many thanks!!!

P.s. Just to remind all, still opened topic about literutre ,books and online!
Last edited on
Hello, sorry for studpi q but just could not resist: is 0 also a sign bit?
0 is just a value any given bit can be. Sign bit is higher order bit in most representations.
p.d. I understood, 1 or 0 is standing for SIGN (+ OR -), 1 for negative numbers and 0 for positive.
http://en.wikipedia.org/wiki/Two%27s_complement
1 in the higher bit is negative, 0 is the rest.

[quote]How about adding odd number of any (1 or 0), does the odd number influence?/quote]I didn't get the question, can you elaborate?

I want to warn again that sign bit expansion is implementation-defined behavior and it can vary even between different versions of one compiler. Generally you should not use bit shifts on signed numbers. Expecially negative numbers.
Topic archived. No new replies allowed.