define a data type of 1 bit size

A

Angel Lopez

Sorry if this appears twice but my first try didn't seem to make it.
but this is my first post (hopefully)...
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.
something like
unsigned MyVariable :1;
Thanks,
Angel
 
M

Martin Ambuhl

Angel said:
Sorry if this appears twice but my first try didn't seem to make it.
but this is my first post (hopefully)...
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.

No. The smallest data type is the char, which is guaranteed to be at
least 8 bits.

You can use bit-masking on an unsigned integral type (unsigned char,
unsigned short, unsigned int, unsigned long, unsigned long long) or you
can use bitfields in a structure if you want.
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.

see above.
 
K

Kelsey Bjarnason

[snips]

Sorry if this appears twice but my first try didn't seem to make it.
but this is my first post (hopefully)...
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.

I seem to recall having worked with a compiler, years ago, that supported
a "bit" type... but that's totally non-portable, even to other compilers
on the same platform.

Probably the easiest is to just use unsigned chars or unsigned ints and a
function or two; it's not that hard to test to see if a given bit is set,
or to set or clear it.

Example: suppose we want to work with, oh, 500 bits, we might do something
like this:

unsigned char bitbuff[(500+(CHAR_BIT-1))/CHAR_BIT];

void setbit( unsigned bit, unsigned state )
{
unsigned byte = bit / CHAR_BIT;
bit %= CHAR_BIT;

bitbuff[byte] &= ~(1 << bit ); /* turns bit off */
if ( state )
bitbuff[byte] |= (1 << bit ); /* turn on if necessary */
}


It's morning, and I haven't had my coffee yet, so it may not be spot on,
but the concept should work.
 
M

Merrill & Michele

Kelsey Bjarnason said:
[snips]

Sorry if this appears twice but my first try didn't seem to make it.
but this is my first post (hopefully)...
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.

I seem to recall having worked with a compiler, years ago, that supported
a "bit" type... but that's totally non-portable, even to other compilers
on the same platform.

Probably the easiest is to just use unsigned chars or unsigned ints and a
function or two; it's not that hard to test to see if a given bit is set,
or to set or clear it.

Example: suppose we want to work with, oh, 500 bits, we might do something
like this:

unsigned char bitbuff[(500+(CHAR_BIT-1))/CHAR_BIT];

void setbit( unsigned bit, unsigned state )
{
unsigned byte = bit / CHAR_BIT;
bit %= CHAR_BIT;

bitbuff[byte] &= ~(1 << bit ); /* turns bit off */
if ( state )
bitbuff[byte] |= (1 << bit ); /* turn on if necessary */
}


It's morning, and I haven't had my coffee yet, so it may not be spot on,
but the concept should work.

But is this bit an ANSI c data type? MPJ
 
C

c453___

if u really want to be economic with process's memory u can use for
example a char (or int) and store in it up to 8 (or 32) one bit values ;)
 
C

Christopher Benson-Manica

c453___ said:
if u really want to be economic with process's memory u can use for
example a char (or int) and store in it up to 8 (or 32) one bit values ;)

You mean CHAR_BIT or sizeof( int )*CHAR_BIT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top