473,473 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hash Function

Hello all,
I didn't know there is a thread on hash function started in this newsgroup
so reposted my posting from another group.
Hope I can have some feedbacks. I am new to hash table.
I came across a very well presented tutorial web page wrt hash table. In
its content, it listed a number of hash functions which the web master(?)
quoted from other web sites. So no explainations for these hash functions
and I failed to understand a couple of them. They are quite similar so I
ask help for one. Wonder if someone can shed some light for me?
/*Peter Weinberger's*/
int hashpjw(char *s)
{
char *p;
unsigned int h,g;
h=0;
for (p=s; *p != '\0'; p++)
{
h = (h<<4) + *p;
if (g = h & 0xF0000000)
{
h ^= g>>24;
h ^= g;
}
}
return h%211;
}

Within the for loop, what exactly does the second line do? Bit shifting by
4 units and added to the particular char entry of the string?
The third line in the for loop, what is the purpose of bitwise and h and
0xF0000000? Why 0xF0000000 in the first place?
I failed to understand the logic of the next two lines within the
conditional statement as well?

Thanks in advance,
FD
Nov 14 '05 #1
4 7215

On Sat, 20 Dec 2003, flipdog wrote:

Hello all,
I didn't know there is a thread on hash function started in this newsgroup
so reposted my posting from another group.
For future reference, it would be nice of you to mention *which*
other group contained the original post. And both for courtesy's
sake and the avoidance of unnecessary discussion [like this], it
would be nice of you to summarize what you've learned from that
other thread and why you still want more information.
Hope I can have some feedbacks. I am new to hash table.
I came across a very well presented tutorial web page wrt hash table.
...which can be found at the following URL: and so on.
Don't forget the common sense, please. [All I find is a
Spanish-language Postscript document, which may or may not be
the document to which you're referring.]
In its content, it listed a number of hash functions which the web master(?)
quoted from other web sites. So no explainations for these hash functions
and I failed to understand a couple of them. They are quite similar so I
ask help for one. Wonder if someone can shed some light for me?
If you want real information as to *why* the hash function is
designed the way it is, or information on how the various lines
interact, I recommend you try comp.programming or sci.crypt.
Hash functions, to me at least, are entirely "black magic," and
I don't expect any useful answers you might get would be on
topic for comp.lang.c, which only discusses the C programming
language (and not the higher math and statistical methods
necessary [IMLE] to fully understand hashing).
Here is what the function does, in "plain" English, since
it sounds like you're unfamiliar with C operators and syntax.

/*Peter Weinberger's*/
int hashpjw(char *s)
{
char *p;
unsigned int h,g;
h=0;
for (p=s; *p != '\0'; p++)
{
Loop over the text string 's', using the variable 'p' as
a sort of index into the string. This is not the way I'd
have written this function today, which implies to me that
this function is taken from an old (1980s or early 90s?)
source. [And a quick Google implies I'm pretty much right.
I hadn't recognized PJW as the W in "AWK"!]
h = (h<<4) + *p;
For each character in the string, add its numerical value
to the quantity obtained by shifting h left by four bits
(multiplying h by 16), and store the result back in h.
h is an "accumulator" or "state" variable (which probably
has another buzzword in crypto/hash jargon) -- it starts out
at zero, and then changes in some hopefully-unpredictable
fashion each time through the loop.
if (g = h & 0xF0000000)
Compute the bitwise AND of h and the hexadecimal constant
0xF0000000 (the high-order four bits of h) and store the
result in g.
{
h ^= g>>24;
h ^= g;
}
Bitwise XOR the value in h with the value of g shifted
right by 24 bits, and then XOR that result with the
original (unshifted) value of g. Store the result back
in h.
Note that the 'if ( ) { }' is unnecessary here; it merely
skips the last two steps if the value of g is zero (in
which case they would have no effect). In other words,
it's an optimization -- one that might not really matter
on modern computers anyway, but *that's* a topic for
comp.arch or some such newsgroup. :)
}
Continue looping over the characters in 's'.
return h%211;
Return the value of the state variable h, modulo 211,
which is simply a small prime. This discards a lot of
information out of the high-order digits of h, but that's
okay because h has been quite well mixed-up by the loop
operations.
}

Within the for loop, what exactly does the second line do? Bit
shifting by 4 units and added to the particular char entry of the
string?
Yes. Characters in C are treated as small integers; most
[but not all] systems these days use [a superset of] ASCII encoding,
which has, for instance, 'A'=65, 'd'=100, and so on. The exact
encoding doesn't matter here, of course -- you could just as easily
think of 'hashpjw' as operating on a list of byte values.
The third line in the for loop, what is the purpose of bitwise and h and
0xF0000000? Why 0xF0000000 in the first place?
I failed to understand the logic of the next two lines within the
conditional statement as well?


Black magic. Consult sci.crypt for the details. [Maybe
someone else can suggest a better group; I know sci.crypt
covers secure hash functions, but I don't know if there might
be a more beginner-oriented, hash- or math-related group out
there.]

HTH,
-Arthur
Nov 14 '05 #2
flipdog wrote:

I came across a very well presented tutorial web page wrt hash
table. In its content, it listed a number of hash functions
which the web master(?) quoted from other web sites. So no
explainations for these hash functions and I failed to
understand a couple of them. They are quite similar so I ask
help for one. Wonder if someone can shed some light for me?

/*Peter Weinberger's*/
int hashpjw(char *s)
{
char *p;
unsigned int h,g;
h=0;
for (p=s; *p != '\0'; p++)
{
h = (h<<4) + *p;
if (g = h & 0xF0000000)
{
h ^= g>>24;
h ^= g;
}
}
return h%211;
}

Within the for loop, what exactly does the second line do? Bit shifting by
4 units and added to the particular char entry of the string?
The third line in the for loop, what is the purpose of bitwise and h and
0xF0000000? Why 0xF0000000 in the first place?
I failed to understand the logic of the next two lines within the
conditional statement as well?


That is picking off the high 4 bits of the (assumed) 32 bit
integer. The function is not portable, because it depends on the
size of an int. The following are the generic functions I
referred you to in my earlier post, which are portable.

/* ============= Useful generic functions ============= */

/* NOTE: hash is called once per operation, while rehash is
called _no more_ than once per operation. Thus it
is preferable that hash be the more efficient.
*/

/* Hash a string quantity */
unsigned long hshstrhash(const char * string)
{
unsigned long h;

h = 0;
while (*string) {
h = h * 31UL + (unsigned char) *string++;
}
return h;
} /* hshstrhash */

/* 1------------------1 */

/* ReHash a string quantity */
unsigned long hshstrehash(const char * string)
{
unsigned long h;

h = 0;
while (*string) {
h = h * 37UL + (unsigned char) *string++;
}
return h;
} /* hshstrehash */

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3

"flipdog" <jo******@worldnet.att.net> wrote in message
news:Pq***********************@bgtnsc04-news.ops.worldnet.att.net...
Hello all,
I didn't know there is a thread on hash function started in this newsgroup
so reposted my posting from another group.
Hope I can have some feedbacks. I am new to hash table.


Have you got a working hashtable code? Did the page yout mentioned
provide source for hashtable? Hashing function is of course part of that
but usually you have to trust to "black magic" of tried and tested hashing
algorithm (mr. O'Dryer covered that black magic
part already).

You just have to rely on benchmarks like
http://www.cs.usyd.edu.au/~scilect/s...s/lect02-2.htm
to choose appropriate hash function/algorithm for your table. Of course
benchmarking
yourself and/or running some "hashtable_inserts" with debugger would be very
educational.

here's one hashtable implementation (google for more yourself)
http://c.snippets.org/snip_lister.php?fname=hash.c
http://c.snippets.org/snip_lister.php?fname=hash.h

with respect,
Toni Uusitalo

Nov 14 '05 #4

"Toni Uusitalo"

Have you got a working hashtable code? Did the page yout mentioned
provide source for hashtable? Hashing function is of course part of that
but usually you have to trust to "black magic" of tried and tested hashing
algorithm (mr. O'Dryer covered that black magic
part already).

You just have to rely on benchmarks like
http://www.cs.usyd.edu.au/~scilect/s...s/lect02-2.htm
to choose appropriate hash function/algorithm for your table. Of course
benchmarking
yourself and/or running some "hashtable_inserts" with debugger would be very educational.

here's one hashtable implementation (google for more yourself)
http://c.snippets.org/snip_lister.php?fname=hash.c
http://c.snippets.org/snip_lister.php?fname=hash.h

with respect,
Toni Uusitalo


Thank you for this post. I have been searching for information like this
with google on my own and getting gazillions of pages concerning java and "c
sharp" with a few C++ pages thrown in. Perhaps my choice of search strings
was bad....

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
6
by: barcaroller | last post by:
I'm looking for a hash function (in C) that will convert a string of arbitrary length (but less than 1024 chars) to a reasonably-unique 16-bit short integer. Can anyone point me to such a hash...
6
by: thecodemachine | last post by:
Hi, I'm looking for a fast and simple one to one hash function, suitable for longer strings (up to 2048 in length). I'd like keys to be relatively short, I doubt I'd be creating more than 256...
12
by: Arash Partow | last post by:
Hi all, I've ported various hash functions to python if anyone is interested: def RSHash(key): a = 378551 b = 63689 hash = 0
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
11
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.