0

I wondering if there is a specific name for binary encoding of integer when each representation has the same number of 1s and 0s. Here an example

 0 000111
 1 001011
 2 001101
 3 001110
 4 010011
 5 010101
 6 010110
 7 011001
 8 011010
 9 011100
10 100011
11 100101
12 100110
13 101001
14 101010
15 101100
16 110001
17 110010
18 110100
19 111000

I believe this kind of code should be in frequent used in artificial binary networks to equalize number of active neurons for any input. I thought this kind of code is related to Hamming code, but it seems I was wrong.

rth
  • 2,946
  • 1
  • 22
  • 27

1 Answers1

0

Thanks a lot to @m69, I got good idea of walking bit algorithm, which after some small modifications does the job. I post this python code here just in case someone else will need it.

def equalcodegen(size):
    x = [ 1 for _ in xrange(size/2)]+[ 0 for _ in xrange(size/2)]
    def walk(seq):
        idx=0
        while seq[idx] != 0: idx+=1
        if idx == 0: 
            s = seq[:]
            yield []
        else:
            seq[idx-1] = 0
            for pos in xrange(idx, len(seq)):
                s = seq[:]
                s[pos] = 1
                yield s
                for p in walk(s[:pos]):
                    if len(p) == 0: continue
                    k = p+s[pos:]
                    yield k


    wset =  [x]+[ x for x in walk(x) ]
    return [ "".join( "%d"%x for x in p) for p in wset ]

if __name__ =="__main__":
    s =  equalcodegen(6)
    for x in s:
        print x

Result:

110000
110100
101100
011100
110010
101010
011010
100110
010110
001110
110001
101001
011001
100101
010101
001101
100011
010011
001011
000111
rth
  • 2,946
  • 1
  • 22
  • 27