From a452de62a60a0c862d340f378cb830a779928edc Mon Sep 17 00:00:00 2001 From: crc Date: Tue, 12 Dec 2017 19:35:57 +0000 Subject: [PATCH] add n:binary-rep to examples FossilOrigin-Name: a5f0d496e4377f08d6f93f76676b99384ff201645b3b5e56ba26a720cdccb0c9 --- example/StringToNumberWithBase.forth | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/example/StringToNumberWithBase.forth b/example/StringToNumberWithBase.forth index a4d636f..6a09f4d 100644 --- a/example/StringToNumberWithBase.forth +++ b/example/StringToNumberWithBase.forth @@ -69,3 +69,19 @@ Going the other way, back to a string, follows a similar process. &String s:reverse ; }} ~~~ + +The `n:to-string` 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 ; +~~~ +