🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2410

Re: Efficiency Question : bits vs. bytes (or anything else, for that matter)

#2410
From: Konrad Schwarz
Date: Wed, 27 Mar 1996 23:58
35 lines
1529 bytes
In article <4jbciv$2kk@solutions.solon.com>, vjarnot@mnw.net (Asmodeus) writes:
|> 
|> I have set up an array in the following manner (obviously I just need
|> 0 or 1) :
|> 
|> typedef struct my_bits {
|> 	unsigned the_bit : 1;
|> };
|> 
|> my_bits the_array[800][800];
|> 
|> 	My question is this : I have read that some operations ( =,+,- are
|> what I use) are slower when performed on bits as opposed to other
|> types.  My objective is to optimize the code for speed, and I figured
|> using less memory would help me do so, now I'm having doubts.  So what
|> is more efficient, bits or some other type???

If you are interested in speed, I suggest

	int the_bit [800][800];

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]

Konrad Schwarz
Technical Details
Message-ID: <4jckmc$9v7@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4jbciv$2kk@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz)
Newsgroups: comp.lang.c.moderated
Subject: Re: Efficiency Question : bits vs. bytes (or anything else, for that matter)
Date: 27 Mar 1996 17:58:04 -0600
Organization: TU Wien
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4jckmc$9v7@solutions.solon.com>
References: <4jbciv$2kk@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com