?
Solved

Writing 9 bit value to a file

Posted on 2003-02-24
23
Medium Priority
?
302 Views
Last Modified: 2010-04-15
How do u write a 9 bit value to a file? Could someone help me with explanation and psuedocode or code. Thank you. I am using FILE* for file.
0
Comment
Question by:us19150
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +3
23 Comments
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8011908
If you're not too concerned about the size of your file,
you can convert the 9-bit value to 16 bits and write the
2 bytes.

If you want to 'pack' the file as tightly as possible with
a series of 9-bit values, then you would write the most-significant 8 bits of the first value to your file, and
wait to write the least-significant bit until you had
another 9-bit quantity to write.  You would then write
a byte to the file that consists of the 1 bit saved from
before, followed by the 7 most-significant bits of this
subsequen byte.  Then you wait to write the remaining
2 bits, and so on and so on.  Don't forget that before
closing the file you ensure that you write any bits
that haven't been written.  You can simply bad this final
bit with 0's in order to get a full 8-bit quantity.

Hope this helps.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8013565
here is how u can have 9 bit values

typedef struct _bitval{
unsigned int ninebits:9;// this will have values from 0 to 511
}bitval;
if u allocate the struct like this

bitval val;
actual size will be one word / double word.. depends on allocation..  with me it was 4 bytes= 2 words..
u can use it like this

val.ninebits=511;
if u write in the file like normal struct .. u'll see 4 byts written in it .. but when u read the data back to variable of above type .. and then u can retrive the values like val.ninebits.. and it will be in the range 0-511

i hope u know how to read /write structs
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8013881
if i didnt tell u the meaning of declaration
unsigned int x:9;
this means x will use the 9 least significant digits of the 4 byte int x.
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:ssnkumar
ID: 8014188
>unsigned int x:9;
This is not in ANSI!!!
0
 
LVL 1

Expert Comment

by:smkstk
ID: 8014311
Actually ....

struct {
   unsigned int is_keyword : 1;
   unsigned int is_extern  : 1;
   unsigned int is_static  : 1;
} flags;

is actually ANSI C.

you must wrap anything that you do this to in a struct
the important thing to not is that almost everything about bit fields is implementation dependant. Including what bits of the item are set for a particular field....

Also not that although the above struct only defines 3 bits out of the presumably 32 bits in an unsigned int the structure will take up 32bits... there are many other limitations as well...

See "The C Programming Language" Second edition by Kernighan and Ritchie for more information.


0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8014361
First of all it's not possible to use a 9-bit data in a machine. You may get a compiler which can store 9-bits, but internally it is storing the data as 16-bit. That means it is simulating the process. To help you out I developed a module which does the same work of simulation. Here is the code below:

#include <stdio.h>
#include <conio.h>

#define BIT_9 unsigned short

void Write9Bit(BIT_9 data, FILE *fpt)
{
     fwrite(&data,sizeof(BIT_9),1,fpt);
}

void Read9Bit(BIT_9 *data, FILE *fpt)
{
     fread(data,sizeof(BIT_9),1,fpt);
}

void main()
{
     BIT_9 data = 67;
     FILE *fpt = fopen("test.dat","w+b");
     Write9Bit(data,fpt);
     fclose(fpt);

        data = 0;
     FILE *fpt = fopen("test.dat","r+b");
     Read9Bit(&data,fpt);
     printf("%d",data);
     fclose(fpt);
}

The two functions Read9Bit and Write9Bit can be used by you anywhere in the program. If you need further help, please consult me in my id rajeev_devin@hotmail.com

Thank you
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8014371
us19150,
I think it will be better, if you can explain your problem, the type of input you are giving and the expected output by giving some example. Then we can pinpoint the problem and give more useful/efficient solutions....

-Narendra
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8014377
i havent learned ANSI by heart .. but most of the compilers i have worked with never complained about  bitfields..
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8014405
What about the famous gcc!? I am sure it will give error!
If you use it inside a structure then it's ok. If you are directly using it as a int declaration, it's not allowed.

-Narendra
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8014437
i dont know who wud like to use it outside the structure .. i dont see any use for that
any ways
again, for obvious reasons, having one struct containing only one bitfield finds no use .. it was just for example of bitfields that,
 i made a structure with only one bitfield.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8014480
Sorry then. I misunderstood your statement:
>unsigned int x:9;
Now I see that it's inside a structure...:-) Then it will work and it is ANSI compliant....!!!:-))

-Narendra
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8014589
thats great .. guess i have learnt art of creating illusions in programming j/k :P
0
 

Author Comment

by:us19150
ID: 8017463
thanks a lot guys...i am getting there..but i have some questions..i wrote the following small experimental program
typedef struct _bitval
{
unsigned int ninebits:9;//values from 0 to 511
} bitval;

int main( void )
{
     FILE* file;
     file = fopen( "c:\\test.dat", "w+");
     bitval val;
     int size = sizeof( val );//size is 4 bytes.
     val.ninebits = 511;
     putc(val.ninebits, file );
     val.ninebits =      getc( file);
     fclose( file );
}
Ok questions:
1)when putc() writes val.ninebits to the file,,i opened test.dat in HexEdit,,it is writing is FF CD ( 255 205)
why is it doing that?
2) When i use val.ninebits = getc(),, val.ninebits is all of the sudden 205..instead of 511. What is happening.
Thanks.
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8017588
Since you're clearly not concerned about how much
space is taken in the file by the storing of your
"9-bit" quantity, why do you care that it's a 9-bit
quantity.  Make val an integer.  Write the integer
to the file, read an integer from the file.  I assume
your application only uses values 0-511, so the values
you read/write to/from the file will also be 0-511.
Who cares that only 9 of the bits might be non-zero?

By the way, your getc() doesn't match because when you
write to a file, the file pointer gets updated.  When
you do the getc(), the file pointer is no longer pointing
to the place where 511 was written.  Try the following.

int main( void )
{
  FILE    *file;
  integer val;
 
  file = fopen( "c:\\test.dat", "w" );

  val = 511;

  putc( val, file );

  fclose( file );

  file = fopen( "c:\\test.dat", "r" );

  val = getc( file );

  printf( "val %d\n", val );

  fclose( file );
}
0
 

Author Comment

by:us19150
ID: 8017704
honey hamster,,,that is what i did not fathom.
1) why is it writing a binary value or not text value of 511 into the test.dat file? u are not using "wb"
2) It is writing Hex FF ( dec 255 ) instead of 511 decimal.?? since u assinged val as 511.
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8018050
Sorry, I shouldn't just have copied your code.

putc() writes a BYTE to the file, i.e. only 8 bits.

You should use fwrite() to write to the file, fread()
to read.

Also, when you look at the file using HexEdit, you need
to remember that you can't fit 511 in 8 bits, so you
won't see the value 511 anywhere in the file.  Since
511 is 0x1FF, you will see that 255 is one of the bytes
in the file.

So here goes.  Note that I've changed val from an int
to a short.  No point storing 4 bytes, when you can get
away with 2 bytes.

int main( void )
{
 FILE  *file;
 short val;
 
 file = fopen( "c:\\test.dat", "w" );

 val = 511;

 fwrite( &val, sizeof( val ), 1, file );

 fclose( file );

 file = fopen( "c:\\test.dat", "r" );

 fread( &val, sizeof( val ), 1, file );

 printf( "val %d\n", val );

 fclose( file );

 return( 0 );
}
0
 

Author Comment

by:us19150
ID: 8019086
honey hamster,
I found the following code to output 9 bit to a file. I think the code uses similiar idea of wrapping left over bits to next bytes.

#define BITS 12
output_code(FILE *output,unsigned int code)
{
static int output_bit_count=0;
static unsigned long output_bit_buffer=0L;

//What is going on this code here. Can u explain.
 output_bit_buffer |= (unsigned long) code << (32-BITS-   output_bit_count);

  output_bit_count += BITS;
  while (output_bit_count >= 8)
  {
   //Also why is he ouputting with >>24???
    putc(output_bit_buffer >> 24,output);
    output_bit_buffer <<= 8;
    output_bit_count -= 8;
  }
}

0
 

Author Comment

by:us19150
ID: 8019355
On this line of code
output_bit_buffer |= (unsigned long) code << (32-BITS-output_bit_count);
  88<<20
|=92274688
basically he is shifting 20 bits, and then that makes output_bit_count 92274688. but why is he shifting 20 bits...but not 22 or ....
0
 
LVL 2

Accepted Solution

by:
honey_hamster earned 120 total points
ID: 8019374
us19150,

I'll try and show what the code does with an example,
and I'll use the case shown with BITS = 12.

Let's see what happens for the following (note that
all 'code' values fit in 12 bits:
output_code( file, 0x0ab );
output_code( file, 0x543 );
output_code( file, 0x011 );
output_code( file, 0x987 );

You would hope to see the following bytes in the file:
0x0a 0xb5 0x43 0x10 0x19 0x87.

And you can see that some of the bytes, i.e. 0xb5 and
0x19, contain bits from different calls to ouput_code().
In other words, when the first call to output_code()
returns, only the 0x0a has been written, and the 0x0b
is 'remembered' by the subroutine in the following way:
output_bit_count has the value 4
output bit_buffer has the value 0xb0000000

Lets see what happens when output_code( file, 0x543 )
is then called.

After the line
output_bit_buffer |= code << (32 - BITS - bit_count);
has been executed,
output_bit_buffer has the value 0xb5430000, i.e.
0xb0000000 | 0x05430000.

After output_bit_count += BITS;,
output_bit_count has the value 16

Now we know that we want to write 0xb5 to the file,
and we get that by shifting the 32-bit integer 24 bits
to the right inside the putc() call.  Remember that
putc() writes 8 bits at a time.  And 24 is used because
on the CPU in question, sizeof( long ) is assumed to
be 4 bytes, i.e. 32 bits.

After output_bit_buffer << = 8;,
output_bit_buffer has the value 0x43.

And after output_bit_count -=8;,
output_bit_count has the value 8.

Next time throught the loop, putc() is called with
0x43 as parameter, output_bit_buffer ends up being 0,
and output_bit_count ends up being 0.

Is this clear?
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8022417
putc cannot write the 9 bit value.. and getc cannot read 9 bit value
getc and putc do with single bytes .. (8 bits).

since u r using the struct .. so what u shud do is
fwrite the struct and read the struct like this
and in this case sizeof struct is 4 bytes .. so u'll see for bytes in test.dat

....................
#include <stdio.h>

typedef struct _bitval
{
unsigned int ninebits:9;//values from 0 to 511
} bitval;

int main( void )
{
    FILE* file;
    bitval val,readval;
    int size = sizeof( val );//size is 4 bytes.
    val.ninebits = 511;
    //val.ninebits = 513; // if u do this then value will be 513-512= 1  rounded on 512 and 512 will give u 0
    file = fopen( "c:\\test.dat", "w+");
fwrite(&val,size,1,file);
fclose(file);

    file = fopen( "c:\\test.dat", "r+");

fread(&readval,size,1,file);

printf("read value is %d\n",readval.ninebits);
    fclose( file );
}

test.dat will look like this
FF 01 00 00
0
 

Author Comment

by:us19150
ID: 8033852
thanks a lot.
Credits go to all of u..mostly hamster( do u mind if i only call u hamster?:) and akshayxx?
So i want to split 30 in half and credit hamster and akshayxx. or shall i just credit 30 to one of u guys?( since it is only petty 30).what do u think?
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8039310
if u wanna distribute u can ask some EE moderator on comunity support..
and since its only petty 30 ..
see how many characters in ur original question ..
if its ODD give me the points .. else if its EVEN
give to honey..
dont thingk i tried tocount .. to make it biased :)
0
 

Author Comment

by:us19150
ID: 8041802
int len = strlen( "How do u write a 9 bit value to a file? Could someone help me with explanation and psuedocode or code. Thank you. I am using FILE* for file.");
 

Well,,,it returned 140. i guess i am gonna award hamster.:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question