#### Archived

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

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

This topic is 6074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
What do you get as output then?

##### Share on other sites
If MyByte doesn''t equal 00110110, then what does it equal? That should work perfectly.

##### Share on other sites
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 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 on other sites
Convert binary string to binary number? Try this:
  char bitString[] = "100000001";int value=0;for (int i = 0; i

##### Share on other sites
Thank you,invective. That is so much easier then what I was attempting. I am still open to suggestions as to what went wrong, however.

##### Share on other sites
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 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 on other sites

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

___________________________________

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

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 32
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631409
• Total Posts
2999929
×