bitwise operations

This is a discussion on bitwise operations within the C++ Programming forums, part of the General Programming Boards category; I am trying to use bitwise operators to fill a unsigned char with data. the data i want to put ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    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. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    but how would i go about changing 8 individual binary bits into a combined 8 bit char ?

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    So, you have a char and you want to get its binary values or something?

    Have you looked into the <bitset> library? Could help you.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    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. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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.
    Last edited by King Mir; 03-23-2007 at 02:56 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    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;
    				}
    			}
    		}
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operations to Read ON/OFF Bits
    By pseudonoma in forum C Programming
    Replies: 4
    Last Post: 02-25-2008, 02:15 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. Bitwise operations
    By sh3rpa in forum C++ Programming
    Replies: 16
    Last Post: 09-25-2007, 06:32 PM
  4. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 04:48 AM
  5. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-27-2002, 11:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21