Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,396 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,112 people online right now. Registration is fast and FREE... Join Now!




how to concatenate bits in c++

 
Reply to this topicStart new topic

how to concatenate bits in c++

jabez_jb
31 Aug, 2006 - 07:01 PM
Post #1

New D.I.C Head
*

Joined: 18 Aug, 2006
Posts: 10


My Contributions
can you tell me how to concatanate bits in c++??
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: How To Concatenate Bits In C++
31 Aug, 2006 - 08:35 PM
Post #2

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


My Contributions
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 Aug, 2006 - 08:39 PM
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: How To Concatenate Bits In C++
31 Aug, 2006 - 08:39 PM
Post #3

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,974



Thanked: 48 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: How To Concatenate Bits In C++
31 Aug, 2006 - 08:43 PM
Post #4

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


My Contributions
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 Aug, 2006 - 08:47 PM
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: How To Concatenate Bits In C++
31 Aug, 2006 - 08:45 PM
Post #5

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,974



Thanked: 48 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
QUOTE(violent_crimson @ 31 Aug, 2006 - 10:43 PM) *

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.
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: How To Concatenate Bits In C++
31 Aug, 2006 - 10:18 PM
Post #6

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


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

CODE

#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 Aug, 2006 - 10:22 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: How To Concatenate Bits In C++
31 Aug, 2006 - 10:46 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.com/guides/content.asp...um=163&rl=1
http://www.cprogramming.com/tutorial/bitwise_operators.html
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: How To Concatenate Bits In C++
1 Sep, 2006 - 05:29 AM
Post #8

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
violent_crimson, great job....but you forgot the OR bitwise operator smile.gif
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: How To Concatenate Bits In C++
1 Sep, 2006 - 10:27 PM
Post #9

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


My Contributions
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
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: How To Concatenate Bits In C++
1 Sep, 2006 - 10:55 PM
Post #10

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
But if the source bit has already been set, XOR would erase it.
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: How To Concatenate Bits In C++
2 Sep, 2006 - 05:06 AM
Post #11

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


My Contributions
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 smile.gif

This post has been edited by violent_crimson: 2 Sep, 2006 - 05:10 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:56AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month