C++ Tutorial/bitset/bitset

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A bitset to hold 8-bits

<source lang="cpp">#include <bitset>

  1. include <string>
  2. include <iostream>

int main () {

   using namespace std;
   // A bitset to hold 8-bits
   bitset <8> eightBits;
   cout << "Enter a 8-bit sequence: ";
   // Store user-supplied sequence into the bitset
   cin >> eightBits;
   cout << endl;
   return 0;

}</source>

Create a copy of a bitset

<source lang="cpp">#include <bitset>

  1. include <string>
  2. include <iostream>

int main () {

   using namespace std;
   // A bitset to hold 8-bits
   bitset <8> eightBits;
   cout << "Enter a 8-bit sequence: ";
   // Store user-supplied sequence into the bitset
   cin >> eightBits;
   cout << endl;
   // create a copy
   bitset <8> flipInput (eightBits);
   return 0;

}</source>

Initialize a bitset with hex number

<source lang="cpp">#include <bitset>

  1. include <iostream>

using namespace std; int main( ){

  const int num_lights = 15;
  const bitset<num_lights> outside( 0xf );
  const bitset<num_lights> inside( 0xff0 );
  const bitset<num_lights> driveway( 0x7000 );

}</source>

instantiate a bitset object for 8 bits, given an unsigned long init value

<source lang="cpp">#include <bitset>

  1. include <iostream>
  2. include <string>

int main () {

   using namespace std;
   bitset <8> eightbits (255);
   cout << "The initial contents of eightBits: " << eightbits << endl;
   return 0;

}</source>

Instantiate a bitset object for holding 4 bits, all initialized to "0000"

<source lang="cpp">#include <bitset>

  1. include <iostream>
  2. include <string>

int main () {

   using namespace std;
   bitset <4> fourBits;
   cout << fourBits << endl;
   return 0;

}</source>

Instantiate a bitset object for holding 5 bits, initialize it to a bit sequence supplied by a string

<source lang="cpp">#include <bitset>

  1. include <iostream>
  2. include <string>

int main () {

   using namespace std;
   bitset <5> fiveBits (string ("10101"));
   cout << fiveBits << endl;
   return 0;

}</source>

Read a binary number into a bitset

<source lang="cpp">#include <bitset>

  1. include <iostream>
  2. include <limits>

using namespace std; int main( ) {

  // Read a binary number into a bitset
  cout << "Enter a binary number: ";
  bitset< numeric_limits<unsigned long>::digits > bits;
  cin >> bits;
  unsigned long decimal_equivalent = bits.to_ulong();
  cout  << "Binary number: " << bits << "\nDecimal equivalent: " << decimal_equivalent;

}</source>

Reset bit in a bitset

<source lang="cpp">#include <bitset>

  1. include <iostream>

using namespace std; int main( ){

  const int num_lights = 15;
  const bitset<num_lights> outside( 0xf );
  const bitset<num_lights> inside( 0xff0 );
  const bitset<num_lights> driveway( 0x7000 );
  bitset<num_lights> lights;
  for( int i = 0; i < num_lights; i +=2 )
     lights.reset( i );
  cout << lights;

}</source>

Test value in a bitset

<source lang="cpp">#include <bitset>

  1. include <iostream>

using namespace std; int main(int argc, char** argv){

 bitset<10> myBitset;
 myBitset.set(3);
 myBitset.set(6);
 myBitset[8] = true;
 myBitset[9] = myBitset[3];
 if (myBitset.test(3)) {
   cout << "Bit 3 is set!\n";
 }
 cout << myBitset << endl;
 return (0);

}</source>

Use bitset with enum together

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <bitset>
  2. include <iostream>

using namespace std; int main() {

   /* enumeration type for the bits
    * - each bit represents a color
    */
   enum Color { red, yellow, green, blue, white, black, //...,
                numColors };
   // create bitset for all bits/colors
   bitset<numColors> usedColors;
   // set bits for two colors
   usedColors.set(red);
   usedColors.set(blue);
   // print some bitset data
   cout << "bitfield of used colors:   " << usedColors
        << endl;
   cout << "number   of used colors:   " << usedColors.count()
        << endl;
   cout << "bitfield of unused colors: " << ~usedColors
        << endl;
   // if any color is used
   if (usedColors.any()) {
       // loop over all colors
       for (int c = 0; c < numColors; ++c) {
           // if the actual color is used
           if (usedColors[(Color)c]) {
               //...
           }
       }
   }

}</source>

bitfield of used colors:   001001
number   of used colors:   2
bitfield of unused colors: 110110

write a decimal integer as a binary number

<source lang="cpp">#include <bitset>

  1. include <iostream>
  2. include <limits>

using namespace std; int main( ) {

  // write a decimal integer as a binary number
  int num = 100;
  cout  << "\n\nDecimal number: " << num << "\tBinary equivalent: " << bitset<8>(num) << endl;

}</source>