LOGAND?

Discussion in 'AutoCAD' started by rdi, Jan 8, 2004.

  1. rdi

    rdi Guest

    Can someone offer some kind of explanation of what the "Log" functions do
    and how to use them?

    I've always wondered what LOGAND and his friends did and the "description"
    of "Performs a 'logical and'" provided by the help and manuals does not
    help.

    TIA
     
    rdi, Jan 8, 2004
    #1
  2. rdi

    Tom Smith Guest

    Search the term "Boolean" or the phrase "Boolean algebra" on the web and
    you'll find abundant resources. If you don't understand the definition in
    Help, you probably need some more background knowledge before it can be
    explained to you. This is a little like asking "how do computers work?"
     
    Tom Smith, Jan 8, 2004
    #2
  3. rdi

    Kenny Guest

    Kenny, Jan 8, 2004
    #3
  4. rdi

    Devin Guest

    You've probably noticed certain values in autocad system variables, like the
    running osnap "osmode". They actually use bits to describe conditions that
    occure. They don't look like bits, for they're actually converted to
    integers, but they represent binary values. You may have noticed that the
    values for osmode are...

    0 = none
    1 = endpoint
    2 = midpoint
    4 = center
    8 = node
    16 = quadrant
    32 = intersection
    64 = insertion
    128 = perpendicular
    256 = tangent
    and so on...

    The thing to notice is that the value none is 0 then the next value is 1 and
    then the next value is double of that then double of that then double of
    that and so on.

    First let's examine the binary number system, since that's what log
    functions work with (note that even though you supply integers, the
    functions actually convert the values to binary to perform their
    operations):

    binary value || decimal value
    =======================
    0000 || 0
    0001 || 1
    0010 || 2
    0011 || 3
    0100 || 4
    0101 || 5
    0110 || 6
    0111 || 7
    1000 || 8

    Now you can see what binary looks like from 0 - 8 in decimal. Of course
    binary is a base 2 number system (either 1's or 0's). Notice what happens
    when you shift the bit from beginning to the end of the binary number...

    binary value || decimal value
    =======================
    0001 || 1
    0010 || 2
    0100 || 4
    1000 || 8

    With each succession in the binary place the decimal value doubles. The
    interesting thing is that if you combine any of these decimal numbers you
    will always create a unique binary value since the bits that are 1 appear at
    different places in the binary number.

    Notice:
    decimal action total binary
    ===========================
    0 + 0 + 0 + 1 = 1 = 0001
    0 + 0 + 2 + 1 = 3 = 0011
    0 + 4 + 2 + 1 = 7 = 0111
    8 + 4 + 2 + 1 = 15 = 1111

    When I add 1 the first binary bit is set to 1, now if I add 2 to that binary
    number the second bit is set. And so on, as I add four to the value the 3rd
    bit is set and lastly, when I add 8 then fourth bit is set.

    Conversley, I can subtract bits from the binary value and unset bit
    positions:

    Notice:

    decimal action total binary
    ============================
    (8 + 4 + 2 + 1) - 0 = 15 = 1111
    (8 + 4 + 2 + 1) - 1 = 14 = 1110
    (8 + 4 + 2 + 1) - 2 = 13 = 1101
    (8 + 4 + 2 + 1) - 4 = 11 = 1011
    (8 + 4 + 2 + 1) - 8 = 7 = 0111

    So this is how you set bits. The next bit is always double of the previous
    bit, likewise the previous bit is always half of the next bit.

    So how is this usefull in programming? Well, as you can see you can set a
    series of bits within a binary number. Then later you can check to see
    which bits are set and which bits are not set. These act like switches in
    your program. 1 = switch on, 0 = switch off:

    Imagine the following as a switch where 1 is on and 0 is off:

    The Switch:
    -------------------------------
    | 0 | 0 | 0 | 0 | All switch buttons are off so
    the value is 0
    -------------------------------

    -------------------------------
    | 0 | 0 | 0 | 1 | The first button is set so the
    value is 1
    -------------------------------

    -------------------------------
    | 0 | 0 | 1 | 1 | The first and second buttons
    are set so the value is 1 + 2 = 3
    -------------------------------

    -------------------------------
    | 0 | 1 | 1 | 1 | The first, second and third
    buttons are set so the value is 1 + 2 + 4 = 7
    -------------------------------

    O.K. so back to your question, what are the log functions for? They perform
    two vary critical functions for working with binary bit sets. The two
    functions are AND and OR. Since we first worked with adding bits together,
    lets look at the OR function, LOGIOR which stands for logical OR. If I have
    two switches, each with 4 buttons and I want to check to see which buttons
    are on for both switches then I use the logior function:

    The logical OR performs an OR where 1 is True and 0 is False. Let's keep it
    simple and use two bits only to compare. The OR function evaluates the OR
    for the first bit of the first value against the first bit of the second
    value. Then the second bit of the first value against the second bit of the
    second value and so on.

    Examine the following:

    binary decimal
    ------------------------------------------
    value1 0000 = 0
    value2 0001 = 1
    ------------------------------------------
    OR result 0001 = 1


    binary decimal
    ------------------------------------------
    value1 0001 = 1
    value2 0001 = 1
    ------------------------------------------
    OR result 0001 = 1


    binary decimal
    ------------------------------------------
    value1 0010 = 2
    value2 0001 = 1
    ------------------------------------------
    OR result 0011 = 3


    binary decimal
    ------------------------------------------
    value1 0011 = 3
    value2 0001 = 1
    ------------------------------------------
    OR result 0011 = 3

    The OR is useful for adding a bit to an already created bit set. It will
    just simply, given two switches (bit sets), determine which buttons are on
    for both switches and return a value of all the buttons that were on for
    both.

    Now for the final part, the LOGAND function. It is useful for checking
    wether a button is on in a switch (bit set). It campares a bit you supply
    (or set of bits) against a bit set to see wether the bit is in the bit set.
    Notice, if it doesn't find the bit in the bit set it returns zero. You can
    then check the result of the logand using the (not (zerop (logand <bit>
    <bitset>))) function to see wether the bit is set.

    Examine the following:

    binary decimal
    ------------------------------------------
    value1 0000 = 0
    value2 0001 = 1
    ------------------------------------------
    AND result 0000 = 0


    binary decimal
    ------------------------------------------
    value1 0001 = 1
    value2 0001 = 1
    ------------------------------------------
    AND result 0001 = 1


    binary decimal
    ------------------------------------------
    value1 0010 = 2
    value2 0001 = 1
    ------------------------------------------
    AND result 0000 = 0


    binary decimal
    ------------------------------------------
    value1 0011 = 3
    value2 0001 = 1
    ------------------------------------------
    AND result 0001 = 1


    binary decimal
    ------------------------------------------
    value1 0111 = 7
    value2 0011 = 3
    ------------------------------------------
    AND result 0011 = 3


    binary decimal
    ------------------------------------------
    value1 1111 = 15
    value2 0010 = 2
    ------------------------------------------
    AND result 0010 = 2

    And that's it. I hope this helps!

    Devin
     
    Devin, Jan 8, 2004
    #4
  5. rdi

    Jeff Mishler Guest

    Jeff Mishler, Jan 8, 2004
    #5
  6. Pretty good explanation! I already understood it but I just wanted to say
    "Hats off to you!" for taking the time to be that thorough.
     
    Eric Schneider, Jan 8, 2004
    #6
  7. rdi

    Devin Guest

    Thanks Eric.
     
    Devin, Jan 8, 2004
    #7
  8. rdi

    rdi Guest

    Thanks Devin.

    That was very thorough and very well written. I already understood the
    concept of how bits work---but I never learned how to use the log function
    (or what they were for).

    When I posted my question, I was figuring that someone would point me to a
    tutorial somewhere. But instead, you wrote one for me. I appreciate your
    taking the time to do so!
     
    rdi, Jan 8, 2004
    #8
  9. rdi

    Devin Guest

    Your welcome.
     
    Devin, Jan 9, 2004
    #9
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.
Similar Threads
Loading...