Jump to content
Sign in to follow this  
AM

integer-to-binary-lengths*

Recommended Posts

perhaps there's a OM-solution... in this case it's to hard to find... (search-engine?)

otherwise...

 


(defun integer-to-binary-lengths* (alist)
  (loop for i in alist
    when (> (abs i) 1)
    append (append (list 1) (loop repeat (- (abs i) 1)
                      collect 0))
    else collect 1))
  

(integer-to-binary-lengths* '(2 2 2 1 1 4 4 4 4))
(integer-to-binary-lengths* '(6 4 8 5 2 1 10 2))

 

Share this post


Link to post
Share on other sites
(binary-to-decimal '(1 1 0))
=> 6
(decimal-to-binary '(9 3 6 1 10 11))
=> ((1 0 0 1) (1 1) (1 1 0) (1) (1 0 1 0) (1 0 1 1))

 

Share this post


Link to post
Share on other sites

it's not decimal-to-binary!!! another idea...

 

for example...

6 => 1 0 0 0 0 0 = a "1" and 5 times a "0")

Share this post


Link to post
Share on other sites

I've made this long time ago, bit different but may be also of interest, this function use a specs of number of "1" and "0" in input:

 

;;; GEN-BINARY_INTEGER
(defun gen-binary-integer (one-list zero-list &key (flatten t))
  (do-verbose
      ("gen-binary-integer")
  (if flatten
    (flatten
     (loop
       for i in one-list
       for o in (gen-trim (length one-list) zero-list) 
       collect 
       (append
        (gen-repeat i '(1))
        (gen-repeat o '(0))
     )))
    (loop
       for i in one-list
       for o in (gen-trim (length one-list) zero-list) 
       collect 
       (append
        (gen-repeat i '(1))
        (gen-repeat o '(0))
     )))))

#| USAGE
(gen-binary-integer '(3 4 3 2 4 2 5) '(1 1 3))
=> (1 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 0 0 1 1 1 1 1 0)

(gen-binary-integer '(3 4 3 2 4 2 5) '(1 1 3) :flatten nil)
=> ((1 1 1 0) (1 1 1 1 0) (1 1 1 0 0 0) (1 1 0) (1 1 1 1 0) (1 1 0 0 0) (1 1 1 1 1 0))

(setf intg '(3 2 4 5 3))
(setf zero-k '(1 2 1 3))

(gen-binary-integer intg zero-k)

=> '(1 1 1 0 1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0)

|#

S.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...