From f7cf039e17f925c42a9c1953e776d27f7633998c Mon Sep 17 00:00:00 2001 From: "Nolte, David D" Date: Tue, 7 Feb 2023 14:54:52 -0500 Subject: [PATCH] Add files via upload --- hamming.m | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 hamming.m diff --git a/hamming.m b/hamming.m new file mode 100644 index 0000000..65f72de --- /dev/null +++ b/hamming.m @@ -0,0 +1,20 @@ +%function y = hamming(x,y) +%Hamming binary distance between non-negative decimal integers x and y + +function hsum = hamming(x,y) + +sx = ceil(log2(x+1)); +sy = ceil(log2(y+1)); +sz = max(sx,sy); + +xb = dec2bin(x); +yb = dec2bin(y); + +z = bitxor(x,y); + +sum = 0; +for loop = 1:sz + sum = sum + bitget(z,loop); +end + +hsum = sum; \ No newline at end of file