Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bit codes

41 views
Skip to first unread message

Jaysen Long

unread,
Jan 6, 1999, 3:00:00 AM1/6/99
to
How does one determine if a particular bit is set?
For example, the group 70 of a POLYLINE could be the sum of any of the
following:
0, 1, 2, 4, 8, 16, 32, 64, 128

What's the best way to determine if 8 is included in the value?
Any mathematicians out there?

Jaysen

Frank Whaley

unread,
Jan 6, 1999, 3:00:00 AM1/6/99
to
Jaysen Long wrote:

> How does one determine if a particular bit is set?

I usually use something like:

(= 8 (logand 8 value))

or:

(/= 0 (logand 8 value))
--
Frank Whaley
Autodesk, Inc.
f...@autodesk.com
http://www.autodesk.com

Matt Stachoni

unread,
Jan 7, 1999, 3:00:00 AM1/7/99
to
Jaysen,

On Wed, 06 Jan 1999 08:37:26 -0800, Jaysen Long <jlong...@bkf.com> wrote:

>How does one determine if a particular bit is set?

>For example, the group 70 of a POLYLINE could be the sum of any of the
>following:
>0, 1, 2, 4, 8, 16, 32, 64, 128
>
>What's the best way to determine if 8 is included in the value?
>Any mathematicians out there?

There's no real math except simple binary addition. It can be confusing, but
once you get the hang of it it's quite logical.

The LOGAND function basically does a boolean bitwise comparison of the binary
equivalents of two decimal numbers, masking off the bits that not are common to
both and returning the result in decimal.

Any decimal number can be expressed as a binary number:

Bit position 7 6 5 4 3 2 1 0
decmial equiv. 128 64 32 16 8 4 2 1
=========================================
binary number 0 1 0 1 1 1 0 1
decimal 64 + 16+8+4 + 1 = 93

Basically, each bit "slot" is the number 2 raised to the bit position
(e.g.,2^4 = 16, 2^0 = 1)

So, to check whether bit 8 is set in a 70 DXF code of 57 (to pick a number), you
are comparing what bits two decimal number have set to 1.

Thus to check these numbers:

Bit position 7 6 5 4 3 2 1 0
decmial equiv. 128 64 32 16 8 4 2 1
==========================================
binary 57 0 0 1 1 1 0 0 0 1
binary 8 0 0 0 0 1 0 0 0 0
==========================================
(logand 8 57) 0 0 0 0 1 0 0 0 0 = 8

In AutoLISP it's very common to check if a bit value is somewhere in a number so
I use the general formula:

(= bit (logand bit val))

So (= 8 (logand 8 57)) returns T

Matt
stac...@bellatlantic.net
msta...@architectsde.com
www.architectsde.com

Rune Wold

unread,
Jan 7, 1999, 3:00:00 AM1/7/99
to
The BOOLE function would work here:
(boole 1 VALUE1 VALUE2)
This code will return all bits that the two values have in common as one
value,
(boole 1 8 12)
will return 8.

Rune

Jaysen Long wrote in message <369391...@bkf.com>...


>How does one determine if a particular bit is set?
>For example, the group 70 of a POLYLINE could be the sum of any of the
>following:
>0, 1, 2, 4, 8, 16, 32, 64, 128
>
>What's the best way to determine if 8 is included in the value?
>Any mathematicians out there?
>

>Jaysen

Morten Warankov

unread,
Jan 7, 1999, 3:00:00 AM1/7/99
to
Jaysen,

In addition to what's said about checking for each bit.
If you want to see at-a-glance which bits are set, you can use the
'bitlist.lsp' found at our freeware pages www.abacus.no. The routine is
useful when you want to visualise which bits are set in let's say 3279.

(getbit 3279) returns (1 2 4 8 64 128 1024 2048),

and you can at-a-glance check which bits are set.

Hope this helps, (for anyone who doesn't think only 0 and 1 :-)

Mortenw
--
***************************************************************************
* A B A C U S
*
* Prof. Brochs gt. 6 Phone: (+47) 73 54 03 15 *
* N-7030 TRONDHEIM Fax: (+47) 73 94 38 61 *
* NORWAY E-mail: aba...@abacus.no *
* URL: http://www.abacus.no
*
*
*
* Formula for success: Underpromise and overdeliver. *
***************************************************************************

0 new messages