Re: define a data type of 1 bit size

From: Alex Fraser (me_at_privacy.net)
Date: 09/06/04


Date: Mon, 6 Sep 2004 15:50:36 +0100


"Angel Lopez" <angel_lul@hotmail.com> wrote in message
news:9d186f1c.0409060535.1d6b342b@posting.google.com...
> Sorry if I am too naive, but this is my first post...
> I have a binary variable (it can contain either a 0 or a 1).
> Is there any way I can define a data type that uses only 1 bit. So
> far I have defined it as a char variable. I've searched everywhere but
> I don't seem to find any place that explains how to define this type
> of data type. The closest thing I've found are bit fields in
> structures, I would like something like bit fields but without the
> structure.

If you don't have too many of them, using a built-in type such as char or
even int is fine - just use zero for 0 and non-zero for 1.

If you want lots (and lots) of them in an array, you can save memory and
perhaps get better performance by using an unsigned type such as unsigned
int and writing simple functions or macros to access them by specifying a
bit index. The optimal type will depend on the platform, but it's easy to
write code so you can change the type to experiment if and when you find
performance to be a problem.

Compilers for some microcontrollers (such as 8051-alikes) have extensions to
the language that allow you to define bit variables. Bit variables are
typically more useful in these embedded environments where memory may be
very limited.

Alex



Relevant Pages