Author Topic: Increment Challenge..  (Read 8298 times)

0 Members and 2 Guests are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #30 on: September 13, 2016, 12:54:39 PM »
 :wink:
Code: [Select]
_$ (ff "a" 25)
"a"
_$ (ff "a" 701)
"{"

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Challenge..
« Reply #31 on: September 13, 2016, 02:59:11 PM »
I've been reluctant to post in this thread because I don't have time to explain these excerpts from my library.

So I offer them "as is" for idea fodder, noting they were written for other purposes, and are only a small subset of a library of 60 related encoding/encryption/bit packing functions (there's other to/from flavors corresponding to other 2^x boundaries).

Anyway ...

Code: [Select]
(defun _Map32 ( )
    ;;  © Michael Puckett
    (setq **map32**
        (vl-string->list
            ;;  Note the omission of letters I,L,O,Q
            "0123456789ABCDEFGHJKMNPRSTUVWXYZ"
        )
    )
    (defun _Map32 ( ) **map32**)
    **map32**
)

Code: [Select]
(defun _ToMap ( i map )
    ;;  © Michael Puckett
    (   (lambda ( mask shift / result )
            (while (not (zerop i))
                (setq
                    result (cons (nth (logand mask i) map) result)
                    i      (lsh i shift)
                )
            )
            (vl-list->string result)
        )
        (1- (length map))
        (- (fix (/ (log (length map)) (log 2))))
    )
)

Code: [Select]
(defun _FromMap ( x map )
    ;;  © Michael Puckett
    (   (lambda ( x map shift result )
            (while x
                (setq result (+ result (length (member (car x) map))))
                (if (setq x (cdr x)) (setq result (lsh result shift)))
            )
            result
        )
        (mapcar 'chr (vl-string->list x))
        (mapcar 'chr (reverse (cdr map)))
        (fix (/ (log (length map)) (log 2)))
        0
    )
)

Code: [Select]
(defun _To32 ( i )
    ;;  © Michael Puckett
    (_ToMap i (_Map32))
)


Code: [Select]
(defun _From32 ( x )
    ;;  © Michael Puckett
    (_FromMap x (_Map32))
)

Code: [Select]
(defun _Inc32 ( x i )
    ;;  © Michael Puckett
    (_To32 (+ i (_From32 x)))
)

The range of values that can be represented by these functions corresponds to the range of integers that AutoCAD can represent:

    (expt 2 31)      >> -2147483648, (_To32 (expt 2 31))      >> "2000000"
    (1- (expt 2 31)) >> +2147483647, (_To32 (1- (expt 2 31))) >> "1ZZZZZZ"

To understand why (expt 2 31) yeilds a negative value the uninitiated may wish to read articles on "two's complement".

Code performs no hand handholding. See _Map32 function for acceptable data, notwithstanding AutoCAD's integer limits. Pass stupid data, crash, get stupid results or an std.
   
Examples:

    (_Inc32 "0" 1)      >> "1"
    (_Inc32 "0" -1)     >> "3ZZZZZZ"
    (_From32 "3ZZZZZZ") >> -1

    (_Inc32 "0" 31)     >> "Z"
    (_Inc32 "0" 32)     >> "10"

    (_Inc32 "Z" 1)      >> "10"
    (_Inc32 "ZZ" 1)     >> "100"

    (_Inc32 "Z" -1)     >> "Y"
    (_Inc32 "ZZ" -1)    >> "ZY"
   
    (_Inc32 "ABC" 1)    >> "ABD"
    (_Inc32 "ABC" 10)   >> "ABP"
    (_Inc32 "ABC" 100)  >> "AEG"
   
    (_Inc32 "ABC" -1)   >> "ABB"
    (_Inc32 "ABC" -10)  >> "AB2"
    (_Inc32 "ABC" -100) >> "A88"


FWIW / Have fun.

Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Increment Challenge..
« Reply #32 on: September 15, 2016, 09:34:18 AM »
...

FWIW / Have fun.

Cheers.
   
Thank you!

I've never used LOGAND function with negative numbers...
Code - Auto/Visual Lisp: [Select]
  1. (logand 31 -1)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Challenge..
« Reply #33 on: September 15, 2016, 10:17:46 AM »
(logand x -1) will always return x because -1 = 1111111111111111. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #34 on: September 15, 2016, 05:51:28 PM »
I've never used LOGAND function with negative numbers...
Code - Auto/Visual Lisp: [Select]
  1. (logand 31 -1)

FWIW, under such circumstances, I find the use of the bitwise NOT operator easier to understand which bits are being masked - for example, using:
Code - Auto/Visual Lisp: [Select]
  1. (logand 13 (~ 8))

It is clear that we are returning every bit except 8.

This is equivalently:
Code - Auto/Visual Lisp: [Select]
  1. (logand 13 -6)

Which isn't so readable with the two's-compliment mental arithmetic.

For the given example:
Code - Auto/Visual Lisp: [Select]
  1. (logand 13 (~ 0))

i.e. mask nothing, return all.



Thank you for sharing your endeavours MP.
« Last Edit: September 15, 2016, 05:55:36 PM by Lee Mac »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Challenge..
« Reply #35 on: September 15, 2016, 07:19:26 PM »
<nodding> Agree, the bitwise not is useful and often makes clearer one's intent. Thanks LM.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst