Thread: Bits byte

  1. #16
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    I actually did change it to this...
    Code:
    const unsigned int bit[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
    				int binValue = 0;
    		int count = 0;
    		int j = 0;
    
    		for(int k=0; k < imageLength; k++)
    		{
    			for(int i=0; i < imageWidth; i++)
    			{
    				if(j > 7) j = 0;
    				binValue = (buf[i] & bit[j]) >> j;
    				cout << binValue;
    				j++;
    			}
    			cout << endl;
    		}
    Can someone try this and see if they are getting the correct values?

    The next step (if this works) is to get the output to reverse what it is right now... any suggestions on the fastest way to do that?
    Last edited by Azmeos; 07-10-2003 at 02:40 PM.
    \0

  2. #17
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I've not tried it, but it seems like it would work, even if it's a little complicated.

    If you insist on using your array, why not just use a simple comparison rather than a shift? It would make your code easier to read...

    Code:
    const unsigned int bit[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
    				int binValue = 0;
    		int count = 0;
    		int j = 0;
    
    		for(int k=0; k < imageLength; k++)
    		{
    			for(int i=0; i < imageWidth; i++)
    			{
    				if(j > 7) j = 0;
    				binValue = ((buf[i] & bit[j]) ? 1 : 0 );
    				cout << binValue;
    				j++;
    			}
    			cout << endl;
    		}

  3. #18
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    ok, I made that change... but I'm trying to isolate the problem of why my program won't work (properly) ... Do you think this is all working properly? This part, at least.
    \0

  4. #19
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Well, from looking at it, your program checks the 7th bit of the first byte, and then the 6th bit of the second byte, 5th bit of the third byte/etc and so on - is this what it is supposed to do? If not, then you need to loop J to check every bit before moving onto the next byte.

  5. #20
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    To test for a single bit in a number:
    Code:
    bool testBit( int number, int bitIndex)
    {
      return number &  1<<bitIndex;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Sang-drax
    To test for a single bit in a number:
    Code:
    bool testBit( int number, int bitIndex)
    {
      return number &  1<<bitIndex;
    }
    Slight correction here on your code:

    return !!(number & 1 << bitIndex);

    Without the double not statement, you are not actually returning a boolean value. The only way you would, would be if bitIndex was zero. Anything else does not return 1. It returns a power of two.

    While it is true that it would work for an equality test, where anything non-zero is considered true, in your case, it is incorrect, because bool should only ever be one or zero.

    This would fail:

    if( testBit( 2, 1 ) == 1 )

    Because "number & 2" would return two, not one.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    I have added comments to show what I believe this does... Please correct me if I'm wrong. And if there's an easier/better/right way to do it.

    (it says PHP but that's just so you can see the color and it's easier to read)

    PHP Code:
            int binValue 0;
            
    int j 0;

            for(
    int k=0imageLengthk++)                    //loops to the start of every line of pixels
            
    {
                for(
    int i=0imageWidthi++)                //loops to each pixel in the line
                
    {
                    if(
    70;                            //restarts the bit count
                    
    binValue = ((buf[i] & mask[j]) ? );    //checks the bit if it's on or off
                    
    cout << binValue;
                    
    j++;                                        //increments bit count
                
    }
                
    cout << endl;
            } 
    \0

  8. #23
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I can tell what your code does without comments, and it still does exactly what I described above - checks exactly one bit in each byte. Why? Because "y" is incremented only after "i" has changed.

    The program, as it stands, checks the 7th bit of the first byte, and then the 6th bit of the second byte, 5th bit of the third byte/etc and so on, like I said above before you ignored my post

  9. #24
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It seems like the loop is not correct:
    Code:
     binValue = ((buf[i] & mask[j]) ? 1 : 0 );
    you would first get buf[0] & mask[0]
    but then it would be buf[1] & mask[1]

    dont you want to compare every bit in the byte before moving on to the next one?

    maybe add another loop:

    Code:
    for(int i=0; i < imageWidth; i++)                
                {
                     for (int j; j < 7; j++)                            
                    {
                       binValue = ((buf[i] & mask[j]) ? 1 : 0 );    
                        cout << binValue;
                    }  
                                      
                }

    but I really don't understand what you are doing...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think JaWib is right. Assuming you want to check all eight bits per byte, you need another loop. And if you want to print the most sigficicant bit first (instead of the least significant bit), just change the bit array (old code commented).
    Code:
       const unsigned int bit[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
       int binValue, j;
       int byte = 0;
    
       for (int k=0; k < imageLength; k++)
       {
          for (int i=0; i < imageWidth; i++)
          {
             //if(j > 7) j = 0;
             for (j=0; j<8; j++)
             {
    	    //binValue = ((buf[i] & bit[j]) ? 1 : 0 );
    	    binValue = ((buf[byte] & bit[j]) ? 1 : 0 );
    	    cout << binValue;
             }
             cout << " ";
             byte++;
    	 //j++;
          }
          cout << endl;
       }
    Last edited by swoopy; 07-11-2003 at 11:28 PM.

  11. #26
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Originally posted by adrianxw
    You should be using a bitmask not an integer, i.e. instead of 1 use 0x01, instead of 10 use 0x02, 100 - 0x04 1000 - 0x08 10000 - 0x10, 100000 - 0x20, 1000000 - 0x40 and 10000000 - 0x80.
    Is it possible to AND with decimals 1,2,4,8,16,32,64, etc? I don't care if it's not the standard way, but is it possible?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #27
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I don't understand x & 0["@"]. Is "0" a system defined character array?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #28
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I still don't understand. Is Steve Summit saying that 5["abcdef"] will return 'f'? As if you've said "abcdef"[5]?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #29
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    Is it possible to AND with decimals 1,2,4,8,16,32,64, etc? I don't care if it's not the standard way, but is it possible?
    <<<

    Yes of course, the point I was making is he was not using 1,2,4... he was using 1, 10, 100...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  15. #30
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Point taken, adrian. That's interesting Salem, although I've never seen that method used in any code.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. byte is equal 8 bits???
    By Micko in forum C Programming
    Replies: 3
    Last Post: 10-15-2004, 10:12 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM