Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Data Type for storing 1 bit in c

Status
Not open for further replies.

e shade

Member level 3
Joined
Dec 31, 2010
Messages
59
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Activity points
1,581
Can anyone help me in finding data type for storing one bit data in C language.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
47,666
Helped
14,078
Reputation
28,413
Reaction score
12,742
Trophy points
1,393
Location
Bochum, Germany
Activity points
276,928
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?
 

e shade

Member level 3
Joined
Dec 31, 2010
Messages
59
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Activity points
1,581
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
Joined
Jan 22, 2008
Messages
47,666
Helped
14,078
Reputation
28,413
Reaction score
12,742
Trophy points
1,393
Location
Bochum, Germany
Activity points
276,928
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
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
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

Member level 3
Joined
Dec 31, 2010
Messages
59
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Activity points
1,581
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
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
@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
Joined
Jan 22, 2008
Messages
47,666
Helped
14,078
Reputation
28,413
Reaction score
12,742
Trophy points
1,393
Location
Bochum, Germany
Activity points
276,928
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
Joined
Jan 12, 2011
Messages
143
Helped
36
Reputation
84
Reaction score
42
Trophy points
28
Location
Gone
Activity points
0
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
Joined
Jan 22, 2008
Messages
47,666
Helped
14,078
Reputation
28,413
Reaction score
12,742
Trophy points
1,393
Location
Bochum, Germany
Activity points
276,928
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
Joined
Dec 31, 2010
Messages
59
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Activity points
1,581
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
 
Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top