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