;;; 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)
)
)
)
; 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)
)
)
)
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>
(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)))
)
_$ (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)
I know (logand), (logior) functions work and for negative numbers, but I don't understand the logic behind... Any insight is welcome...
01100011 -> (logior 64 32 2 1)
So the bits for -100 are: 10011100 -> (logior -128 16 8 4)
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)
Command: (apply 'logior (_bits 99))
99
Command: (apply 'logior (_bits -100))
-100
In VLIDE I got something different than your 0 and 1 bits...
0000 0000 0000 0000 0000 0000 0110 0011
and for -100 or (~ 99)1111 1111 1111 1111 1111 1111 1001 1100
And assuming the Most Significant Bit is the sign bit (2^31 = -2147483648), this matches Roy's or Lee's results.In math I learned :
(2^31 = +2147483648)
In PC math :
(2^31 = -2147483648)
Is it possible to have an example of using negative bits in AutoCAD? :embarrassed:
(= 4 (logand (cdr (assoc 70 (entget <dim>))) (~ 224)))
(= 4 (logand 4 (cdr (assoc 70 (entget <dim>)))))
198 =
00000000 00000000 00000000 11000110
(~ 224) =
11111111 11111111 11111111 00011111
00000000 00000000 00000000 11000110
AND 11111111 11111111 11111111 00011111
---------------------------------------
00000000 00000000 00000000 00000110 = 6
Lee was faster (and his English is more fluent).
_$ (setq flag 5) ; -> 4 | 1
5
_$ (turnBitsOn 3 flag) ; -> 4 | 2 | 1
7
_$ (turnBitsOff 3 flag) ; -> 4
4
_$ (toggleBits 3 flag) ; -> 4 | 2
6
@Marc'Antonio AlessiI have never exceeded 32767... so what is the maximum value?
AFAIK 'negative' bits are not used in entity lists. But bit-coded values do exceed the max value of 32767 (See: VIEWPORT).
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
(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)
I have never exceeded 32767... so what is the maximum value?
(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)
)
; 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 <<--
;
; 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)
(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))
)
)
)
)
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
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
This is getting a 'bit' complicated ;D8) :crazy2: