Created
May 26, 2020 07:32
-
-
Save plutooo/fb8c385d5498003f9c44450f17c0e1b3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
typedef void (*HammingDistanceCallback)(uint8_t* Buf); | |
template <size_t NumBytes, size_t Distance> | |
static void HammingDistanceRecurse(uint8_t Buf[NumBytes], HammingDistanceCallback Callback, int Level, int StartPosition) | |
{ | |
Callback(Buf); | |
if (Level == Distance) | |
return; | |
int i; | |
for (i=StartPosition; i<8*NumBytes; i++) | |
{ | |
Buf[i/8] ^= 1 << (i%8); | |
HammingDistanceRecurse<NumBytes, Distance>(Buf, Callback, Level+1, i); | |
Buf[i/8] ^= 1 << (i%8); | |
} | |
} | |
template <size_t NumBytes, size_t Distance> | |
void HammingDistance(uint8_t Buf[NumBytes], HammingDistanceCallback Callback) | |
{ | |
return HammingDistanceRecurse<NumBytes, Distance>(Buf, Callback, 0, 0); | |
} |
This file contains hidden or 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 <stdint.h> | |
#include <stddef.h> | |
#include <stdio.h> | |
#include "HammingDistance.h" | |
void Callback(uint8_t Buf[]) | |
{ | |
int i; | |
for (i=0; i<16; i++) | |
printf("%02x ", Buf[i]); | |
printf("\n"); | |
} | |
int main() | |
{ | |
uint8_t Zeroes[16] = {0}; | |
// Loop over all strings with hamming distance <= 2 from zeroes. | |
HammingDistance<sizeof(Zeroes), 2>(Zeroes, &Callback); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment