Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BITS & BYTES???

Status
Not open for further replies.

benlogan1981

Programmer
Dec 9, 2001
14
GB
OK, I'm having a few difficulties here. New to C++!
I have an input file of 0s and 1s, which are meant to represent bits. I want to read 10 of these characters in and create a 10 bit byte with them. Then I store this byte into an array to represent memory[256]. And then read another 10 'bits' from the file, etc
This is what I have, but when I try to progress from here I just mess it up...

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>

char *infile = &quot;data.txt&quot;;

int main()
{
ifstream iFile(infile, ios::in);
char memory[256];
char byte[10];
//read operation - for cycling through the bytes
//for(int i = 0; i < 16; i++) { //loop 16 bytes
//iFile >> memory; //from file, shouldnt be !!!
//}
//read operation - leave when input buffer is full

//read operation - bits for creating the bytes
for(int d = 0; d < 10; d++) {//loop 10 bits in bytes
iFile >> byte[d]; //from file
}
//read operation - leave when input buffer is full

for(int e = 0; e < 10; e++) {
cout << byte[e];
}//displaying a byte

//transfer contents of full input to the output
int x = 0;
for(int j = 16; j < 32; j++) {
memory[j] = memory[x];
memory[x] = 0;//clearing the input buffer
x++;
}

//write operation - to screen or file
for(int a = 0; a < 16; a++){
cout << &quot;Input Buffer: &quot; << memory[a];
cout << &quot; Held at LOC: &quot; << a << endl;
}
for(int b = 16; b < 32; b++){
cout << &quot;Output Buffer: &quot; << memory;
cout << &quot; Held at LOC: &quot; << b << endl;
}
//write operation
return 0;
}
 
Hi,

Ok, I'll try to help a little but I'm no authority on this so I hope someone else responds as well.

A bit is represented by a &quot;0&quot; or a &quot;1&quot; in memory. A char is represented by one byte. A one byte is 8 bits, (generally -it depends on the system you are using) so using a value like this:

char byte[10];

...to read in each bit won't cut it. Instead, it reads in the size of one char on each pass through the loop. So

byte[0]

will hold the first char read from the file, which will actually be 8 bits of data. That would be incorrect for your example. What you need to do is read in one bit at a time or ten bits at a time to produce the results you need.

In 'C' you can use a structure with an unsigned int value to represent bits - called a bit field operator to represent a value that holds a certain number of bits. I'm sure you could use that here.

Hope that made sense.

-Tyler



 
If I follow correctly... you want to read in a byte and store each byte successively in the &quot;memory&quot; buffer.

First, a byte is 8 bits not 10 so here is my approach

ifstream iFile(infile,ios::in);
char memory[256];
char byte[8];

int memLoc = 0;
char* temp;
while(!iFile.eof() && iFile.peek()!= EOF)
{
iFile.getline(byte,sizeof(byte));

ASSERT(strspn(byte,&quot;01&quot;) == strlen(byte));

int value = 0;
for(int i = strlen(byte)-1,j=0;i>=0;i--,j++)
{
if(byte == '1')
{
value |= 1<<j;
}
}

memory[memLoc++]=value;
}


I think this along the lines of what you are looking for. Please let me know if I steered you down the wrong path.

Matt

 
SORRY FOR THE DOUBLE POST... forgot my code tag

If I follow correctly... you want to read in a byte and store each byte successively in the &quot;memory&quot; buffer.

First, a byte is 8 bits not 10 so here is my approach

Code:
ifstream iFile(infile,ios::in);
char memory[256];
char byte[8];

int memLoc = 0;
char* temp;
while(!iFile.eof() && iFile.peek()!= EOF)
{
   iFile.getline(byte,sizeof(byte));

   ASSERT(strspn(byte,&quot;01&quot;) == strlen(byte));
   
   int value = 0;
   for(int i = strlen(byte)-1,j=0;i>=0;i--,j++)
   {
      if(byte[i] == '1')
      { 
          value |= 1<<j;
      }
   }

   memory[memLoc++]=value;
}


I think this along the lines of what you are looking for. Please let me know if I steered you down the wrong path.

Matt

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top