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.

Compacting RAM using 4-bit or 2-bit data type

Status
Not open for further replies.

banh

Advanced Member level 1
Joined
Dec 16, 2004
Messages
458
Helped
17
Reputation
34
Reaction score
5
Trophy points
1,298
Activity points
3,935
4-bit, 2-bit data type..

I wish to compact RAM as much as possible, so i define 4-bit and 2-bit data types to use when necessary:

struct {
unsigned char val:4;
}

struct {
unsigned char val:2;
}


What do you think about this? is it compiler-dependent to achieve this? are we always sure that the memory will be used in compact way with this?
 

Re: 4-bit, 2-bit data type..

Try to define 2 and 4 bit variables and use union (this must work).

Regards.


Mr.Cube
 

Re: 4-bit, 2-bit data type..

of course the structs i define work.

however, i found out that using this struct may not always optimize your RAM..
reason: the issue of memory alignment... this quite depends on your microcontroller and your compiler..
 

Re: 4-bit, 2-bit data type..

OK!

I know that in win programing when you have signed char program allocates 2 bytes, only unsigned char uses one byte, so, when you write char posibly compiler can allocate whole byte and just use few bits that you define inside this byte.

Who knows.

Mr.Cube
 

4-bit, 2-bit data type..

Compilers usually try to pack several bit fields into one word, but they usually don't try to pack several structs into one word. Try doing something like this:
Code:
struct
{
  unsigned val1:4, val2:4, val3:4, val4:4;
} foo;


16-bit signed char in a Windows compiler? Perhaps your compiler is set to an international mode. My two Windows compilers both have 8-bit signed char.
 

4-bit, 2-bit data type..

yuh,
quite compiler and microcontroller dependent...,

i guess to use memory in such efficient and "packed" way (i.e. use every bit and not waste any), we have to come up with very carefully-coded mem management stuff..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top