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

0 Members and 1 Guest are viewing this topic.

ribarm

  • Water Moccasin
  • Posts: 1976
  • Marko Ribar, architect
bits...
« on: February 02, 2019, 01:57:47 AM »
Hi... I've put this short sub for splitting integer into bits, but I am unsure is it correct for negative integers... I know (logand), (logior) functions work and for negative numbers, but I don't understand the logic behind... Any insight is welcome...

Code: [Select]
;;; bits by M.R.

(defun bits ( n / bit bitl rtn ) ;;; n - integer ; (bits 20000) => (32 512 1024 2048 16384) ; (bits -20000) => (-16384 -2048 -1024 -512 -32) ; (bits 0) => nil
  (if (minusp n)
    (progn
      (setq bit -0.5)
      (while (>= (setq bit (fix (* bit 2.0))) n)
        (setq bitl (cons bit bitl))
      )
      (foreach bit (reverse bitl)
        (if (= (logand (- bit) (- n)) (- bit))
          (setq rtn (cons bit rtn))
        )
      )
      rtn
    )
    (progn
      (setq bit 0.5)
      (while (<= (setq bit (fix (* bit 2.0))) n)
        (setq bitl (cons bit bitl))
      )
      (foreach bit (reverse bitl)
        (if (= (logand bit n) bit)
          (setq rtn (cons bit rtn))
        )
      )
      (reverse rtn)
    )
  )
)

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1659
  • BricsCAD 18
Re: bits...
« Reply #1 on: February 02, 2019, 03:54:10 AM »
-2000 represented as a 32-bit binary number:
11111111111111111111100000110000
The list your function returns should contain a number for every '1':
Code - Auto/Visual Lisp: [Select]
  1. (defun KGA_Math_BitList (int)
  2.    0
  3.    (mapcar
  4.      '(lambda (idx) (lsh (lsh (lsh int (- 31 idx)) -31) idx))
  5.      '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31)
  6.    )
  7.  )
  8. )
(KGA_Math_BitList -2000) =>
(16 32 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 951
  • Marco
Re: bits...
« Reply #2 on: February 02, 2019, 05:14:34 AM »
Code: [Select]
; Function: UtlBits_GetBitWise ; Credits: Juerg Menzi
;
; Version 1.00 - 16/11/2006
;
; Arguments:
;   BtsVal = Integer [INT]
;
; Return Values:
;  [LIST] a list of all logical bitwise AND's of an integer
;         or nil if BtsVal > 32767 or < 1
;
; Examples:
;  (UtlBits_GetBitWise 32767)
;  ==> (1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384)
;  (UtlBits_GetBitWise 32768)
;  ==> nil
;  (UtlBits_GetBitWise   nil)
;  ==> nil
;  (UtlBits_GetBitWise     0)
;  ==> nil
;
(defun UtlBits_GetBitWise (BtsVal / CurBit OutVal)
  (cond
    ( (> 1 BtsVal 32767) nil ) ; 32767 = bits sum max value
    ( (setq CurBit 1)
      (while (<= CurBit BtsVal)
        (if (= (logand BtsVal CurBit) CurBit)
          (setq OutVal (cons CurBit OutVal))
        )
        (setq CurBit (* CurBit 2))
      )
      (reverse OutVal)
    )
  )
)

ribarm

  • Water Moccasin
  • Posts: 1976
  • Marko Ribar, architect
Re: bits...
« Reply #3 on: February 02, 2019, 07:31:22 AM »
So... With help of Roy's sub, I figured out what is logic behing (logand) and (logior) :

Code - Auto/Visual Lisp: [Select]
  1. (defun _bits ( int )
  2.    0
  3.    (mapcar
  4.      '(lambda ( idx ) (lsh (lsh (lsh int (- 31 idx)) -31) idx))
  5.      '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31)
  6.    )
  7.  )
  8. )
  9.  
  10. (defun _int ( l1 l2 / rtn )
  11.  (foreach x l1
  12.    (if (vl-position x l2)
  13.      (setq rtn (cons x rtn))
  14.    )
  15.  )
  16.  (reverse rtn)
  17. )
  18.  
  19. (defun _uni ( l1 l2 / unique )
  20.  (defun unique ( l )
  21.    (if l
  22.      (cons (car l)
  23.        (unique (vl-remove (car l) l))
  24.      )
  25.    )
  26.  )
  27.  (unique (append l1 l2))
  28. )
  29.  
  30. (defun _logand ( i1 i2 )
  31.  (apply '+ (_int (_bits i1) (_bits i2)))
  32. )
  33.  
  34. (defun _logior ( i1 i2 )
  35.  (apply '+ (_uni (_bits i1) (_bits i2)))
  36. )
  37.  

Thank you very much for your sub @Roy...
Regards...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1659
  • BricsCAD 18
Re: bits...
« Reply #4 on: February 02, 2019, 07:37:14 AM »
You may also want to look at the boole function.

Lee Mac

  • Seagull
  • Posts: 12153
  • London, England
Re: bits...
« Reply #5 on: February 02, 2019, 09:04:17 AM »
Nice code Roy  :-)

Here are a couple of alternatives, using logand -
Code - Auto/Visual Lisp: [Select]
  1. (defun bits ( n / b r )
  2.    (repeat (setq b 32) (setq r (cons (logand n (lsh 1 (setq b (1- b)))) r)))
  3.    (vl-remove 0 r)
  4. )
Code - Auto/Visual Lisp: [Select]
  1. (defun bits ( n )
  2.    (if (/= 0 n) (vl-remove 0 (cons (logand 1 n) (mapcar '(lambda ( x ) (lsh x 1)) (bits (lsh n -1))))))
  3. )
« Last Edit: February 02, 2019, 09:28:23 AM by Lee Mac »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 951
  • Marco
Re: bits...
« Reply #6 on: February 03, 2019, 05:06:10 AM »
Since it's a function I use very often I'm interested in being able to change it with a more efficient one.
Code: [Select]
Benchmark.lsp | 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (BITSJ1 32767)................1469 / 7.61 <fastest>
    (BITSJ1 32767)................1485 / 7.52
    (BITSMR 32767)................2031 / 5.5
    (BITSMR 32767)................2062 / 5.42
    (BITSL1 32767)................3047 / 3.67
    (BITSL1 32767)................3047 / 3.67
    (_BITS 32767).................4093 / 2.73
    (KGA_MATH_BITLIST 32767)......4094 / 2.73
    (KGA_MATH_BITLIST 32767)......4109 / 2.72
    (_BITS 32767).................4156 / 2.69
    (BITSL2 32767)...............11125 / 1
    (BITSL2 32767)...............11172 / 1 <slowest>

Elapsed milliseconds / relative speed for 65536 iteration(s):
    (BITSJ1 128)...............1062 / 3.9 <fastest>
    (BITSJ1 128)...............1078 / 3.84
    (BITSMR 128)...............1328 / 3.12
    (BITSMR 128)...............1328 / 3.12
    (BITSL1 128)...............2968 / 1.4
    (BITSL1 128)...............2984 / 1.39
    (_BITS 128)................4000 / 1.04
    (KGA_MATH_BITLIST 128).....4016 / 1.03
    (KGA_MATH_BITLIST 128).....4031 / 1.03
    (_BITS 128)................4094 / 1.01
    (BITSL2 128)...............4125 / 1
    (BITSL2 128)...............4141 / 1 <slowest>
Code: [Select]
(defun bitsMR ( n / bit bitl rtn ) ;;; n - integer ; (bits 20000) => (32 512 1024 2048 16384) ; (bits -20000) => (-16384 -2048 -1024 -512 -32) ; (bits 0) => nil
  (if (minusp n)
    (progn
      (setq bit -0.5)
      (while (>= (setq bit (fix (* bit 2.0))) n)
        (setq bitl (cons bit bitl))
      )
      (foreach bit (reverse bitl)
        (if (= (logand (- bit) (- n)) (- bit))
          (setq rtn (cons bit rtn))
        )
      )
      rtn
    )
    (progn
      (setq bit 0.5)
      (while (<= (setq bit (fix (* bit 2.0))) n)
        (setq bitl (cons bit bitl))
      )
      (foreach bit (reverse bitl)
        (if (= (logand bit n) bit)
          (setq rtn (cons bit rtn))
        )
      )
      (reverse rtn)
    )
  )
)

; -- Function MeGetBitwise
; Returns a list of all logical bitwise AND's of an integer.
; Copyright:
;   1998 MENZI ENGINEERING GmbH, Switzerland
; Arguments [Typ]:
;   Val = Value [INT]
; Return [Typ]:
;   > List of logical bitwise AND's [LIST]
; Notes:
;   None
;
(defun BitsJ1 (n / b o) ; > MeGetBitwise

      (setq b 1)
      (while (<= b n)
        (if (= (logand n b) b)
          (setq o (cons b o))
        )
        (setq b (* b 2))
      )
      (reverse o)
)

(defun bitsL1 ( n / b r )
    (repeat (setq b 32) (setq r (cons (logand n (lsh 1 (setq b (1- b)))) r)))
    (vl-remove 0 r)
)

(defun bitsL2 ( n )
    (if (/= 0 n) (vl-remove 0 (cons (logand 1 n) (mapcar '(lambda ( x ) (lsh x 1)) (bitsL2 (lsh n -1))))))
)

(defun KGA_Math_BitList (int)
  (vl-remove
    0
    (mapcar
      '(lambda (idx) (lsh (lsh (lsh int (- 31 idx)) -31) idx))
      '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31)
    )
  )
)

(defun _bits ( int )
  (vl-remove
    0
    (mapcar
      '(lambda ( idx ) (lsh (lsh (lsh int (- 31 idx)) -31) idx))
      '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31)
    )
  )
)
 
(defun _int ( l1 l2 / rtn )
  (foreach x l1
    (if (vl-position x l2)
      (setq rtn (cons x rtn))
    )
  )
  (reverse rtn)
)
 
(defun _uni ( l1 l2 / unique )
  (defun unique ( l )
    (if l
      (cons (car l)
        (unique (vl-remove (car l) l))
      )
    )
  )
  (unique (append l1 l2))
)
 
(defun _logand ( i1 i2 )
  (apply '+ (_int (_bits i1) (_bits i2)))
)
 
(defun _logior ( i1 i2 )
  (apply '+ (_uni (_bits i1) (_bits i2)))
)
« Last Edit: February 03, 2019, 05:15:38 AM by Marc'Antonio Alessi »

Lee Mac

  • Seagull
  • Posts: 12153
  • London, England
Re: bits...
« Reply #7 on: February 03, 2019, 05:28:55 AM »
Apples/Oranges

Code: [Select]
_$ (bitsmr -512)
(-512)
_$ (bitsj1 -512)
nil
_$ (bitsl1 -512)
(512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648)
_$ (bitsl2 -512)
(512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648)
_$ (kga_math_bitlist -512)
(512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648)

roy_043

  • Water Moccasin
  • Posts: 1659
  • BricsCAD 18
Re: bits...
« Reply #8 on: February 03, 2019, 08:51:07 AM »
BricsCAD/AutoCAD?

gile

  • Water Moccasin
  • Posts: 2186
  • Marseille, France
Re: bits...
« Reply #9 on: February 03, 2019, 09:07:33 AM »
I know (logand), (logior) functions work and for negative numbers, but I don't understand the logic behind... Any insight is welcome...

I'll try an explaination.
To make things more readable, we'll consider 8 bits signed integers, i.e. range [-128..127] (where the most significant bit (128) is used for the sign).
A negative number (let's say -100) is the 1's complement (bitwise not) of its absolute value minor one (99).
Code - Auto/Visual Lisp: [Select]
  1. (~ 99) ; -> -100
The bits for 99 are:
Code: [Select]
01100011 -> (logior 64 32 2 1)So the bits for -100 are:
Code: [Select]
10011100 -> (logior -128 16 8 4)
« Last Edit: February 03, 2019, 09:11:25 AM by gile »
Speaking English as a French Frog

ribarm

  • Water Moccasin
  • Posts: 1976
  • Marko Ribar, architect
Re: bits...
« Reply #10 on: February 04, 2019, 03:48:12 AM »
In VLIDE I got something different than your 0 and 1 bits...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Water Moccasin
  • Posts: 1976
  • Marko Ribar, architect
Re: bits...
« Reply #11 on: February 04, 2019, 06:21:32 AM »
Using @Roy's sub :

Code: [Select]
Command: (_bits 99)
(1 2 32 64)
Command: (_bits -100)
(4 8 16 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)

Code: [Select]
Command: (apply 'logior (_bits 99))
99
Command: (apply 'logior (_bits -100))
-100
« Last Edit: February 04, 2019, 06:28:03 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

gile

  • Water Moccasin
  • Posts: 2186
  • Marseille, France
Re: bits...
« Reply #12 on: February 04, 2019, 06:57:10 AM »
In VLIDE I got something different than your 0 and 1 bits...

Because the VLIDE uses a 'signed notation' for binary.
As LISP uses 32 bits integers, the 'classical notation' for 99 should be:
Code: [Select]
0000 0000 0000 0000 0000 0000 0110 0011and for -100 or (~ 99)
Code: [Select]
1111 1111 1111 1111 1111 1111 1001 1100And assuming the Most Significant Bit is the sign bit (2^31 = -2147483648), this matches Roy's or Lee's results.

Try this:
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda (e b) (* b (expt 2 e)))
  2. '(31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0)
  3. '(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1)
  4. )
  5.  
  6. (mapcar '(lambda (e b) (* b (expt 2 e)))
  7. '(31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0)
  8. '(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0)
  9. )

Speaking English as a French Frog

ribarm

  • Water Moccasin
  • Posts: 1976
  • Marko Ribar, architect
Re: bits...
« Reply #13 on: February 04, 2019, 03:32:18 PM »
In math I learned :
(2^31 = +2147483648)

In PC math :
(2^31 = -2147483648)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12153
  • London, England
Re: bits...
« Reply #14 on: February 04, 2019, 03:52:19 PM »
In math I learned :
(2^31 = +2147483648)

In PC math :
(2^31 = -2147483648)

Two's complement

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 951
  • 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: 12153
  • 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: 2186
  • 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: 12153
  • 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: 951
  • 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: 2186
  • 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: 12153
  • 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: 1659
  • 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: 951
  • 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: 1659
  • 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: 2186
  • 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: 17278
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 | About

Lee Mac

  • Seagull
  • Posts: 12153
  • 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: 951
  • 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: 17278
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 | About

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 951
  • Marco
Re: bits...
« Reply #30 on: February 06, 2019, 10:17:39 AM »
Code: [Select]
(defun c:test ( / num)
  (setq num 2)
  (while (= num (logand num num))
    (if (zerop num)
      (setq num nil)
      (progn
        (princ num) (princ " ")
        (setq num (* 2 num))
      )
    )
  )
)

Code: [Select]
BricsCAD:
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
4294967296 8589934592 17179869184 34359738368 68719476736 137438953472 274877906944 549755813888 1099511627776 2199023255552 4398046511104 8796093022208 17592186044416 35184372088832 70368744177664
140737488355328 281474976710656 562949953421312 1125899906842624 2251799813685248 4503599627370496 9007199254740992 18014398509481984 36028797018963968 72057594037927936 144115188075855872
288230376151711744 576460752303423488 1152921504606846976 2305843009213693952 4611686018427387904 -9223372036854775808

: (logand 4611686018427387904 4611686018427387904)
4611686018427387904
Code: [Select]
AutoCAD:
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 ; errore: tipo di argomento errato: fixnump: nil

Comando: (logand 4611686018427387904 4611686018427387904)
; errore: tipo di argomento errato: fixnump: 4.61169e+018

ronjonp

  • Needs a day job
  • Posts: 6844
Re: bits...
« Reply #31 on: February 06, 2019, 11:37:37 AM »
This is getting a 'bit' complicated  :-D

Windows 10 x64 - AutoCAD /C3D 2019

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 951
  • Marco
Re: bits...
« Reply #32 on: February 06, 2019, 11:58:32 AM »
This is getting a 'bit' complicated  ;D
8) :crazy2: