Author Topic: bits...  (Read 1366 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 988
  • Marco
Re: bits...
« Reply #15 on: February 05, 2019, 09:11:44 AM »
Is it possible to have an example of using negative bits in AutoCAD?   :embarrassed:

Lee Mac

  • Seagull
  • Posts: 12241
  • London, England
Re: bits...
« Reply #16 on: February 05, 2019, 12:24:24 PM »
Is it possible to have an example of using negative bits in AutoCAD?   :embarrassed:

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:
Code: [Select]
(= 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):
Code: [Select]
(= 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:
Code: [Select]
198 =
00000000 00000000 00000000 11000110

Performing a bitwise NOT on 224 (=32+64+128) would be represented in binary as:
Code: [Select]
(~ 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:
Code: [Select]
    00000000 00000000 00000000 11000110
AND 11111111 11111111 11111111 00011111
---------------------------------------
    00000000 00000000 00000000 00000110 = 6


« Last Edit: February 05, 2019, 12:27:27 PM by Lee Mac »

gile

  • Water Moccasin
  • Posts: 2222
  • Marseille, France
Re: bits...
« Reply #17 on: February 05, 2019, 12:25:34 PM »
You can use negative bits when you want to remove some bits to a flag.

For example, to deactivate MIDpoint (2) and CENter (4) to the OSMODE sysvar without checking if these modes are activated or not, you can do:
Code - Auto/Visual Lisp: [Select]
  1. (setvar 'osmode (logand (~ 6) (getvar 'osmode)))
which is the same as:
Code - Auto/Visual Lisp: [Select]
  1. (setvar 'osmode (logand -7 (getvar 'osmode)))

Lee was faster (and his English is more fluent).
« Last Edit: February 05, 2019, 12:30:36 PM by gile »
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12241
  • London, England
Re: bits...
« Reply #18 on: February 05, 2019, 12:49:38 PM »
Lee was faster (and his English is more fluent).

You should see my French :oops:  :wink:

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 988
  • Marco
Re: bits...
« Reply #19 on: February 05, 2019, 01:26:46 PM »
Grazie Lee and Gile, molto interessante!  :) :) :)
Thanks for your time.

gile

  • Water Moccasin
  • Posts: 2222
  • Marseille, France
Re: bits...
« Reply #20 on: February 05, 2019, 01:56:04 PM »
Using example

Code - Auto/Visual Lisp: [Select]
  1. (defun turnBitsOn (bits flag) (logior bits flag))
  2.  
  3. (defun turnBitsOff (bits flag) (logand (~ bits) flag))
  4.  
  5. (defun toggleBits (bits flag) (Boole 6 bits flag))

Code: [Select]
_$ (setq flag 5) ; -> 4 | 1
5
_$ (turnBitsOn 3 flag) ; -> 4 | 2 | 1
7
_$ (turnBitsOff 3 flag) ; -> 4
4
_$ (toggleBits 3 flag) ; -> 4 | 2
6
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12241
  • London, England
Re: bits...
« Reply #21 on: February 05, 2019, 02:07:07 PM »
Good examples gile.  :-)

The same can be accomplished using the versatility of the boole function:

Code - Auto/Visual Lisp: [Select]
  1. (defun bitson  ( b v ) (boole 7 b v))
  2. (defun bitsoff ( b v ) (boole 4 b v))
  3. (defun bitstog ( b v ) (boole 6 b v))
  4.  
Code - Auto/Visual Lisp: [Select]
  1. 5 = 4+1
  2. 3 = 2+1
  3. _$ (bitson  3 5) => 7 = 4+2+1
  4. _$ (bitsoff 3 5) => 4
  5. _$ (bitstog 3 5) => 6 = 4+2
« Last Edit: February 05, 2019, 02:30:18 PM by Lee Mac »

roy_043

  • Water Moccasin
  • Posts: 1700
  • BricsCAD 18
Re: bits...
« Reply #22 on: February 05, 2019, 03:24:02 PM »
@Marc'Antonio Alessi
AFAIK 'negative' bits are not used in entity lists. But bit-coded values do exceed the max value of 32767 (See: VIEWPORT).

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 988
  • Marco
Re: bits...
« Reply #23 on: February 06, 2019, 03:12:41 AM »
@Marc'Antonio Alessi
AFAIK 'negative' bits are not used in entity lists. But bit-coded values do exceed the max value of 32767 (See: VIEWPORT).
I have never exceeded 32767... so what is the maximum value?
Code: [Select]
Viewport status bit-coded flags:
...
32768 (0x8000) = Currently always enabled
65536 (0x10000) = Enables non-rectangular clipping
131072 (0x20000) = Turns the viewport off
262144 (0x40000) = Enables the display of the grid beyond the drawing limits
524288 (0x80000) = Enable adaptive grid display
1048576 (0x100000) = Enables subdivision of the grid below the set grid spacing when the grid display is adaptive
2097152 (0x200000) = Enables grid follows workplane switching
Code: [Select]
(defun bits ( n / b r )
    (repeat (setq b 32) (setq r (cons (logand n (lsh 1 (setq b (1- b)))) r)))
    (vl-remove 0 r)
)
(bits (+ 32768 65536 131072 262144 524288 1048576 2097152)) =>  (32768 65536 131072 262144 524288 1048576 2097152)

roy_043

  • Water Moccasin
  • Posts: 1700
  • BricsCAD 18
Re: bits...
« Reply #24 on: February 06, 2019, 03:41:42 AM »
If the max bit is 2097152, the bit-coded value would have a max of (+ 2097152 2097152 -1).

gile

  • Water Moccasin
  • Posts: 2222
  • Marseille, France
Re: bits...
« Reply #25 on: February 06, 2019, 04:07:21 AM »
The gc-purge LISP function defined in the RadicalPurge plugin uses bits up to 2097152 (see the attached help file).
Speaking English as a French Frog

MP

  • Seagull
  • Posts: 17370
Re: bits...
« Reply #26 on: February 06, 2019, 08:16:50 AM »
If the max bit is 2097152 (expt 2 21), the bit-coded value would have a max of 4194303 (1- (expt 2 22)).
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Lee Mac

  • Seagull
  • Posts: 12241
  • London, England
Re: bits...
« Reply #27 on: February 06, 2019, 08:17:28 AM »
I have never exceeded 32767... so what is the maximum value?

Maximum used in AutoCAD, or maximum in general for a 32-bit signed integer?

For the latter, it would be the most significant bit, i.e. the sign bit:

10000000 00000000 00000000 00000000 = (~ 2147483647) = -2147483648

With every possible bit-code set, this would be:

11111111 11111111 11111111 11111111 = (~ 0) = -1

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 988
  • Marco
Re: bits...
« Reply #28 on: February 06, 2019, 08:39:48 AM »
BricsCAD vs. AutoCAD:
Code: [Select]
(defun bits ( n / b r )
    (repeat (setq b 32) (setq r (cons (logand n (lsh 1 (setq b (1- b)))) r)))
    (vl-remove 0 r)
)
Code: [Select]
; Bricscad
: (bits (1- (expt 2 27)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864)

: (bits (1- (expt 2 28)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728)

: (bits (1- (expt 2 29)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456)

: (bits (1- (expt 2 30)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912)

: (bits (1- (expt 2 31)))
; ----- LISP : Call Stack -----
; [0]...BITS <<--
;

Code: [Select]
; AutoCAD
Comando: (bits (1- (expt 2 31)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824)

Comando: (bits (1- (expt 2 32)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648)

Comando: (bits (1- (expt 2 33)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648)

Comando: (bits (1- (expt 2 45)))
(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648)

MP

  • Seagull
  • Posts: 17370
Re: bits...
« Reply #29 on: February 06, 2019, 08:49:22 AM »
Your AutoCAD results are misleading as anything over (expt 2 31) returns 0 in AutoCAD, e.g. (expt 2 32) >> 0.

Bricscad is crashing because anything over (expt 2 31) returns a real, e.g. (expt 2 32) >> 4294967296.0 and logand expects an integer. Kaboom.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox