example: n:binary-rep correction for negative numbers
FossilOrigin-Name: ede742661f927d96f9035edeb000140d3cb79be07cc1b51d8cf68d512e431dca
This commit is contained in:
parent
c58165d6ff
commit
4616a07abe
1 changed files with 11 additions and 9 deletions
|
@ -74,14 +74,16 @@ 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.
|
not the actual bitwise representation. The next word takes care of this.
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
:n:binary-rep (n-s)
|
{{
|
||||||
|
'Selector var
|
||||||
|
:bit (f-c) [ $0 ] [ $1 ] choose ;
|
||||||
|
---reveal---
|
||||||
|
:n:binary-rep (n-s)
|
||||||
|
dup n:negative? [ n:inc &n:odd? ] [ &n:even? ] choose !Selector
|
||||||
[ s:empty buffer:set
|
[ s:empty buffer:set
|
||||||
dup n:negative? [ n:inc ] if
|
#32 [ dup @Selector call bit buffer:add #2 / ] times drop
|
||||||
#32 [ dup n:negative?
|
|
||||||
[ dup n:odd? ] [ dup n:even? ] choose
|
|
||||||
[ $1 ] [ $0 ] choose buffer:add
|
|
||||||
#2 / ] times drop
|
|
||||||
buffer:start s:reverse
|
buffer:start s:reverse
|
||||||
] buffer:preserve ;
|
] buffer:preserve ;
|
||||||
|
}}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue