Last active
January 29, 2020 03:14
-
-
Save nickisnoble/9df9b605efcb0df4d23ca9fca069f9a7 to your computer and use it in GitHub Desktop.
Hashing function in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
// Hashes word to a number | |
unsigned int hash(const char *word) | |
{ | |
// "pneumonoultramicroscopicsilicovolcanoconiosis" | |
// Another thought, | |
// Coverting to binary, adding up all the 1s (ignoring place) gives us 207 | |
// If all ones, it would be 360 | |
int i = strlen(word), | |
o = 0; | |
while(i--) | |
{ | |
// Store each char as ascii | |
int ascii = (int) word[i]; | |
// As long as there's a letter | |
while(ascii) | |
{ | |
// Convert to base 2, | |
// and add the '1's together | |
ascii /= 2; | |
o += ascii % 2; | |
} | |
} | |
// Return the total | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment