* Strange results
@ 2013-09-07 6:54 Dwaddle
2013-09-07 12:32 ` Pascal J. Bourguignon
0 siblings, 1 reply; 2+ messages in thread
From: Dwaddle @ 2013-09-07 6:54 UTC (permalink / raw)
To: info-gnus-english
Hi
i've been dabbling around in elisp for a while. Made this function
The issue is the byte var which 'remembers' the result from a previous calculation.
(integer-bin 5) [0 0 0 0 0 1 0 1]
(integer-bin 3) [0 0 0 0 0 1 1 1]
To beat the critics I know it isn't very clean programmed and a loop would make it al lot shorter
<code>
(defun integer-bin (dec)
"Convert integer to binairy"
(setq 8bit [128 64 32 16 8 4 2 1])
(setq byte [0 0 0 0 0 0 0 0])
(if (= 1 (/ dec (aref 8bit 0))) (when
(aset byte 0 1)
(setq dec (- dec 128))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 1))) (when
(aset byte 1 1)
(setq dec (- dec 64)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 2))) (when
(aset byte 2 1)
(setq dec (- dec 32)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 3))) (when
(aset byte 3 1)
(setq dec (- dec 16)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 4))) (when
(aset byte 4 1)
(setq dec (- dec 8)))))
(if (< 0 dec)
(if (= 1 (/ d<ec (aref 8bit 5))) (when
(aset byte 5 1)
(setq dec (- dec 4)))))
(if (< 0 dec)
(if (= 1 (/ dec (aref 8bit 6))) (when
(aset byte 6 1)
(setq dec (- dec 2)))))
(if (< 0 dec)
(if (= 1 1) (when (aset byte 7 1)
(setq dec (- dec 1)))))
(print byte)
)
</code>
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Strange results
2013-09-07 6:54 Strange results Dwaddle
@ 2013-09-07 12:32 ` Pascal J. Bourguignon
0 siblings, 0 replies; 2+ messages in thread
From: Pascal J. Bourguignon @ 2013-09-07 12:32 UTC (permalink / raw)
To: info-gnus-english
Dwaddle <rmborchers@gmail.com> writes:
> Hi
>
> i've been dabbling around in elisp for a while. Made this function
>
> The issue is the byte var which 'remembers' the result from a previous calculation.
>
> (integer-bin 5) [0 0 0 0 0 1 0 1]
> (integer-bin 3) [0 0 0 0 0 1 1 1]
>
> To beat the critics I know it isn't very clean programmed and a loop would make it al lot shorter
Yes, use a loop!
> <code>
> (defun integer-bin (dec)
> "Convert integer to binairy"
> (setq 8bit [128 64 32 16 8 4 2 1])
> (setq byte [0 0 0 0 0 0 0 0])
Instead of setq, use let to introduce local variables.
[0 0 0 0 0 0 0 0] is a literal "constant" vector.
(vector 0 0 0 0 0 0 0 0) produces a new mutable vector.
(make-vector 8 0) produces a new mutable vector with each slot
initialized to the same object..
(require 'cl)
(defun* integer-bin (integer &optional (word-size 8))
"Convert `integer' to binary"
(let ((word (make-vector word-size 0))) ; a new mutable vector
(loop
for bit-index from (1- word-size) downto 0
for current = integer then (truncate current 2) ; or (ash current -1)
for bit = (mod current 2) ; or (logand current 1)
do (setf (aref word bit-index) bit))
word))
(integer-bin 42) ; --> [0 0 1 0 1 0 1 0]
(integer-bin 42 16) ; --> [0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0]
(defun integer-to-base (integer base word-size)
"Convert `integer' to base `base', over `word-size` digits."
(let ((word (make-vector word-size 0))) ; a new mutable vector
(loop
for digit-index from (1- word-size) downto 0
for current = integer then (truncate current base)
for digit = (mod current base)
do (setf (aref word digit-index) digit))
word))
(integer-to-base 42 2 8) ; --> [0 0 1 0 1 0 1 0]
(integer-to-base 42 8 8) ; --> [0 0 0 0 0 0 5 2]
(integer-to-base 42 24 4) ; --> [0 0 1 18]
--
__Pascal Bourguignon__
http://www.informatimago.com/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-09-07 12:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-07 6:54 Strange results Dwaddle
2013-09-07 12:32 ` Pascal J. Bourguignon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).