From: supergems on 9 Apr 2010 11:25 @Teuvo Yrjömäki thanks for your program. I rewrote it in ASCII format: \<< 0 \-> ACC \<< DUP2 B\->R .001 + LOG 2 LOG / IP SWAP B\->R .001 + LOG 2 LOG / IP MAX 0 SWAP 1 + FOR X DUP2 # 1b AND SWAP # 1b AND IF \=/ THEN ACC 1 + 'ACC' STO END SR SWAP SR NEXT DROP2 ACC \>> \>> 'HAMDIST' STO Ex. # 1101101b # 1110011b HAMDIST --> 4 @Dave Hayden and Michael J. Schülke thanks for your suggestion to use the BITS program of Jurjen NE Bos: \<< # 7777777777777777h OVER SR OVER AND DUP2 SR AND ROT OVER SR AND + + - DUP SR SR SR SR + # F0F0F0F0F0F0F0Fh AND # FFh DUP2 / * - \>> 'BITS' STO Ex. # 1101101b # 1110011b XOR --> # 11110b (here already we see the number of bits) and then with BITS we get # 100b which is # 4d supergems
From: Han on 9 Apr 2010 16:41 On Apr 9, 8:29 am, "Michael J. Schülke" <n...(a)mjschuelke.de> wrote: > Dave Hayden schrieb: > > > Given two binary numbers A and B, then Hamming distance is the number > > of "1" bits in (A AND B). > > A XOR B, surely? > > Regards, > Michael Yes, I believe XOR is the correct operation, not AND. Hamming distance is the number of positions in which there differences. The wiki page explains this clearly.
From: Virgil on 9 Apr 2010 17:05 In article <1f9d829d-487e-4078-b090-57bb451a216a(a)c36g2000yqm.googlegroups.com>, supergems <simone.cerica(a)gmail.com> wrote: > Hi, how can we calculate the Hamming distance ( > http://en.wikipedia.org/wiki/Hamming_distance > ) between two binary numbers? For example, the Hamming distance > between 1011101 and 1001001 is 2. > > supergems Short program to count the one bits in a binary number: With binary number on the stack execute \<< # 0b SWAP WHILE DUP # Ob > REPEAT DUP # 1b AND ROT + SWAP SR END DROP B\->R \<< Thus to find the Hamming distance between two binary numbers you could either XOR the numbers first and use the above program or use the slightly longer program: \<< XOR # 0b SWAP @ or #0 in whatever base you are using WHILE DUP # Ob > REPEAT DUP # 1b AND ROT + SWAP SR END DROP B\->R \<< Note that the "b" in the above presumes base two, but since the only binary numbers entered are # 0 and # 1, which are the same in all bases except for the base indicator, the base actually activated does not matter.
From: supergems on 10 Apr 2010 02:40 On 9 Apr, 23:05, Virgil <Vir...(a)home.esc> wrote: > In article > <1f9d829d-487e-4078-b090-57bb451a2...(a)c36g2000yqm.googlegroups.com>, > > supergems <simone.cer...(a)gmail.com> wrote: > > Hi, how can we calculate the Hamming distance ( > >http://en.wikipedia.org/wiki/Hamming_distance > > ) between two binary numbers? For example, the Hamming distance > > between 1011101 and 1001001 is 2. > > > supergems > > Short program to count the one bits in a binary number: > > With binary number on the stack execute > > \<< # 0b SWAP > WHILE DUP # Ob > > REPEAT DUP # 1b AND ROT + SWAP SR > END > DROP B\->R > \<< > > Thus to find the Hamming distance between two binary numbers you could > either XOR the numbers first and use the above program or use the > slightly longer program: > > \<< XOR # 0b SWAP @ or #0 in whatever base you are using > WHILE DUP # Ob > > REPEAT DUP # 1b AND ROT + SWAP SR > END > DROP B\->R > \<< > > Note that the "b" in the above presumes base two, but since the only > binary numbers entered are # 0 and # 1, which are the same in all bases > except for the base indicator, the base actually activated does not > matter. Thank you very much Virgil! :-)
From: Virgil on 10 Apr 2010 16:57 In article <e306448c-165a-419b-9aff-c4133a0747eb(a)b33g2000yqc.googlegroups.com>, supergems <simone.cerica(a)gmail.com> wrote: > On 9 Apr, 23:05, Virgil <Vir...(a)home.esc> wrote: > > In article > > <1f9d829d-487e-4078-b090-57bb451a2...(a)c36g2000yqm.googlegroups.com>, > > > > �supergems <simone.cer...(a)gmail.com> wrote: > > > Hi, how can we calculate the Hamming distance ( > > >http://en.wikipedia.org/wiki/Hamming_distance > > > ) between two binary numbers? For example, the Hamming distance > > > between 1011101 and 1001001 is 2. > > > > > supergems > > > > Short program to count the one bits in a binary number: > > > > With binary number on the stack execute > > > > \<< # 0b �SWAP � � � � > > � � WHILE DUP # Ob > > > � � REPEAT DUP # 1b AND ROT + SWAP SR > > � � END > > � � DROP B\->R > > \<< > > > > Thus to find the Hamming distance between two binary numbers you could > > either XOR the numbers first and use the above program or use the > > slightly longer program: > > > > \<< XOR # 0b �SWAP � � � � �@ or #0 in whatever base you are using > > � � WHILE DUP # Ob > > > � � REPEAT DUP # 1b AND ROT + SWAP SR > > � � END > > � � DROP B\->R > > \<< > > > > Note that the "b" in the above presumes base two, but since the only > > binary numbers entered are # 0 and # 1, which are the same in all bases > > except for the base indicator, the base actually activated does not > > matter. > > Thank you very much Virgil! :-) Glad to be of use.
First
|
Prev
|
Pages: 1 2 Prev: Nelson & Carter Next: John Wilson's Differential Geometry UserRPL Program |