Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

help: what does this c code signify

Status
Not open for further replies.

alvarorahul

Advanced Member level 4
Joined
Apr 13, 2005
Messages
110
Helped
6
Reputation
12
Reaction score
1
Trophy points
1,298
Activity points
2,062
what is the significance of the colon in the following code


struct abc
{
int bit1:1;
int bit3:4;
int bit4:4;
}xyz(1,2,2);

main()
{
//code
}
 

newelltech

Full Member level 4
Joined
Aug 23, 2006
Messages
216
Helped
34
Reputation
68
Reaction score
2
Trophy points
1,298
Activity points
2,929
struct abc
{
int bit1:1;
int bit3:4;
int bit4:4;
}xyz(1,2,2);

These are packed bit fields. The number after the colon is the length. The compiler will pack these into an integer if possible. Usually this is only needed if memory is at a premium. Generally it is best to use unsigned int instead of int so no bits are added for the sign.

So in the xyz variable,
xyz.bit1 = 1 and is 1 bit long
xyz.bit3 = 2 and is 4 bits long
xyz.bit4 = 2 and is 4 bits long
All the above values will be automatically converted to int.
I don't know what happened to bit2. But it could have been set to a 7 bit variable without increasing the memory use.

Use bit fields with caution as they are not very portable between platforms (int16 vs 1in32).

Hope this helps
 

alvarorahul

Advanced Member level 4
Joined
Apr 13, 2005
Messages
110
Helped
6
Reputation
12
Reaction score
1
Trophy points
1,298
Activity points
2,062
newelltech said:
I don't know what happened to bit2. But it could have been set to a 7 bit variable without increasing the memory use.

i did not understand this part........how is it this can be done?

even so .........int is 2 bytes right? so even if we do 15 bit variable bit2 then also it shd happen widout extra memory being used?

am a little confused.........pls explain :?:
 

vsmGuy

Advanced Member level 2
Joined
Jun 26, 2006
Messages
503
Helped
44
Reputation
88
Reaction score
15
Trophy points
1,298
Location
VddVss
Activity points
3,886
alvarorahul said:
newelltech said:
I don't know what happened to bit2. But it could have been set to a 7 bit variable without increasing the memory use.

i did not understand this part........how is it this can be done?

even so .........int is 2 bytes right? so even if we do 15 bit variable bit2 then also it shd happen widout extra memory being used?

am a little confused.........pls explain :?:

I may help you more if you can tell me in what application you need this.

If you are just asking this to increase you C Aptitude, then you will be better off asking at C Newsgroup than EDABoard.

But if this is formware related, better give a more complete snippet of code and I iwll try to help you out.
 

newelltech

Full Member level 4
Joined
Aug 23, 2006
Messages
216
Helped
34
Reputation
68
Reaction score
2
Trophy points
1,298
Activity points
2,929
The definition of 'int' is platform dependent, i.e. it depends on what compiler you're using since compilers are free to set the size of int.
My explanation was assuming that an integer was 16 bits and by adding a bit2 of 7 bits long, the total bits used would be 16

bit1 length = 1
bit2 length = 7
bit3 length = 4
bit4 length = 4
----------------
total =16 bits = one integer

If you're writing code for the PC, more than likely your compiler has 32 bit integers. Some microprocessor compilers have 16 bit integers. I haven't ever used a C compiler with 8 bit 'int' that supports bit fields.

Does this clear it up?
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top