From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.emacs.gnus.user/16505 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.gnus.user Subject: Re: Strange results Date: Sat, 07 Sep 2013 14:32:02 +0200 Organization: Informatimago Message-ID: <87r4d0dd8d.fsf@informatimago.com> References: <73ac625b-72a4-4719-9ca4-e4d5da9e8c0a@googlegroups.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1378557302 17487 80.91.229.3 (7 Sep 2013 12:35:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 7 Sep 2013 12:35:02 +0000 (UTC) To: info-gnus-english@gnu.org Original-X-From: info-gnus-english-bounces+gegu-info-gnus-english=m.gmane.org@gnu.org Sat Sep 07 14:35:07 2013 Return-path: Envelope-to: gegu-info-gnus-english@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VIHjG-0007VO-Mp for gegu-info-gnus-english@m.gmane.org; Sat, 07 Sep 2013 14:35:06 +0200 Original-Received: from localhost ([::1]:42061 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VIHjG-0005lP-3E for gegu-info-gnus-english@m.gmane.org; Sat, 07 Sep 2013 08:35:06 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.gnus Original-Lines: 68 Original-X-Trace: individual.net ET19UHpsLJZygUgi0XTUTwUQMq8cMuZ5SJiAH1PhZBKGXPyLF6 Cancel-Lock: sha1:NmI5MWJhYWFlYWU5N2NmODMxMWUyODgyMTI5ZmY1NWQ2MThmNzY3Yw== sha1:OOWWSiNYTZpyYC4wF6BFwOTEyig= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.gnus:87632 X-BeenThere: info-gnus-english@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Announcements and discussions for GNUS, the GNU Emacs Usenet newsreader \(in English\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: info-gnus-english-bounces+gegu-info-gnus-english=m.gmane.org@gnu.org Original-Sender: info-gnus-english-bounces+gegu-info-gnus-english=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.gnus.user:16505 Archived-At: Dwaddle 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! > > (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/