Connecting Tech Pros Worldwide Forums | Help | Site Map

filling up of a frame with 10bit words c++

Newbie
 
Join Date: Mar 2006
Posts: 1
#1: Mar 27 '06
consider a frame has 720 words , each word is of 9 bits

how can i fill up my frame of 720 words with numbers starting from 0 to 720.

if we store this frame in a file then size taken by this frame should not be more than 810 bytes.

i hav to fill up the frame word by word.
note:-will BitArray or Bitfield help here if yes then how

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#2: Mar 27 '06

re: filling up of a frame with 10bit words c++


I assume the title of this thread is an error?

I assume that you have noticed that 810 8 bit words (or bytes) contain exactly the same number of data bits as 720 bit words, i.e. you data must not have any padding bits.

Anyway bit fields will not help, if you consider this structure

Expand|Select|Wrap|Line Numbers
  1. typedef struct word_9bit
  2.   {
  3.     unsigned int bit_0:1;
  4.     unsigned int bit_1:1;
  5.     unsigned int bit_2:1;
  6.     unsigned int bit_3:1;
  7.     unsigned int bit_4:1;
  8.     unsigned int bit_5:1;
  9.     unsigned int bit_6:1;
  10.     unsigned int bit_7:1;
  11.     unsigned int bit_8:1;
  12. } WORD_9BIT
This would hold a 9 bit word, however if you were to take the size of this structure you would find it was 2 bytes, there are 7 padding bits at the end of the structure. Most c compilers would place in memory 2 byte aligned, i.e. at a even byte boundary.

What this means is that it you had an array of these

WORD_9BIT array[2];

That there will be 7 padding bits between the data for the 2 structures, as stated above you don't want padding bits.

If you put a 9 bit word in an array of bytes (8 bit words) then you will always be crossing 1 and only 1 byte boundary (this is not true for higher bit values, for instance a 10 bit word could cross 1 or 2 byte boundaries).

So to put the 9 bit word into the byte array you will have to do a shift, mask and or operation for the high and low bytes, for a given 9 bit word in an array of 9 bit words it would be relatively easy to calculate these byte offsets and shif and mask values.

In light of this I think my approach would be to create a 9 bit word array class and then overload the array operator [] to allow you to treat your array of 9 bit words exactly like a normal array of words but resulting in an array of bytes containing the actual data.
Reply