10 Replies - 27342 Views - Last Post: 02 September 2006 - 06:06 AM Rate Topic: -----

#1 jabez_jb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-August 06

how to concatenate bits in c++

Post icon  Posted 31 August 2006 - 08:01 PM

can you tell me how to concatanate bits in c++??
Is This A Good Question/Topic? 0
  • +

Replies To: how to concatenate bits in c++

#2 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

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...

This post has been edited by violent_crimson: 31 August 2006 - 09:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1813
  • View blog
  • Posts: 20,232
  • Joined: 17-March 01

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.
Was This Post Helpful? 0
  • +
  • -

#4 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

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...

This post has been edited by violent_crimson: 31 August 2006 - 09:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1813
  • View blog
  • Posts: 20,232
  • Joined: 17-March 01

Re: how to concatenate bits in c++

Posted 31 August 2006 - 09:45 PM

View Postviolent_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.
Was This Post Helpful? 0
  • +
  • -

#6 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

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++;

#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

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

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
Was This Post Helpful? 0
  • +
  • -

#8 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 175
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

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 :)
Was This Post Helpful? 0
  • +
  • -

#9 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

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
Was This Post Helpful? 0
  • +
  • -

#10 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 175
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

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.
Was This Post Helpful? 0
  • +
  • -

#11 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

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 :)

This post has been edited by violent_crimson: 02 September 2006 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1