🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9739

Re: Portable arrays of bits

#9739
From: Thomas Paul Brisco
Date: Thu, 06 Nov 1986 16:04
42 lines
2145 bytes
>Is there a standard, machine independant way of defining an array 
>of bits?  I am writing code and do not want to have to do a function
>call each time I reference a bit, but want a Boolean array packed
>as tightly as possible.


	The immediate (and naive) approach is to say:
	struct{
		bit:1
		}[LOTS]

	However, C does not support bit fields directly. For every bit
field it will allocate a full word. This is distressing particularly
if you have a sparse structure, and many of them.

	A better approach is to define a subroutine or macro as
following:

#define getbit(x,p) ((x >> p)&~(~0 << 1))

	This is from K&R (God bless 'em). A "setbit" macro could be
fudged up fairly similiarly.  Of course for this to work *really*
good, you should make sure that the struct uses char's ,not int's --
vaxes have a (literally) twisted concept of how bytes fit into ints.
By using chars you avoid the little/big -endian problems. Also, you'll
probably want (somewhere) a #define for the number of bits in a
character (just to cover all bases) for computational purposes.

	The structure I used look like this:
	unsigned char bitmap[1024][1024]
and used the previous macro as a sub-routine to reference bits within
the char's.  I set bits by simple multiplications.




				tp.
-- 
                  ----------------------------------------------------------
                  -                  ARPA: Brisco@rutgers                  -
                  -  UUCP: (ihnp4!ut-sally, allegra!packard) !caip!brisco  -
                  ----------------------------------------------------------
Technical Details
Message-ID: <3862@caip.RUTGERS.EDU>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!rutgers!caip!brisco
References: <140@houligan.UUCP> <8828@sun.uucp> <608@kodak.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!rutgers!caip!brisco
From: brisco@caip.RUTGERS.EDU (Thomas Paul Brisco)
Newsgroups: net.lang.c
Subject: Re: Portable arrays of bits
Message-ID: <3862@caip.RUTGERS.EDU>
Date: Thu, 6-Nov-86 11:04:54 EST
Article-I.D.: caip.3862
Posted: Thu Nov  6 11:04:54 1986
Date-Received: Fri, 7-Nov-86 09:27:24 EST
References: <140@houligan.UUCP> <8828@sun.uucp> <608@kodak.UUCP>
Organization: Rutgers Univ., New Brunswick, N.J.
Lines: 42