Archived

This topic is now archived and is closed to further replies.

How to store 4bit data types in a file

This topic is 5902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I get a data type with 4 bits of size? I need it for Windows Bitmap (DIB) manipulation. If the Bitmap is in 4 bit per pixel format, (that makes for a palette of 16 colors) I need to get a pointer to the bitmap bits which is 4 bits wide. But the smallest I can get is 8 bits (by using an unsigned char). Simply trimming the value to a max of 0xFF won´t cut it, because the type pointed to by my surface pointer is still 8 bits wide and the next 4 bits are filled with zeroes. How would you do it?

Share this post


Link to post
Share on other sites
I''m not exactly sure what you''re trying to do, but you can''t access 4 bits directly. You have to get 8 bits then mask off the unwanted part.

lower 4 bits = 8bits & 0xF
upper 4 bits = (8bits & 0xF0)>>4

That''s very quick off the top of my head. Check to make sure it works ok.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
typedef struct FOURBITS
{
char data :4;
}FOURBITS;

struct FOURBITS data;

then to access

data.data = 15;

that should work, been a while since ive used bit fields

Share this post


Link to post
Share on other sites
First off, thank you for your replies!

Thrump: I had already thought of something like this, but I hadn´t yet grasped bitwise operations in C that well. But your tip made it all go *click* inside my head and it works now as intended!

I´ve come up with a bitfield packed in a struct as well, but I couldn´t get a pointer to the wrapped bitfield in it (only to the structure as a whole, which wasn´t of any use).

Share this post


Link to post
Share on other sites
First off, thank you for your replies!

Thrump: I had already thought of something like this, but I hadn´t yet grasped bitwise operations in C that well. But your tip made it all go *click* inside my head and it works now as intended!

I´ve come up with a bitfield packed in a struct as well, but I couldn´t get a pointer to the wrapped bitfield in it (only to the structure as a whole, which wasn´t of any use).

Share this post


Link to post
Share on other sites