Applying what we have seen so far, we can create a function that converts any decimal number, positive or negative, into its binary representation.
The AutoCAD Customization manual proposes a solution, but is applicable only to positive integers:
;;;From the
AutoCAD Customization manual;;;converts a POSITIVE integer into a string
;;;in the given base:
(defun base (bas int / ret yyy zot)
(defun zot (i1 i2 / xxx)
(if (> (setq xxx (rem i2 i1)) 9)
(chr (+ 55 xxx))
(itoa xxx)
) ;_ end of if
) ;_
end of
defun
(setq ret (zot bas int)
yyy (/ int bas)
) ;_ end of setq
(while (>= yyy bas)
(setq ret (strcat (zot bas yyy) ret))
(setq yyy (/ yyy bas))
) ;_ end of while
(strcat (zot bas yyy) ret)
) ;_ end of defun
For positive integers it works correctly but not for negatives, as can be seen in the following examples:
Our BINARY function takes into account the operating system's word length and returns the correct sequence of zeros and ones even when dealing with negative values. The function expects to receive an integer, but takesinto account the possibility of a real number being entered, truncating it to the nearest smaller integer.
The utility functions BIT and POSBIT are defined, the latter used to store the value of the position of the bit being analyzed. The conversion itself is performed by the ConvertBinary function that receives as arguments the number to be converted and a predicate function to be applied according to the number being positive or negative.
This conversion function is called from the main BINARY function that analyzes whether the received number is positive or negative. If it is negative, it passes its logical NOT (~ numdec) and the predicate '= to the conversion function, instead of the number received and the predicate '/= that would be passed as arguments in case the number was positive.
;;;Binario.lsp
;;;The cycle will continue until LSH returns a negative value meaning
;;;that it has reached the end of the word (extreme left position).
;;;The binary value is returned in list format.;;;
;;; Bit auxiliary function: returns the decimal value of the bit
;;;in the specified position.;;;
(defun Bit (pos) (lsh 1 (1- pos)))
;;;
;;;
;;;Function used to update the position index.
;;;
(defun PosBit ()
(if (null position)
(setq position 1)
(setq position (1+ position))
) ;_ end of if
) ;_ end of defun
;;;
;;;
;;;Function for processing the decimal number.
;;;It receives as an argument the predicate to apply
;;;according to the numerical argument being positive or negative.;;;
(defun ConvertBinary (numdec predicate / position numbin)
(while (not (minusp (bit (1- (PosBit)))))
(setq numbin
(cons
(if
(apply
predicate
(list (logand (bit position) (fix numdec)) 0)
) ;_ end of apply
1
0
) ;_ end of if
numbin
) ;_ end of cons
) ;_ end of setq
) ;_ end of while
) ;_ end of defun
;;;
;;;Main function.
;;;It takes into account whether the number is positive or negative:;;;
(defun Binary (numdec /)
(if (minusp numdec)
(ConvertBinary (~ numdec) '=)
(
ConvertBinary
numdec '/=)
) ;_ end of if
) ;_ end of defun
Unlike the BASE function, a correct answer is obtained from both positive and negative integers:
If we want the result in string format instead of a list, it would be enough to map 'ITOA to the returned list to convert the integers into strings and then apply 'STRCAT to join the characters in a single string:
We will dedicate the next section to the conversion in the other direction, from binary to decimal.
** AutoCAD Versión 13. Manual dePersonalización. Autodesk, Inc. 1994. ISBN: 2-88447-327-0.pág. 249