Article View: comp.lang.c.moderated
Article #2470Re: Efficiency Question : bits vs. bytes (or anything else, for that matter)
From: Richard A. O'Keefe
Date: Tue, 02 Apr 1996 12:38
Date: Tue, 02 Apr 1996 12:38
48 lines
2446 bytes
2446 bytes
schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) writes:
>Upon further reflection, I now am more inclined toward the following:
> struct {
> unsigned b [800][800]:1;
> } the_array;
*Sigh*. This is not legal C.
The standard says (Section 6.2.1, Semantics paragraph 5) that
"A bit field shall have a type that is a qualified or
unqualified version of one of int, unsigned int, or signed int."
The type here is "array [800] of array [800] of unsigned int", so this
is not legal.
There is *NO* way of directly getting a packed array of bits yourself
in standard C (or C++). You will _have_ to program it yourself using
shifts and masks.
>Furthermore, in yesterdays posting making the size of the cell sizeof (unsigned)
>is arbitrary; in particular, it has nothing to do with the fact that the
>type of the bitfield is unsigned. In the environment I am currently
>computing in, a cell size of sizeof (void *) would be more likely to make
>the cell size equal to the machine word length.
There are machines in which sizeof (void *) greatly exceeds the size of
any integral type. If you want a packed array of bits, you will need
a plain array whose elements are of some integral type; it will be
simpler if they are unsigned. The reasonable implementations are
#define round_up(x,t) (((x) + (CHAR_BIT * sizeof (t) - 1)) \
/ (CHAR_BIT * sizeof (t)) )
unsigned char uc[round_up(x, unsigned char)];
unsigned short us[round_up(x, unsigned short)];
unsigned int ui[round_up(x, unsigned int)];
unsigned long ul[round_up(x, unsigned long)];
--- modulo mistakes in the round_up() macro ---
and the best of them is probably the third.
--
Result of state election = 50% Victorians didn't get MP of their choice.
Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
Technical Details
Message-ID:
<4jr74b$h95@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: ok@cs.rmit.edu.au (Richard A. O'Keefe) Newsgroups: comp.lang.c.moderated Subject: Re: Efficiency Question : bits vs. bytes (or anything else, for that matter) Date: 2 Apr 1996 06:38:35 -0600 Organization: Comp Sci, RMIT, Melbourne, Australia Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jr74b$h95@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 X-Newsreader: NN version 6.5.0 #0 (NOV)