Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Djb2 hash function
unsigned long hash(char *str) {
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash % NUM_BUCKETS;
}
@gatsby003

This comment has been minimized.

Copy link

@gatsby003 gatsby003 commented Aug 18, 2020

thank you ! This helped me with building a sweet hash table.

@MarcusXavierr

This comment has been minimized.

Copy link

@MarcusXavierr MarcusXavierr commented Aug 20, 2020

Thanks man

@geekyDrake

This comment has been minimized.

Copy link

@geekyDrake geekyDrake commented Oct 10, 2020

Thanks for the code - really helped!!

@hein-j

This comment has been minimized.

Copy link

@hein-j hein-j commented Nov 2, 2020

thank you so much! the one that's widely circulating wouldn't compile. But this one did.

@XBoy-352

This comment has been minimized.

Copy link

@XBoy-352 XBoy-352 commented Dec 20, 2020

its not compiling and its saying "conflicting types for 'hash'" and looks like the unsigned long hash(char *str) and then again unsigned long hash = 5381 are conflicting

@Ernestgio

This comment has been minimized.

Copy link

@Ernestgio Ernestgio commented Jan 24, 2021

Thank you for the code! :D

@wbarbosa0

This comment has been minimized.

Copy link

@wbarbosa0 wbarbosa0 commented Apr 22, 2021

its not compiling and its saying "conflicting types for 'hash'" and looks like the unsigned long hash(char *str) and then again unsigned long hash = 5381 are conflicting

Write like it this:

unsigned long hash = 5381L;

@Anudip2019

This comment has been minimized.

Copy link

@Anudip2019 Anudip2019 commented Sep 13, 2021

thanks very cool

@mongnak

This comment has been minimized.

Copy link

@mongnak mongnak commented Sep 18, 2021

hi I'm noob
what does while ((c=*str++)) means?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment