Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site watmath.UUCP Path: utzoo!watmath!rbutterworth From: rbutterworth@watmath.UUCP (Ray Butterworth) Newsgroups: net.lang.c Subject: Re: Portable arrays of bits Message-ID: <3249@watmath.UUCP> Date: Thu, 6-Nov-86 08:59:09 EST Article-I.D.: watmath.3249 Posted: Thu Nov 6 08:59:09 1986 Date-Received: Fri, 7-Nov-86 21:27:11 EST References: <140@houligan.UUCP> <8828@sun.uucp> <608@kodak.UUCP> Organization: U of Waterloo, Ontario Lines: 29 > 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. Some machines have fast function calls and assembler instructions for directly addressing bits. But if you want portable, how about this? It should run on any C with an appropriate setting of the first define. Make sure that the _BPW_ is 36 or 16 or however many bits there are in an (int) on your machine. Make sure you use (int) and not (long) or (char) for the array; on many machines accessing a (char) is a lot more work than accessing an (int). #define _BPW_ (36) /* bits per word (natural machine size, not char) */ #define _BIT_(bit) (1<<((bit)%_BPW_)) /* internal use */ #define _WORD_(name,bit) ((name)[(bit)/_BPW_]) /* internal use */ #define DECLARE(name,bits) int name[1+((bits)/_BPW_)] #define SET(name,bit) (_WORD_(name,bit)|=_BIT_(bit)) #define CLEAR(name,bit) (_WORD_(name,bit)&=~_BIT_(bit)) #define FLIP(name,bit) (_WORD_(name,bit)^=_BIT_(bit)) #define TEST(name,bit) ((_WORD_(name,bit)&_BIT_(bit))!=0) #define SAME(name1,name2,bit) \ (((_WORD_(name1,bit)^_WORD_(name2,bit))&_BIT_(bit))==0) etc.