1. ## bitwise operations

I am trying to use bitwise operators to fill a unsigned char with data. the data i want to put in is the binary number 0 0 0 1 0 1 1 1
However the data comes in backwards so when reading the data i get 1 1 1 0 1 0 0 0
i currently have a for loop like so
Code:
unsigned char achar
for ( int n = 0; n < 8; n++)
{
Obtain next Bit;
achar =
}
i cant figure out what to put after the equals as the data comes back the wrong way around, also it comes in one at a time, i've tried all sorts, shifts etc. I think i need to use an or, but cant seem to implement it :S
cheers for any help in advance

2. If you're getting it the wrong way around, just change the limits of your loop:

for ( int n=7; n>=0; n-- )

and do the same.

3. but how would i go about changing 8 individual binary bits into a combined 8 bit char ?

4. So, you have a char and you want to get its binary values or something?

5. nah, the other way around, i have a binary value, which is the binary of a text file, i would like to pop this into an unsigned char array, im then going to output the results as a text file.

6. Code:
unsigned char achar = 0;
for(i=0;i<8;++i){
Obtain next Bit;
if(nextBit==1)
++achar ;//there are a number of ways to implement this line.
achar <<=1;
}
That's one of many ways of doing it.
Make sure you understand what's going on.

7. Code:
#include <bitset>
#include <iostream>
#include <climits>

int main()
{
unsigned long x = 0;
x |= (1<<3);
x |= (1<<4);
std::cout << x <<std::endl;

std::bitset<CHAR_BIT * sizeof x> bs;
bs[3] = 1;
bs[4] = 1;
x = bs.to_ulong();
std::cout << x <<std::endl;
}

8. Code:
buffer[n] |= BinNumber << i;
where buffer[n] is a binary version of a text file.

This line of code seemed to do the trick, i do get some problems though, ocassionally when writing the binary back to a "text" file certain characters and positions seem to just terminate the output for now reason o.o

the entire loop to extract the binary from a image looks as follows
Code:
for (int y=0; y < Dest->Picture->Bitmap->Height; y++)
{
for (int x=0; x < Dest->Picture->Bitmap->Width; x++)
{
if( x == RandX[PixelIndex] && y == RandY[PixelIndex] )
{
Pixel.Colours = Dest->Canvas->Pixels[x][y];
ColorToRGB(Pixel.Colours);

CurrentColour = GetRValue(Pixel.Channel.Red);

BinNumber = (CurrentColour >> 0) & 1;
buffer[n] |= BinNumber << i;

PixelIndex++;

x = 0;
y = 0;
i++;
if( i > 7 )
{
buffer[n + 1] = 0;
n++;
i = 0;
}
}
}
}