Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Storing / Inserting Data per BIT
Storing / Inserting Data per BIT [message #36059] Sun, 22 April 2012 16:02 Go to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
Hi,

got a little question:

I try to get data stored as bit formats, need to store it in this way:

<9 bit empty><10 bit as a number 0-1023><10 bit as a number 0-1023><....>
I need 16 Byte in complete... just told you the first 29 BIT.

But i don't know how to realize that.

Thanks for help!
Re: Storing / Inserting Data per BIT [message #36060 is a reply to message #36059] Sun, 22 April 2012 17:02 Go to previous messageGo to next message
Lance is currently offline  Lance
Messages: 526
Registered: March 2007
Contributor
check bit-wise operation

you will need
bit or |
bit and &
bit negation ~
and bit shift <<, >>

eg, if you want to set 2 most significant bits of a int16 without changing other bits, you can:

void test_bit()
{
      uint16 target=0;
      DUMP(target);

      // set 2 most significant bits
      
      target |= ((uint16)0x3 << 14);
      // 0x3=0000 0000 0000 0011b right shift by 14 bits becomes 1100 0000 0000 0000b
      // or 0xC000
      DUMP(target);

      // and set the 3 LSBs
      target |= 0x7;
      DUMP(target);

      // if you instead want to unset the 2 least significant bits
      // you can do something like:
      target &= ~0x3;
      // 0x3 is 0000 0000 0000 0011b
      // ~0x3 becomes 1111 1111 1111 1100b
      // bitwise-and this number with target result in the
      // 2 LSB being unset.
      DUMP(target)  
}

[Updated on: Sun, 22 April 2012 17:06]

Report message to a moderator

Re: Storing / Inserting Data per BIT [message #36061 is a reply to message #36060] Sun, 22 April 2012 17:12 Go to previous messageGo to next message
Lance is currently offline  Lance
Messages: 526
Registered: March 2007
Contributor
Depending on your situation, you may find bit-field more handy in your case:

//<9 bit empty><10 bit as a number 0-1023><10 bit as a number 0-1023><....>
//I need 16 Byte in complete... just told you the first 29 BIT.

struct MyData
{
    unsigned dummy:9;
    unsigned number1:10;
    unsigned number2:10;
    //....
};


Then you can modify each field as you do a normal variable, and load/store the whole 16 bytes together.
Re: Storing / Inserting Data per BIT [message #36069 is a reply to message #36061] Mon, 23 April 2012 21:00 Go to previous message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
Thank you very much for help, I've done it by bit-wise operations...

just if someone wants to see the code:
struct canFrame
{
    unsigned int sAdress:10;
    unsigned int dAdress:10;
    char buffer[16];
    
    char* getBuffer() {
	return buffer;
    }
    void setAdress(const int& s, const int& d) {
	sAdress = s;
	dAdress = d;
	buffer[0] = sAdress;
	buffer[1] = (dAdress << 2);
	buffer[1] += sAdress/256;
	buffer[2] = (dAdress >> 6);
    }
    String getAdresses() {
	return (String)AsString(sAdress) + " :: " + AsString(dAdress);
    }
    canFrame() {
	sAdress = 0b0000000000;
	dAdress = 0b0000000000;
	for (int i=0;i<16;i++)
	    buffer[i] = 0x0;
    }
};


Hope this code is "ok"
Previous Topic: How to get pixels per millimeter for current screen?
Next Topic: How to stop SetTimeCallback
Goto Forum:
  


Current Time: Thu Mar 28 15:15:17 CET 2024

Total time taken to generate the page: 0.01468 seconds