add n:binary-rep to examples

FossilOrigin-Name: a5f0d496e4377f08d6f93f76676b99384ff201645b3b5e56ba26a720cdccb0c9
This commit is contained in:
crc 2017-12-12 19:35:57 +00:00
parent 8162610f96
commit a452de62a6

View file

@ -69,3 +69,19 @@ Going the other way, back to a string, follows a similar process.
&String s:reverse ; &String s:reverse ;
}} }}
~~~ ~~~
The `n:to-string<with-base>` returns a representation of binary numbers, but
not the actual bitwise representation. The next word takes care of this.
~~~
:n:binary-rep (n-s)
[ s:empty buffer:set
dup n:negative? [ n:inc ] if
#32 [ dup n:negative?
[ dup n:odd? ] [ dup n:even? ] choose
[ $1 ] [ $0 ] choose buffer:add
#2 / ] times drop
buffer:start s:reverse
] buffer:preserve ;
~~~