[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-dev] [commit] gray code primitive



I've added a grayCode primitive for Integer:


f = { |n=3| (0..(2**n-1).asInteger).do { |x| x.grayCode.asBinaryDigits(n).postln } };

f.(4)

[ 0, 0, 0, 0 ]
[ 0, 0, 0, 1 ]
[ 0, 0, 1, 1 ]
[ 0, 0, 1, 0 ]
[ 0, 1, 1, 0 ]
[ 0, 1, 1, 1 ]
[ 0, 1, 0, 1 ]
[ 0, 1, 0, 0 ]
[ 1, 1, 0, 0 ]
[ 1, 1, 0, 1 ]
[ 1, 1, 1, 1 ]
[ 1, 1, 1, 0 ]
[ 1, 0, 1, 0 ]
[ 1, 0, 1, 1 ]
[ 1, 0, 0, 1 ]
[ 1, 0, 0, 0 ]

_______________________________________________________________


there was a bug in GRAYCODE:

diff -p -b -B -r1.11 clz.h
*** clz.h       28 May 2004 20:37:20 -0000      1.11
--- clz.h       1 Mar 2006 15:30:04 -0000
*************** inline bool ISPOWEROFTWO(int32 x)
*** 121,127 ****
  // input a series of counting integers, outputs a series of gray codes .
  inline int32 GRAYCODE(int32 x)
  {
!       return x & (x>>1);
  }

  // find least significant bit
--- 121,127 ----
  // input a series of counting integers, outputs a series of gray codes .
  inline int32 GRAYCODE(int32 x)
  {
!       return x ^ (x>>1);
  }




--





.