Continue to Site

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.

problem using bit-fields

Status
Not open for further replies.

sachinkp21587

Member level 2
Joined
Mar 15, 2010
Messages
51
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
india (U.P.)
Activity points
1,703
according to me the following code structure should take 6 byte i.e. (3bits *16variable)/8bits but in practical case its taking 8 bytes.
how?
i am using mplab c18
struct {
unsigned int audio1 : 3;
unsigned int audio2 : 3;
unsigned int audio3 : 3;
unsigned int audio4 : 3;
unsigned int audio5 : 3;
unsigned int audio6 : 3;
unsigned int audio7 : 3;
unsigned int audio8 : 3;
unsigned int audio9 : 3;
unsigned int audio10 : 3;
unsigned int audio11 : 3;
unsigned int audio12 : 3;
unsigned int audio13 : 3;
unsigned int audio14 : 3;
unsigned int audio15 : 3;
unsigned int audio16 : 3;
} MutingDelay;

even if i am trying
struct {
unsigned int audio1 : 3;
unsigned int audio2 : 3;
unsigned int audio3 : 3;
unsigned int audio4 : 3;
unsigned int audio5 : 3;
}MutingDelay;

Then if unsigned int is 16 bits thn it should take 2 bytes, but its taking 3 bytes. Why?
 
Last edited:

1, Hi what is the size of int?? I think its not 8 may be 16 bit check your compiler manual...
2, when you using struct the first two variables will get allocated 6 bits but the next variable cant be allocated in remaining 2 bits so it will go for next byte thats why your are getting 8 instead of 6...
 

unsigned int is (normally) a 16bit type and seems to fit the size you describe

Code:
  unsigned int audio1 : 3;
    unsigned int audio2 : 3;
    unsigned int audio3 : 3;
    unsigned int audio4 : 3;
    unsigned int audio5 : 3;
  // 15 bit have been filled, the next three bits can't fit in the one bit left so they are allocated in the next integer space
    unsigned int audio6 : 3;
    unsigned int audio7 : 3;
    unsigned int audio8 : 3;
    unsigned int audio9 : 3;
    unsigned int audio10 : 3;
  // 15 bit have been filled, the next three bits can't fit in the one bit left so they are allocated in the next integer space
    unsigned int audio11 : 3;
    unsigned int audio12 : 3;
    unsigned int audio13 : 3;
    unsigned int audio14 : 3;
    unsigned int audio15 : 3;
  // 15 bit have been filled, the next three bits can't fit in the one bit left so they are allocated in the next integer space
    unsigned int audio16 : 3;

so it occupies four 16bit integers
 

it says 16bits Untitled.png

- - - Updated - - -

you are right alexan_e that what i think the compiler should work as per my knowledge. bt m not getting why is it taking 3 bytes for even 15 bits...
 

Then if unsigned int is 16 bits thn it should take 2 bytes, but its taking 3 bytes. Why?

Hi there is no meaning with unsigned int or char whtever its going to be 3 bits and going to be allocated in byte architecture so the structure padding will be
so first two values two bits remaining
second two variables two bits remaining
last variable
 

according to me the following code structure should take 6 byte i.e. (3bits *16variable)/8bits but in practical case its taking 8 bytes.
how?
i am using mplab c18


even if i am trying


Then if unsigned int is 16 bits thn it should take 2 bytes, but its taking 3 bytes. Why?

in first your code taking 3 byte and second code is taking only 1 byte as alexan_e mentioned .
 

whatever the data type you mentioned will be only used when using the variable when allocation every variable will be allocated byte wise....
 

Your compiler allocates the memory interms of 8 bits...
Code:
//1st 8 bit
unsigned int audio1 : 3;
unsigned int audio2 : 3;
//2nd 8 bit
unsigned int audio3 : 3;
unsigned int audio4 : 3;
//3rd 8 bit
unsigned int audio5 : 3;
unsigned int audio6 : 3;
//4th 8 bit
unsigned int audio7 : 3;
unsigned int audio8 : 3;
//5th 8 bit
unsigned int audio9 : 3;
unsigned int audio10 : 3;
//6th 8 bit
unsigned int audio11 : 3;
unsigned int audio12 : 3;
//7th 8 bit
unsigned int audio13 : 3;
unsigned int audio14 : 3;
//8th 8 bit
unsigned int audio15 : 3;
unsigned int audio16 : 3;
So 8 bytes...

for 2nd the same procedure is followed...
Code:
//1st 8 bit
unsigned int audio1 : 3;
unsigned int audio2 : 3;
//2nd 8 bit
unsigned int audio3 : 3;
unsigned int audio4 : 3;
//3rd 8 bit
unsigned int audio5 : 3;
So it takes 3 byte...
 
Last edited:

hey mathespbe why second code also take 8 byte i don't understand please tell me?

and jayanth also tell me how take 512 bit?
 
Last edited:

Hi look at the results

attachment.php


attachment.php


attachment.php


compiled using dev c++

- - - Updated - - -

The problem for stack overflow discussion may be the size of int is 16 bit...
 

Attachments

  • untitled1.JPG
    untitled1.JPG
    30.6 KB · Views: 135
  • untitled2.JPG
    untitled2.JPG
    25.8 KB · Views: 129
  • untitled.JPG
    untitled.JPG
    36.3 KB · Views: 138

hey mathespbe why second code also take 8 byte i don't understand please tell me?

Even though you are using 16 bit compiler your memory allocation takes as 8 bit (1 Byte) pattern...
If it doesn't 2nd should only take 2 bytes. Due to this reason only it took 3 bytes...
on the 1st 8 bit (1st BYTE) 3+3 =6 bits allocated... (2 bits not enoug to allocate 3 bit of memory)
on the 2nd 8 bit (2nd BYTE) 3+3 = 6 bits allocated... (2 bits not enoug to allocate 3 bit of memory)
on the 3rd 8 bit (3rd BYTE) 3 bits allocated.... (5 free bits)

if you allocate another variable of 5 bit wide also it'll allocated on the 3 BYTE itself...
 

i understand that 's a reason but in post #8 u posted both 8 byte but second one is only took 3 byte right ?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top