Continue to Site

# Data Type for storing 1 bit in c

Status
Not open for further replies.

#### e shade

##### Member level 3
Can anyone help me in finding data type for storing one bit data in C language.

#### FvM

##### Super Moderator
Staff member
Effective storage of logical values is implementation dependent. You have bitfields as a standard C means, some compilers also support an explicite bit or int1 type. What's your compiler?

#### qwertyuiop415263

##### Full Member level 4
which sofware you are using?

#### e shade

##### Member level 3
thanks. its GCC based compiler, i am using that in openAT IDE for wavecom modules.

---------- Post added at 14:29 ---------- Previous post was at 14:25 ----------

@FvM: i checked with explicite bit or int1 type/ bitfield. none is working out.

#### FvM

##### Super Moderator
Staff member
What are you trying to achieve? Do you actually need to save storage space? For portable code, you should better use char or int to represent a boolean value.

#### alexan_e

##### Administrator
You can create your own but usually you can use simple char variable, is the space that limited?

This should work:

Code C - [expand]1
2
3
4
5
struct {unsigned int running:1; unsigned int stopped:1; unsigned int counter:4;} machine;

machine.stopped = 1;    // only 0 and 1 values are allowed, this is a bit
machine.running = 0;        // only 0 and 1 values are allowed, this is a bit
machine.counter++;      // this is a 4 bit value so 0-15 are allowed

Alex

e shade

### e shade

Points: 2
Helpful Answer Positive Rating

#### e shade

##### Member level 3
Thanks for comments...
@FvM: while i was trying to save bool value in u8, an argument raised "is there any provision of datatype for storing one bit data". So i wish to clarify the same.
@alexan:the values are restricted to 0 and 1 but the datatype of variables represent int(byte).storage space not reduced.correct???

#### alexan_e

##### Administrator
@alexan:the values are restricted to 0 and 1 but the datatype of variables represent int(byte).storage space not reduced.correct???

It is one integer broken in small slices with different name for each slice, the complete structure represents one integer memory space.

You can also do it on your own using bit wise operations "and" and "or" the same way you set/clear only one bit of a port.
For example

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))

char mychar;

SETBIT(mychar,0);       // set bit 0 to 1
CLEARBIT(mychar,0);   // set bit 0 to 0

SETBIT(mychar,1);       // set bit 1 to 1
CLEARBIT(mychar,1);   // set bit 1 to 0

SETBIT(mychar,2);       // set bit 2 to 1
CLEARBIT(mychar,2);   // set bit 2 to 0

Its up to you to use each bit to store a boolean value which represents something

Alex

---------- Post added at 14:48 ---------- Previous post was at 14:30 ----------

I forgot to mention the way to check the value of the bit

Code C - [expand]1
2
3
4
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))

if CHECKBIT(mychar,0)  // check for 1
if (!CHECKBIT(mychar,0))  // check for 0

#### FvM

##### Super Moderator
Staff member
Reviewing to the latest example provided by alex, I think it's also reasonable to check, if packing bits into a larger entity has any benefit, if the hardware platform doesn't provide bit instructions. It most likely has advantages for larger arrays to safe storage space. But not for single bits, because it's too expensive in terms of ROM requirement.

In addition you should check, if portable constructs with bit shifts are effectively understood by a compiler, if the processors has bit instructions. Some, e.g. CCS C, are known to generate huge overhead for similar constructs.

Finally, I still didn't understand, if the original question refers to a real problem beyond an academic discussion.

#### polo-g40

##### Full Member level 2
I've been intrigued and had a go at what I thought was a good question to look at, I'm about to thinks about going through my code similarly.. I like the Set/Clear/Checkbits and will wade through the theory after a tea.....

But, I had thought of this recently,
I'd been looking at the PC16F887.h that comes with HTC for just anything interesting and found these

volatile bit RC1 @ ((unsigned)&PORTC* 8 )+1; // etc
volatile bit RC2 @ ((unsigned)&PORTC* 8 )+2;
volatile bit RC3 @ ((unsigned)&PORTC*8 )+3;
volatile bit EEPGD @ ((unsigned)&EECON1*8 )+7; // etc

so it looks to me like it takes the pointer address, times it by 8, add number of bits. Giving the number of bits from zero that the bit is in memory..?.. ////

Anyway, I tried to do similar
static char OneNaughtBits=0; // To store a bit it'll need a byte anyway to put it in.
// If we have each bit of
// the byte for each bit var, saves ((number of yes/no/1/0's)-1 bytes)
// ad infinitem (0-7) char or 0-16 word etc

#define IsMyArmsOnFire @ ((unsigned)&OneNaughtBits*8 )+1; // No They ARE on fire//
#define AmIDreaming @ ((unsigned)&OneNaughtBits*8 )+2; // LetsHopeSo.

and even
static char IsMyHeadOnFire @ ((unsigned int)*OneNaughtBits); // No

but the complier throws

{ Error [981] C:\MPLab2\sla7029M-01\SLA7029Step1.c; 46.59 pointer required
Error [981] C:\MPLab2\sla7029M-01\SLA7029Step1.c; 46.59 pointer required
Error [188] C:\MPLab2\sla7029M-01\SLA7029Step1.c; 46.60 constant expression required
Error [236] C:\MPLab2\sla7029M-01\SLA7029Step1.c; 46.60 simple integer expression required }
...

Is there a way to impliment this idea? The coding the << way is only the same as what is done in the compiler to get to the bits anyway I would think so OKish, but would like to know if the solution is workable?
Many thanks
NewNeal

Last edited:

#### FvM

##### Super Moderator
Staff member
PIC16 has in fact bit instructions, and most PIC compilers are supporting it somehow, e.g. CCS C bit a bit1 type. Bit access is at least needed for SFR and also suggested for variables due to the small PIC16 RAM space. I'm not aware at present of the HighTech C concepts for bit access, but I guess, they have some.

The quoted syntax however isn't directly referring to the C standard and needs explanation
Code:
volatile bit RC1 @ ((unsigned)&PORTC* 8 )+1;
You are discussing implementation details of a specific compiler. The first point would be to notice their exact decription of the intended syntax for bit variables in data space. I understand from your post, that you don't know it. Perhaps a HighTech C user can help?

P.S.: I checked, that the Microchip C30 compiler, which is based on GCC, achieves all accesses to bit data by defining bitfields. It doesn't have a data type smaller than char.

Last edited:

#### e shade

##### Member level 3
You can create your own but usually you can use simple char variable, is the space that limited?

This should work:

Code C - [expand]1
2
3
4
5
struct {unsigned int running:1; unsigned int stopped:1; unsigned int counter:4;} machine;

machine.stopped = 1;    // only 0 and 1 values are allowed, this is a bit
machine.running = 0;        // only 0 and 1 values are allowed, this is a bit
machine.counter++;      // this is a 4 bit value so 0-15 are allowed

Alex

Dear all,Thanks for your comments. the above was the actual thing which i was searching for. the complete definition about my question was given in below link

Low Level Operators and Bit Fields

polo-g40

### polo-g40

Points: 2
Helpful Answer Positive Rating
Status
Not open for further replies.