LOGAND ?#2

RDI

Usenet Archive
Joined
Jul 25, 2003
Messages
77
Reaction score
0
I recently asked for an explanation on Logand. I recieved an EXCELLENT response to that but now I do have another question. This a very detailed explanation of how binary works. The problem is that I already understood how binary works.

I think I understand the logand function now after studying the example provided and reading a few other google threads on the subject. Now I'd just like confirmation so that I can be sure.

Assuming that I DO understand it, in a practical setting I would pass LOGAND 2 seperate integers. The first would be the "Current" setting. And the second would be what the maximum possible setting would be if the bit I'm interested in is OFF (this is the "Mask").

i.e. if I'm trying to figure out if a layer is frozen, I'm only intersted in bit #1 of group #70.

the bits are 1-If set, layer is frozen; 2-If set, layer is frozen by default in all new viewports; 4-if set, layer is locked.

if all bits are set (cdr (assoc 70 (tblsearch "Layer1"))) wil return 7. If all bits EXCEPT bit #1 are set then it will return 6.

So if (logand (cdr (assoc 70 (tblsearch "Layer1"))) 6) returns 0 then Bit #1 is OFF. If it returns 1 then the bit is ON.

And if I were trying to figure out if the layer was locked I would use (logand(cdr (assoc 70 (tblsearch "Layer1"))) 3). If it returns 4 then the layer is locked. If it returns 0 then the layer us NOT locked.

Is this correct?

Thanks.
 
Well after some more tinkering and testing. I see that I DID misunderstand. Instead of what I termed the "Mask" pass it the bit I'm looking for. Per my previous example:

So if (logand (cdr (assoc 70 (tblsearch "Layer1"))) 1) returns 0 then Bit #1 is OFF. If it returns 1 then the bit is ON. So if (logand (cdr (assoc 70 (tblsearch "Layer1"))) 4) returns 0 then Bit #4 is OFF. If it returns 4 then the bit is ON.
 
Nope.

If you are interested in the frozen status of a layer you only need to check that bit, not "the maximum possible setting".

e.g.: (logand (cdr (assoc 70 LayerCodes)) 1)

or, "in other words", (say the layer *is* frozen):
00000001 = (cdr (assoc 70 LayerCodes)) = 1
00000001 = bit 0 is on = 1
00000001 = bit 0 was set = 1, so layer is frozen

or, if the layer is thawed:
00000000 = (cdr (assoc 70 LayerCodes)) = 0
00000001 = bit 0 is on = 1
00000000 = bit 0 was not set = 0, so layer is thawed

Is the layer locked?
(logand (cdr (assoc 70 LayerCodes)) 4)

or, "in other words", (say the layer *is* locked):
00000100 = (cdr (assoc 70 LayerCodes)) = 4
00000100 = bit 2 is on = 4
00000100 = bit 2 was set = 4, so layer is locked

or, if the layer is not locked:
00000000 = (cdr (assoc 70 LayerCodes)) = 0
00000100 = bit 2 is on = 4
00000000 = bit 2 was not set = 0, so layer is unlocked

Now let's have some fun! What if the layer is frozen & locked, but we are only curious about the frozen status?

e.g.: (logand (cdr (assoc 70 LayerCodes)) 1)

or, "in other words", (say the layer *is* frozen & locked):
00000101 = (cdr (assoc 70 LayerCodes)) = 5
00000001 = bit 0 is on = 1
00000001 = bit 0 was set = 1, so layer is frozen

or, if the layer is thawed & locked:
00000100 = (cdr (assoc 70 LayerCodes)) = 4
00000001 = bit 0 is on = 1
00000000 = bit 0 was not set = 0, so layer is thawed

--
R. Robert Bell, MCSE
www.AcadX.com

| I recently asked for an explanation on Logand. I recieved an EXCELLENT | response to that but now I do have another question. This a very detailed | explanation of how binary works. The problem is that I already understood | how binary works. | | I think I understand the logand function now after studying the example | provided and reading a few other google threads on the subject. Now I'd | just like confirmation so that I can be sure. | | Assuming that I DO understand it, in a practical setting I would pass LOGAND | 2 seperate integers. The first would be the "Current" setting. And the | second would be what the maximum possible setting would be if the bit I'm | interested in is OFF (this is the "Mask"). | | i.e. if I'm trying to figure out if a layer is frozen, I'm only intersted in | bit #1 of group #70. | | the bits are 1-If set, layer is frozen; 2-If set, layer is frozen by default | in all new viewports; 4-if set, layer is locked. | | if all bits are set (cdr (assoc 70 (tblsearch "Layer1"))) wil return 7. If | all bits EXCEPT bit #1 are set then it will return 6. | | So if (logand (cdr (assoc 70 (tblsearch "Layer1"))) 6) returns 0 then Bit #1 | is OFF. If it returns 1 then the bit is ON. | | And if I were trying to figure out if the layer was locked I would use | (logand(cdr (assoc 70 (tblsearch "Layer1"))) 3). If it returns 4 then the | layer is locked. If it returns 0 then the layer us NOT locked. | | Is this correct? | | Thanks. | |
 
(logand(cdr(assoc 70 (tblsearch "Layer1"))) 1) will always return the same as (cdr(assoc 70 (tblsearch "Layer1")))

Your second example will always return 0.

These are not good applications for logand.

Do you have a specific application in mind? Logand
is best for manipulating system variables that are bit
sums.
 
Forget what I just said.

70 format is bit masked and your examples will work fine and you understand correctly. Sheesh, Monday afternoon I should be awake already.
 
Normally, I just test for the bit you wish to know about. -David

(defun lay_status (l)
(if (and (= (type l) 'STR)
(snvalid l)
(setq laylst (tblsearch "LAYER" l)))
(progn
(princ (strcat "\nLayer " l " Status"))
(setq layflg (cdr (assoc 70 laylst)))
(if (minusp (cdr (assoc 62 laylst)))
(princ "\nOff")
(princ "\nOn"))
(if (= (logand layflg 1) 1)
(princ "\nFrozen")
(princ "\nThawed"))
(if (= (logand layflg 2) 2)
(princ "\nNewfrozen"))
(if (= (logand layflg 4) 4)
(princ "\nLocked"))
(if (= (logand layflg 16) 16)
(princ "\nDepend on a Xref"))
(if (= (logand layflg 32) 32)
(princ "\nDepend on a Xref has been successfully resolved")) (if (= (logand layflg 64) 64) (princ "\nLayer is referenced..."))) (progn (princ "\nLayer ") (prin1 l) (princ " is either not a valid symbol or does not exist")))
(princ))
 
Thanks all. Logand & LogIOR will REALLY open up new doors for me. In the past I've always tested each possible value manually because I didn't know any other way. For the most part, I'm all self-taught. The parts that I didn't learn by just reading (examples that I've found online here and in other locations as well as in a somewhat useful--at the time--AutoLisp book I used to have), I learned by asking questions here.

For example (if I'm only interested in whether or not it's frozen):

(if (or
(= (cdr(assoc 70 layer)) 1); frozen only
((= (cdr(assoc 70 layer)) 3); frozen AND frozen in new vp's (= (cdr(assoc 70 layer)) 7); frozen AND frozen in new vp's AND locked );end or then do something....
)

Will become:
(if (= (logand (cdr (assoc 70 layer)) 1) 1)
then do something....
)
 

Latest threads

Back
Top