10 bit struct, how to use less than 2 bytes?

Status
Not open for further replies.

T3STY

Full Member level 4
Joined
Apr 17, 2012
Messages
239
Helped
24
Reputation
48
Reaction score
24
Trophy points
1,308
Visit site
Activity points
3,715
With the new LCD Driver I made I'm using an int data type to store 10 bits. Although, I'd like to reduce the size to only 10 bits, the ones I'm really using.
I tried to define a bit field into a struct like this:
Code:
struct {
   unsigned DB0:1;
   unsigned DB1:1;
   unsigned DB2:1;
   unsigned DB3:1;
   unsigned DB4:1;
   unsigned DB5:1;
   unsigned DB6:1;
   unsigned DB7:1;
   unsigned RW:1;
   unsigned RS:1;
} LCD_DATA;
But standard C says it will fit into 2 bytes and it will use 2 bytes, even if I'll only be using 10 bits of 16 available.

Smart question: is there any way I can create a 10 bits struct so I won't waste 6 bits?
 

The structure looks like a collection of GPIO bits used to control a LCD. What's the purpose of mirroring it in memory?

Data objects are ususally aligned on byte boundaries for 8-Bit processors, with the exception of single bit variables that are supported by some processors.
 

I think each memory location is 1 byte wide. So, there are no nibble type memory location. 1 and 0 is also stored as a byte i.e., bit variable also takes 1 byte in memory. 0 is stored as 00000000.
 

The purpose is not making data any efficient, I only hate to waste every single bit on PICs because they have limited memory and any bit save can be a good. I was only trying to create a data type that would only use 10 bits instead of using an INT data type which is 16-bits long.

EDIT
oh.. what a shame! I thought it could fit bits in less space, but I was obviously wrong. The struct actually makes the data type even larger then... I'll avoid this way.
Thank you for your time guys
 

1 and 0 is also stored as a byte i.e., bit variable also takes 1 byte in memory. 0 is stored as 00000000.
That's the basic way. As said, some compilers support single bit variables that are packed to memory. E.g. CCS int1 type.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…