Creating a Bitmap?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Creating a Bitmap?

Post by pcmattman »

Can anyone help me out? I need to somehow put the top 4 bits of an unsigned char into bits 8-11 of a short, and the bottom 4 into bits 0-3.

I can get the bits out of the char, but I don't know how to put them into the short.
pjt
Posts: 6
Joined: Thu Nov 23, 2006 10:46 am

Re: Creating a Bitmap?

Post by pjt »

pcmattman wrote:Can anyone help me out? I need to somehow put the top 4 bits of an unsigned char into bits 8-11 of a short, and the bottom 4 into bits 0-3.

I can get the bits out of the char, but I don't know how to put them into the short.
First step: s |= ((short)(uc & 0xf0)) << 4
Second step: s|= (uc & 0xf)

Cheers.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Hmmm... still doesn't get what I need.

I need to create a bit field that I can use in the EEPROM command register (3Com 3C90xC) from a unsigned char for the register and opcode. And it needs to be a short.
User avatar
omin0us
Member
Member
Posts: 49
Joined: Tue Oct 17, 2006 6:53 pm
Location: Los Angeles, CA
Contact:

Post by omin0us »

I dont see why pjt's method wont work for you?
but you can also create a struct that uses only certain amounts of bits for each member of the struct.

Code: Select all

struct shortVariable
{
        unsigned bits0to3 : 4;
        unsigned bits4to7 : 4;
        unsigned bits8to11 : 4;
        unsigned bits12-15 : 4;
};

struct shortVariable sv;
unsigned char data;
...

sv.bits0to3 = data & 0x0f;
sv.bits8to11 = (data & 0xf0) << 4;
and then you can cast the struct to a short. I dunno if the struct members will get aligned in memory wierd with that struct. I wouldn't think so, since it is 2 bytes even. But if it does, you can always use __attribute__((packed)) with struct definition.
http://ominos.sourceforge.net - my kernel
#ominos @ irc.freenode.net
http://dtors.ath.cx - my blog
pjt
Posts: 6
Joined: Thu Nov 23, 2006 10:46 am

Post by pjt »

Just one nitpick:

Code: Select all

sv.bits8to11 = (data & 0xf0) >> 4; 
For creating a short from a stuct the easiest way is to put them both in a union.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I'll have to try that. Thanks for your help, I tried the struct but forgot about the packed attribute :shock: ...
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

I have created this template class for this purpose.

Code: Select all

//-[ BitField<T> ]--------------------------------------------------------------
template <typename T>
class                   BitField {
    T                       bits;

  public:
    BitField(void) : bits(0) {}
    BitField(T data) : bits(data) {}
    BitField(voidp addr) : bits(*(T *)addr) {}

    T                       get(int16u, int16u);
    void                    set(int16u, int16u, T);

} __attribute__((packed));
//--------------------------------------------------------------[ BitField<T> ]-


//-[ BitField<T>::get ]---------------------------------------------------------
template <class T> T
  BitField<T>::get
  ( int16u                  base,                    //- first bit of data field
    int16u                  size                     //- number of bits to fetch
  )
{   const T                 mask = (((T)1 << size) - (T)1);
    return((bits >> base) & mask);
}
//---------------------------------------------------------[ BitField<T>::get ]-


//-[ BitField<T>::set ]---------------------------------------------------------
template <class T> void
  BitField<T>::set
  ( int16u                  base,                    //- first bit of data field
    int16u                  size,                    //- number of bits to store
    T                       data                     //- value to store in field
  )
{   const T                 mask = (((T)1 << size) - (T)1) << base;
    bits = (bits & ~mask) | ((data << base) & mask);
}
//---------------------------------------------------------[ BitField<T>::set ]-
Can anyone help me out? I need to somehow put the top 4 bits of an unsigned char into bits 8-11 of a short, and the bottom 4 into bits 0-3.

I can get the bits out of the char, but I don't know how to put them into the short.
You can use it like:

Code: Select all

BitField<char>    byte(/*value*/);
BitField<short>    word(/*value*/);

word.set(8, 4/*bit 8-11*/, byte.get(0,4/*bit 0-3*/));
Author of COBOS
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Maybe I should switch to C++ as my OSdevving langauge. It looks so much easier to work with... Classes are my thing :D

Edit: got it... I just had to put the struct into a union with an unsigned short and it now works perfectly. Thankyou everyone :D
Post Reply