Article View: comp.lang.c.moderated
Article #2430Re: Efficiency Question : bits vs. bytes (or anything else, for that matter)
From: J. Kanze
Date: Fri, 29 Mar 1996 12:08
Date: Fri, 29 Mar 1996 12:08
53 lines
2614 bytes
2614 bytes
In article <4je1p1$g1s@solutions.solon.com>
schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) writes:
|> I posted the following suggestion for a ``packed array'' of bits yesterday:
|> |> If you are interested in space, one suggestion is
|> |>
|> |> # include <limits.h>
|> |>
|> |> # define UNSIGNED_BIT (CHAR_BIT * sizeof (unsigned))
|> |>
|> |> struct {
|> |> unsigned the_bits [UNSIGNED_BIT]: 1;
|> |> } the_array [800][(800 + UNSIGNED_BIT - 1) / UNSIGNED_BIT];
|> |>
|> |> # define BIT(X, Y)\
|> |> the_array [Y][(X) / UNSIGNED_BIT].the_bits [(X) % UNSIGNED_BIT]
|> Upon further reflection, I now am more inclined toward the following:
|> struct {
|> unsigned b [800][800]:1;
|> } the_array;
So what does your compiler have to say about it? According to all of
the documation I've read (including ISO 9899), bit fields must have
integral types. And an array is not an integral type.
I've generally used the following macros (BM: Bit Map):
#define BM_SIZE( n ) ((n) / CHAR_BITS + ((n) % CHAR_BITS != 0))
#define BM_ISSET( b , n ) \
(((b)[ (n) / CHAR_BITS ] & (1 << ((n) % CHAR_BITS))) != 0)
#define BM_SET( b , n ) \
((b)[ (n) / CHAR_BITS ] |= (1 << ((n) % CHAR_BITS)))
#define BM_RESET( b , n ) \
((b)[ (n) / CHAR_BITS ] &= ~(1 << ((n) % CHAR_BITS)))
/* For a bitmap of `unsigned char' */
char bm[ BM_SIZE( UCHAR_MAX + 1 ) ] ;
memset( bm , 0 , sizeof( bm ) ) ;
BM_SET( bm , (unsigned char)( 'a' ) ) ;
if ( BM_ISSET( bm , (unsigned char)( 'b' ) )
...
Care is needed in their use, since they evaluate there arguments several
times, but I've found them quite useful in the past.
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Technical Details
Message-ID:
<4jgjrg$s9s@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4jbciv$2kk@solutions.solon.com> <4jckmc$9v7@solutions.solon.com> <4je1p1$g1s@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: kanze@gabi-soft.fr (J. Kanze)
Newsgroups: comp.lang.c.moderated
Subject: Re: Efficiency Question : bits vs. bytes (or anything else, for that matter)
Date: 29 Mar 1996 06:08:16 -0600
Organization: GABI Software, Sarl.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4jgjrg$s9s@solutions.solon.com>
References: <4jbciv$2kk@solutions.solon.com> <4jckmc$9v7@solutions.solon.com>
<4je1p1$g1s@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com