Thread: C++ bit testing

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    C++ bit testing

    While my power was out
    I had alot of time to read, so here is a clean C++ way of testing bits.

    Code:
    #include <iostream>
    #include <bitset>  // Include the bitset header
    
    int main ( void )
    {
    	
        // Set the container
        // Since I'm testing a single byte you want the size to be 8
        // You can change this to the bit size of the variable your testing
        std::bitset<8> flags;
    
        // Value to test	
        unsigned char value = 0x3D;
    
        // Set the flags
        flags |= value;
    
        // Display the binary number
        std::cout << flags << std::endl;
    
        // The bits are stored in an array -----------
        // You can test each one ---------------------
        for ( int loop = 0; loop < 8; loop++ ) {
    
            if ( flags[loop] ) 
    	    std::cout << "Flag " << loop << " is set" << std::endl;
    
    
    	else std::cout << "Flag " << loop << " is not set" << std::endl;
    
        }
        // -------------------------------------------
        // -------------------------------------------
    
        return 0;
    
    }
    What is C++?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I suggest to place this in FAQ section!

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Or you could use the & operator without using the bitset class:
    Code:
    #include <iostream>
    
    
    int main()
    {
    int value = 61;
    for (int i=1,loop=0;i<=value;i*=2,loop++)
      if (value&i)
        std::cout<<"Bit "<<loop<<" is set."<<std::endl;
    
    std::cin.get();
    }
    But it would be more difficult to output the value in binary...
    "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

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by JaWiB
    But it would be more difficult to output the value in binary...
    Not really

    Code:
    #include <iostream>
    
    int main ( void )
    {
    
        unsigned char value = 0x3D;
    
        for ( int loop = 7; loop >= 0; loop-- ) {
    
            int the_bit = (value >> loop) & 1;
            std::cout << the_bit;
    
        }
    
        return 0;
    
    }


    And that would also work with C.

    I just found that the C++ bitset class was a really simple method to dispay, test, and manipulate bits.
    Last edited by Vicious; 09-19-2004 at 04:07 PM.
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  2. 16 bit compilar or 32 bit compilar
    By rajkumarmadhani in forum C Programming
    Replies: 16
    Last Post: 12-04-2007, 09:48 AM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. byte and bit order question
    By chunlee in forum C Programming
    Replies: 7
    Last Post: 11-07-2004, 01:50 PM
  5. bit conversions
    By wazilian in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 08:59 PM