add=: 4 : 0 smoutput '' CARRY=: 0 a=.|.2^i.#x b=.+/a*x c=.+/a*y smoutput 'x + y = ',(": b),' + ',(": c),' = ',": b+c smoutput '' d=.|.CARRY,~> (|.x) addbin &.> |.y e=.|.2^i.#d smoutput ' ',":a smoutput 'x is : ',":x smoutput 'y is : ',":y smoutput '' smoutput ' ',":e smoutput 'x + y is : ',(": d), ' = ', ": +/ d * e smoutput '' )
addbin=: 4 : 0 a=.x+y + CARRY select. a case. 0 do. CARRY=: 0 0 case. 1 do. CARRY=: 0 1 case. 2 do. CARRY=: 1 0 case. 3 do. CARRY=: 1 1 end. ) 0 1 1 add 1 1 1 x + y = 3 + 7 = 10 4 2 1 x is : 0 1 1 y is : 1 1 1 8 4 2 1 x + y is : 1 0 1 0 = 10 2011/12/27 Björn Helgason <gos...@gmail.com> > f=: 3 : 0 > select. y > case. 0 do. 0 0 0 > case. 1 do. 0 0 1 > case. 2 do. 0 1 0 > case. 3 do. 0 1 1 > case. 4 do. 1 0 0 > case. 5 do. 1 0 1 > case. 6 do. 1 1 0 > end. > ) > > br=: 3 : 0 > > f &.> y > ) > > br 0 > 0 0 0 > br 2 > 0 1 0 > > > br i.5 > 0 0 0 > 0 0 1 > 0 1 0 > 0 1 1 > 1 0 0 > > 2011/12/27 Kip Murray <k...@math.uh.edu> > >> Can you write a verb br which returns the binary representation of y >> without using #: or #. ? >> >> br 0 >> 0 >> br 2 >> 1 0 >> br i. 5 >> 0 0 0 >> 0 0 1 >> 0 1 0 >> 0 1 1 >> 1 0 0 >> br _13 >> |domain error: br >> | br _13 >> >> An inverse I like is >> >> bv =: (2 p.~ |.)"1 :. br >> >> bv br i. 5 >> 0 1 2 3 4 >> ---------------------------------------------------------------------- >> For information about J forums see http://www.jsoftware.com/forums.htm >> > > > > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm