mirror of
https://git.sr.ht/~crc_/retroforth
synced 2024-11-16 19:48:56 +01:00
add n:binary-rep to examples
FossilOrigin-Name: a5f0d496e4377f08d6f93f76676b99384ff201645b3b5e56ba26a720cdccb0c9
This commit is contained in:
parent
8162610f96
commit
a452de62a6
1 changed files with 16 additions and 0 deletions
|
@ -69,3 +69,19 @@ Going the other way, back to a string, follows a similar process.
|
|||
&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 ;
|
||||
~~~
|
||||
|
||||
|
|
Loading…
Reference in a new issue