Skip to content

Instantly share code, notes, and snippets.

@fend25
Created November 6, 2015 11:52
Show Gist options
  • Save fend25/99347aba1903c881ae48 to your computer and use it in GitHub Desktop.
Save fend25/99347aba1903c881ae48 to your computer and use it in GitHub Desktop.
hamming code on C# (for 11 digits, 15 total)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hammingCode
{
class Program
{
public const bool t = true;
public const bool f = false;
public const int startWith = 2;
static int length;
static bool[] Encode(bool[] code)
{
var encoded = new bool[length];
int i = startWith, j = 0;
while (i < length)
{
if (i == 3 || i == 7) i++;
encoded[i] = code[j];
i++;
j++;
}
encoded[0] = Helpers.doXoringForPosition(encoded, length, 1);
encoded[1] = Helpers.doXoringForPosition(encoded, length, 2);
encoded[3] = Helpers.doXoringForPosition(encoded, length, 4);
if (length > 7)
encoded[7] = Helpers.doXoringForPosition(encoded, length, 8);
return encoded;
}
static bool[] Decode(bool[] encoded)
{
var decoded = new bool[11];
int i = startWith, j = 0;
while (i < length)
{
if (i == 3 || i == 7) i++;
decoded[j] = encoded[i];
i++;
j++;
}
return decoded;
}
static int ErrorSyndrome(bool[] encoded)
{
int syndrome =
(Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 1) ^ encoded[0])) +
(Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 2) ^ encoded[1]) << 1) +
(Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 4) ^ encoded[3]) << 2);
if (length > 7) syndrome +=
(Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 8) ^ encoded[7]) << 3);
return syndrome;
}
static void MixinSingleError(bool[] encoded, int pos)
{
encoded[pos - 1] = !encoded[pos - 1];
}
static void Main(string[] args)
{
length = 15;
int errorPosition = 10;
string codeString = "01010101111";
var code = Helpers.prettyStringToBoolArray(codeString);
var encoded = Encode(code);
Console.WriteLine(Helpers.boolArrayToPrettyString(code));
Console.WriteLine(Helpers.boolArrayToPrettyString(encoded));
MixinSingleError(encoded, errorPosition);
Console.WriteLine(Helpers.boolArrayToPrettyString(encoded));
Console.WriteLine(ErrorSyndrome(encoded));
encoded[errorPosition-1] = !encoded[errorPosition-1];
var decoded = Decode(encoded);
Console.WriteLine(Helpers.boolArrayToPrettyString(decoded));
Console.WriteLine(Enumerable.SequenceEqual(code, decoded));
Console.WriteLine();
Console.ReadLine();
}
}
public class Helpers
{
public static String boolArrayToPrettyString(bool[] arr)
{
return String.Join("", arr.Select(x => Convert.ToInt32(x)));
}
public static bool[] prettyStringToBoolArray(String s)
{
return s.ToArray().Select(x => ((Convert.ToInt32(x) - 48) > 0)).ToArray();
}
public static bool notPowerOf2(int x)
{
return !(x == 1 || x == 2 || x == 4 || x == 8);
}
public static int[] getPositionsForXoring(int length, int currentHammingPosition)
{
var positions = new List<int>();
for (int i = 1; i <= length; i++)
{
if ((i & currentHammingPosition) > 0 && notPowerOf2(i))
positions.Add(i);
}
return positions.ToArray();
}
public static bool doXoringForPosition(bool[] vector, int length, int currentHammingPosition)
{
return getPositionsForXoring(length, currentHammingPosition)
.Select(x => vector[x - 1])
.Aggregate((x, y) => x ^ y);
}
}
}
@sobi1995
Copy link

hi
i have question for 100 bit or more.

string codeString = "0101010111101010100011100101010011011001100011001001010010011001100100101000";
do your code can handle my bit?

@fend25
Copy link
Author

fend25 commented Mar 21, 2020

I'm not sure - this algorithm supports strings of length 15

@sobi1995
Copy link

I'm not sure - this algorithm supports strings of length 15

thanks fend25 I do it : )

@qtran98
Copy link

qtran98 commented Mar 15, 2021

Did you figure out @sobi1995? the code above is only up to 8 bits I believe

@DaTweaks
Copy link

DaTweaks commented Jul 2, 2024

I tested it, and for the "testing" and it seems like it was done really wierdly.
Decoding doesn't fix the bit at all. You have to go through ErrorSyndrome and Get the faulty position and Flip it yourself.
I've updated my code so it just flips the faulty bit when it decodes.

so what i basically did was added this to the Decode function:

        static bool[] Decode(bool[] encoded)
        {
            int faultyBitPosition = ErrorSyndrome(encoded);
            if(faultyBitPosition != -1)
            {
                encoded[faultyBitPosition] = !encoded[faultyBitPosition];
            }
            var decoded = new bool[11];

            int i = startWith, j = 0;
            while (i < length)
            {
                if (i == 3 || i == 7) i++;
                decoded[j] = encoded[i];

                i++;
                j++;
            }

            return decoded;
        }

And i edited Error syndrome to say Positions for arrays instead of saying the first being 1 etc etc

        static int ErrorSyndrome(bool[] encoded)
        {
            int syndrome =
                (Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 1) ^ encoded[0])) +
                (Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 2) ^ encoded[1]) << 1) +
                (Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 4) ^ encoded[3]) << 2);
            if (length > 7) syndrome +=
               (Convert.ToInt32(Helpers.doXoringForPosition(encoded, length, 8) ^ encoded[7]) << 3);

            return syndrome-1;
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment