Skip to content

Instantly share code, notes, and snippets.

@nickisnoble
Last active January 29, 2020 03:14
Show Gist options
  • Save nickisnoble/9df9b605efcb0df4d23ca9fca069f9a7 to your computer and use it in GitHub Desktop.
Save nickisnoble/9df9b605efcb0df4d23ca9fca069f9a7 to your computer and use it in GitHub Desktop.
Hashing function in c
#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