can you tell me how to concatanate bits in c++??
how to concatenate bits in c++
Page 1 of 110 Replies - 27342 Views - Last Post: 02 September 2006 - 06:06 AM
Replies To: how to concatenate bits in c++
#2
Re: how to concatenate bits in c++
Posted 31 August 2006 - 09:35 PM
Buffer: 00000000
Current Sequence: 00000110
New Bits To Add: 0010
Buffer = 00000000 ^ Current Sequence
Buffer == 00000110
Buffer = Buffer << 4
Buffer == 01100000
Buffer = Buffer ^ New Bits To Add
Buffer == 01100010
Output Sequence = Buffer...
Current Sequence: 00000110
New Bits To Add: 0010
Buffer = 00000000 ^ Current Sequence
Buffer == 00000110
Buffer = Buffer << 4
Buffer == 01100000
Buffer = Buffer ^ New Bits To Add
Buffer == 01100010
Output Sequence = Buffer...
This post has been edited by violent_crimson: 31 August 2006 - 09:39 PM
#3
Re: how to concatenate bits in c++
Posted 31 August 2006 - 09:39 PM
Violent_crimson, thanks for helping out jabez_jb!
We do normally require some effort on behalf of the original poster (code, a description of the problem they are having, etc.) to prevent cheating or simply copying. We pride ourselves on being a teaching forum.
Best of luck with your project jabez_jb, and thanks again for helping violent_crimson.
We do normally require some effort on behalf of the original poster (code, a description of the problem they are having, etc.) to prevent cheating or simply copying. We pride ourselves on being a teaching forum.
Best of luck with your project jabez_jb, and thanks again for helping violent_crimson.
#4
Re: how to concatenate bits in c++
Posted 31 August 2006 - 09:43 PM
sorry about the under explination, im at school and i dont have time to write up c++ source...
if i were to write anything right now without my IDE, i would probably be giving incorrect source which wouldn't help at all...
ill be home in an hour...
if i were to write anything right now without my IDE, i would probably be giving incorrect source which wouldn't help at all...
ill be home in an hour...
This post has been edited by violent_crimson: 31 August 2006 - 09:47 PM
#5
Re: how to concatenate bits in c++
Posted 31 August 2006 - 09:45 PM
violent_crimson, on 31 Aug, 2006 - 10:43 PM, said:
sorry about the under explination, im at school and i dont have time to write up c++ source...
No worries mate, you didn't do anything wrong, we just typically ask the original poster to show a little effort when asking a question like that, keeps people honest, your response was just fine.
#6
Re: how to concatenate bits in c++
Posted 31 August 2006 - 11:18 PM
yes, well im sorry if you don't understand this code, but skyhawk133 is extremely right, if you don't explain your problem in detail, you wont get the exact help you require;
i hope the following code helps, i've excessively added comments to help even beginners to c++;
this example was created by me in visual c++ 2005 professional!
i hope the following code helps, i've excessively added comments to help even beginners to c++;
#include <iostream>
using namespace std;
int main ()
{
//buffer = 00000000;
//any 0 before a binary sequence will cancel itself out;
//therefore buffer is really equal to 0;
//in this case, i've kept the number of bits in the sequence constant to one byte;
//a byte is approximately 8 bits;
//buffer = 0;
int nBuffer = 0x00;
//sequence = 0x06 which is really equal to 0110, but the leading 0 cancels out, so this becomes 110;
int nSequence = 0x06;
//buffer, which is equal to 0, gets assigned, 0 XOR 0x06, which is really;
// 00000000;
//^00000110;
//=00000110;
//if you don't understand the XOR (^) operator, you can find information about it by looking it up in google;
nBuffer = nBuffer ^ nSequence;
//we will let c++ output the result in decimal;
//output, nBuffer which is equal to nBuffer = nBuffer ^ nSequence; which equals 00000110;
cout << nBuffer << "\t: Buffer == 00000110;" << endl;
//shift the sequence 4 bits to the left, c++ will call in 4 trailing bits set to 0;
nBuffer = nBuffer << 4;
//buffer (00000110) = buffer (00000110) << 4 (01100000);
//output the new result in nBuffer;
//which is the return value of the top equation;
//(nBuffer = nBuffer << 4;);
cout << nBuffer << "\t: Buffer == 01100000;" << endl;
//now, we wanted to concatenate (add to the end of) 0010 to our value;
//so after we have shifted the value enough bits to the left, (exactly enough to fit in the new value),
//we can XOR our current nBuffer with the new value;
//01100000 = 01100000 ^ 00000010;
//which is in turn 01100010;
nBuffer = nBuffer ^ 0x02;
cout << nBuffer << "\t: Buffer == 01100010;" << endl;
cout << endl;
system ("PAUSE");
return 0;
}
this example was created by me in visual c++ 2005 professional!
This post has been edited by violent_crimson: 31 August 2006 - 11:22 PM
#7
Re: how to concatenate bits in c++
Posted 31 August 2006 - 11:46 PM
As violent_crimson has shown in the code example provided you need to use bitwise operators in order to manipulate bits in C++.
Here are a couple of tutorials that will hopefully give you a better understanding of how to use them.
http://www.informit....p...um=163&rl=1
http://www.cprogramm..._operators.html
Here are a couple of tutorials that will hopefully give you a better understanding of how to use them.
http://www.informit....p...um=163&rl=1
http://www.cprogramm..._operators.html
#8
Re: how to concatenate bits in c++
Posted 01 September 2006 - 06:29 AM
violent_crimson, great job....but you forgot the OR bitwise operator
#9
Re: how to concatenate bits in c++
Posted 01 September 2006 - 11:27 PM
well, XOR is my personal preference for such a situation as it would virtually give the same result as an OR operation
0000 ^ 0101 = 0101
0000 | 0101 = 0101
0000 ^ 0101 = 0101
0000 | 0101 = 0101
#10
Re: how to concatenate bits in c++
Posted 01 September 2006 - 11:55 PM
But if the source bit has already been set, XOR would erase it.
#11
Re: how to concatenate bits in c++
Posted 02 September 2006 - 06:06 AM
hmm - that's correct, well in my above example i was expecting the buffer to be set to 0, which would give XOR the same effect as OR
or maybe i didn't understand what you said - because right now i have a headache
or maybe i didn't understand what you said - because right now i have a headache
This post has been edited by violent_crimson: 02 September 2006 - 06:10 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|