Working with BitFlags

Discussion in 'AutoCAD' started by Shane-W, Dec 3, 2004.

  1. Shane-W

    Shane-W Guest

    I would like to use bit flags in some my code but I have no idea how to figure out how to determine what bit flags are being used.

    Ex.
    2 = A
    4 = B
    8 = C
    16 = D
    32 = E
    -------------------
    50 = A D E
    In code whats the easiest way to know that 50 = 2, 16, & 32.

    Any one out there now of a good place to get help/info on this topic?

    Thanks,
    Shane
     
    Shane-W, Dec 3, 2004
    #1
  2. Shane-W

    John Uhden Guest

    Maybe (logand) might help...

    (defun interpret (code / rosetta answer)
    (setq rosetta '((1 "-")(2 "A")(4 "B")(8 "C")(16 "D")(32 "E")))
    (setq answer "")
    (foreach stone rosetta
    (if (= (logand code (car stone))(car stone))
    (setq answer (strcat answer (cadr stone)))
    )
    )
    answer
    )

    Command: (interpret 50)
    "ADE"

    Command: (interpret 51)
    "-ADE"

    Of course you can control the order of characters only by changing the rosetta
    list.




    out how to determine what bit flags are being used.
     
    John Uhden, Dec 4, 2004
    #2
  3. Shane-W

    Jürg Menzi Guest

    Hi Shane-W

    This will do the job:
    Code:
    ;
    ; -- Function MeGetBitwise
    ; Returns a list of all logical bitwise AND's of an integer.
    ; Copyright:
    ;   ©1998 MENZI ENGINEERING GmbH, Switzerland
    ; Arguments [Typ]:
    ;   Val = Value [INT]
    ; Return [Typ]:
    ;   > List of logical bitwise AND's [LIST]
    ; Notes:
    ;   None
    ;
    (defun MeGetBitwise (Val / CurBit RetVal)
    (setq CurBit 1)
    (while (<= CurBit Val)
    (if (= (logand Val CurBit) CurBit)
    (setq RetVal (cons CurBit RetVal))
    )
    (setq CurBit (* CurBit 2))
    )
    (reverse RetVal)
    )
    
    _$ (MeGetBitwise 50)
    (2 16 32)

    Cheers
     
    Jürg Menzi, Dec 4, 2004
    #3
  4. Shane-W

    Don I Guest

    Shane,

    A while back asked for some help on LOGAND (the function you'll need).
    Devin posted an EXCELLENT tutorial on using bits. I also posted a follow up
    question and you might be interested in those responses as well.

    Watch out for the word wrap and note that the asterisk at the end IS part of
    the URL.

    The original question:
    http://groups.google.com/groups?hl=...=logand&safe=images&as_uauthors=rdi&lr=&hl=en

    The Follow Up Question:
    http://groups.google.com/groups?hl=...meta=group%3Dautodesk.autocad.customization.*

    Also a google search on LOGAND in the customization NG:
    http://groups.google.com/groups?hl=en&lr=&q=logand&meta=group=autodesk.autocad.customization.*

    HTH
     
    Don I, Dec 5, 2004
    #4
  5. Shane-W

    Shane-W Guest

    Thanks for the ton of info.
    Shane
     
    Shane-W, Dec 6, 2004
    #5
  6. Shane-W

    Don I Guest

    Glad to help.
     
    Don I, Dec 6, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.