Jump to content

Featured Replies

Posted

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))

 

(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))

 

  • Author

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

 

for example...

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

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.

Create an account or sign in to comment


Copyright © 2014-2025 Opusmodus™ Ltd. All rights reserved.
Product features, specifications, system requirements and availability are subject to change without notice.
Opusmodus, the Opusmodus logo, and other Opusmodus trademarks are either registered trademarks or trademarks of Opusmodus Ltd.
All other trademarks contained herein are the property of their respective owners.

Powered by Invision Community

Important Information

Terms of Use Privacy Policy