Am I implimenting bit insertion correctly? Because it dosn't work.
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.
If MyByte doesn''t equal 00110110, then what does it equal? That should work perfectly.
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
#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
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?
And cloxs, isn''t that basically what I am doing?
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;
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.
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:
Edited by - Midnight Coder on November 4, 2001 9:00:04 PM
32 | 22:
00100000 00010110 ________=00110110
Edited by - Midnight Coder on November 4, 2001 9:00:04 PM
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:
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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement