Skip to content

Instantly share code, notes, and snippets.

@a10k
Last active August 29, 2015 14:02
Show Gist options
  • Save a10k/135e8c25c8e471331962 to your computer and use it in GitHub Desktop.
Save a10k/135e8c25c8e471331962 to your computer and use it in GitHub Desktop.
// Closest 64 bit integers to a given binary 64 bit integer,less than a distance of 4 will be sorted and printed
//input line will have 0,1,2,232983293 .. keys
//input line2 is a 64 char binary 100100111010....11
//we print all keys that are within a max bit error of 4
#include<stdio.h>
#define STR_LEN 40000000
#define KEY_LEN 64
char key_list[STR_LEN]="";
char cmp_key[65]="";
char *key=NULL;
int
main(){
char com[2] = ",";
scanf("%s",&key_list);
scanf("%s",&cmp_key);
key = strtok(key_list,com);
while(key != NULL) {
compare_keys(key);
key = strtok(NULL,com);
}
}
int
compare_keys(char *loc_key) {
int il = KEY_LEN;
char key_bin[65] = "0";
unsigned long int i_tmp = strtoull(loc_key, (char **)NULL, 10);
unsigned long int i = i_tmp;
/* converting to binary */
key_bin[il] = '\0';
key_bin[il-1]=((i & 1) + '0');
il--;
while(il--) {
i = i >> 1;
key_bin[il]=((i & 1) + '0');
}
if(cmp_str(key_bin))
printf("%d,",i_tmp);
}
int
cmp_str(char key_bin[]) {
int i=0;
int count=0;
for(i=0 ; i <= 64 ; i++) {
if(key_bin[i] != cmp_key[i])
count++;
if(count > 4)
return 0;
}
return 1;
}
// (c) Alok Pepakayala
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment