Is it possible to have an example of using negative bits in AutoCAD?
Note that when working with bitwise operations, you wouldn't necessarily consider the bits to be 'negative', but rather that, for a signed integer, the most-significant bit is the sign bit, determining whether the resulting decimal representation is positive or negative.
To offer an example in which such sign bit may come in play, in the past, I've used the following construct to exclude bits from a mask:
(= 4 (logand (cdr (assoc 70 (entget <dim>))) (~ 224)))
The above was to test whether a dimension was a radial dimension: DXF group
70 is usually always a bit-coded value, however, the DXF
70 group for dimensions is slightly odd in that values
0-6 are consecutive integers, and values
32 onwards are bit-codes.
Hence, although Radial dimensions have a DXF group
70 code of
4, you cannot simply use the following to test whether a dimension is Radial (as you might with other DXF group
70 codes):
(= 4 (logand 4 (cdr (assoc 70 (entget <dim>)))))
Since, this will also return
T for Ordinate dimensions (as
6=2+4).
Therefore, rather than performing an
AND operation with a specific bit (i.e. bit
4 in this case), the
logand expression in my example is masking bits
32-128, such that the bitwise
AND operation will match any other bits that are set.
As an example, consider an X-type Ordinate dimension that the user has repositioned, with DXF group
70 equal to
198 (6+64+128).
The binary representation of
198 as a 32-bit signed integer would be:
198 =
00000000 00000000 00000000 11000110
Performing a bitwise
NOT on
224 (=32+64+128) would be represented in binary as:
(~ 224) =
11111111 11111111 11111111 00011111
Now, performing a bitwise
AND operation against these two binary representations effectively masks the
32,
64 &
128 bits, leaving us with any remaining bits that are set:
00000000 00000000 00000000 11000110
AND 11111111 11111111 11111111 00011111
---------------------------------------
00000000 00000000 00000000 00000110 = 6