Archived

This topic is now archived and is closed to further replies.

Gwahir the Windlord

Am I implimenting bit insertion correctly? Because it dosn't work.

Recommended Posts

Ok, what I am trying to do is set a certain bit of a byte. I want to be able to set the 3rd most significant byte to 1. If the byte was 00000000, I want it to be 00100000. If it was 10001100, I would want to change it to 10101100. The way I am going about it should work, according to according to Joseph Farrels article here in bitwise operations. But it dosn''t. I am basically doing this: BYTE BitMask = 32; // is 00100000 binary BYTE MyByte = 22; // is 00010110 binary MyByte = MyByte | BitMask; // MyByte should now equal 00110110 binary. //but it dosn''t. This is probably extremely obvious. But I cannot figure it out. That article explained it very well, but it failed to give an example. This is all I could come up with, but it dosn''t work. And of course, this is a simplified version of my problem. What I am really trying to do is to convert an 8 char string containing 1''s and 0''s to an 8 bit variable. I could just create a huge lookup table, but this should work, and is a much easier way to do it. If I could get it to work. If it matters, I am programming in Borland C++ 5.02 with windows 98. If you could help, I would really appreciate it.

Share this post


Link to post
Share on other sites
Cloxs    122
Hope this helps:

#include
#include

int main(int argc, char **argv)
{
char f, s, fi;
f = 1;
s = 2;
fi = f | s;
printf("%i | %i = %i", f,s,fi);

return(0);
}

Also, just as a note, if you're setting a bit to one, you need to bitwise-OR it, however if you set it to zero, you need to use bitwise-AND. Hope that helped.

Edited by - cloxs on November 4, 2001 7:06:49 PM

Share this post


Link to post
Share on other sites
I really couldn''t tell. You see, in practice the variables are different all the time. This is just an example I have made up. When I fputc() the byte, it always winds up showing as an y with two dots on top of it. I am not sure what it is called, and I couldn''t find it on my ascii tables.

And cloxs, isn''t that basically what I am doing?


Share this post


Link to post
Share on other sites
invective    118
Convert binary string to binary number? Try this:
  
char bitString[] = "100000001";
int value=0;

for (int i = 0; i<strlen (bitString)-1; i++)
{
if (bitString[i] == ''1'')
value = value | 1;
value <<= 1;
}

cout << value;

Share this post


Link to post
Share on other sites
Midnight Coder    122
That character, "ÿ" has an ASCII value of 152, or 10011000 in binary. Are you sure this is what you were getting, because that makes no sense to me.

32 | 22:

 00100000
 00010110
 ________

=00110110


Edited by - Midnight Coder on November 4, 2001 9:00:04 PM

Share this post


Link to post
Share on other sites
Ok, I rewrote all my code in a much more efficient manner. I am now having different problems, but they are related. I really cannot find out what is wrong with this:

  

void TextToBin(void)
{
const BYTE BitMask[8] = {128,64,32,16,8,4,2,1};
char c;
int i;

while(c != EOF)
{
c = fgetc(fOriginal);

if(c == EOF)
break;

// each loop converts 1 char to 8 chars

for(i = 0;i <= 7;i += 1)
{
if(c & BitMask[i])
{
fputc('1',fNew);
}
else
{
fputc('0',fNew);
}
}
}
}



This is the routine I use for translating a file into binary. The input file on my test was a plain text file containing:

abcdefghijklmnopqrstuvwxyz(return)
ABCDEFGHIJKLMNOPQRSTUVWXYZ

I am sure this is working, because looking at the output and manually converting the first few bits it does seem ok. The first 8 chars are binary for an ascii 'a', the next 8 are binary for an ascii 'b', and so on.

  

void BinToText(void)
{
char c,cr;
int i;

while(c != EOF)
{
// each loop reads 8 chars

for(i = 0;i <= 7;i += 1)
{
c = fgetc(fOriginal);
cr = 0;

if(c == EOF)
break;

if(c == '1')
{
cr = cr | 1; // 1 is decimal for 00000001

cr << 1;
}
}

fputc(cr,fNew);
}
}



This is what seems not to be working. I first ran the first function, then ran this one on the results of that. It should turn it back into readable text, but the result file is merely junk, namely:






Edited by - Gwahir the Windlord on November 5, 2001 1:41:44 PM

Share this post


Link to post
Share on other sites
bishop_pass    109
Make your BiteMask array a static global outside of your two functions. Then, replace your last lines with this:

    
if (cr == '1')
cr |= BitMask[i];


___________________________________



Edited by - bishop_pass on November 5, 2001 1:49:40 PM

Share this post


Link to post
Share on other sites
bishop_pass    109
Actually, you have a number of problems going on there. You shouldn't be initializing cr to 0 every iteration of the loop. do this:

      

int c;
int i;
char cr;
int reading;

reading = TRUE;
while (reading) {

// read 8 more chars from the file

cr = 0;
for (i = 0; i < 8; i++) {
c = fgetc (fOriginal);
if (c == EOF) {
reading = FALSE;
break;
}
else if (c == '1')
cr |= BitMask[i];
}
// only write the char if we read at least one bit

if (i > 0)
fputc (cr, fNew);
}


___________________________________



Edited by - bishop_pass on November 5, 2001 2:14:08 PM

Share this post


Link to post
Share on other sites
bishop_pass    109
Also, note that when you call fgetc(), it returns an int and you must read that int.

It must be like this:

  
int c;

c = fgetc(stream);


How else would you know it was an EOF?

Share this post


Link to post
Share on other sites
bishop_pass    109
We can also eliminate the nested loop.

            

int c;
int i;
char cr;
int b = 0;

for (i = 0, c = fgetc (fOriginal), cr = 0; c != EOF; i++) {
b = i % 8;
if (c == '1')
cr |= BitMask[b];
if (b == 7) {
fputc (cr, fNew);
cr = 0;
}
c = fgetc (fOriginal);
}
// write the last char

if (b != 0)
fputc (cr, fNew);



___________________________________



Edited by - bishop_pass on November 5, 2001 2:27:05 PM

Share this post


Link to post
Share on other sites