理解这个bitset实现(C ++)

时间:2011-12-02 10:52:46

标签: c++ bitset

我刚刚获得了一个数独求解器的框架,但我不明白他们使用的语法以及我应该如何继续。他们称之为bitset,但在搜索它时我发现没有类似的东西。

   // This file contains a simple implementation of sets of
   // digits between 1 and 9, called fields.
 #ifndef __SUDOKU_FIELD_H__
#define __SUDOKU_FIELD_H__
#include <iostream>
#include <cassert>
#include "digit.h"

class Field {
private:
  // Use integers for a bitset
  unsigned int _digits;
  // Number of digits in bitset
  unsigned int _size;
public:
  // Initialize with all digits between 1 and 9 included
  Field(void) 
    : _digits((1 << 1) | (1 << 2) | (1 << 3) |
          (1 << 4) | (1 << 5) | (1 << 6) |
          (1 << 7) | (1 << 8) | (1 << 9)), _size(9) {}

  // Return size of digit set (number of digits in set)
  unsigned int size(void) const {
    // FILL IN
  }

  // Test whether digit set is empty
  bool empty(void) const {
    // FILL IN
  }

  // Test whether set is assigned (that is, single digit left)
  bool assigned(void) const {
    // FILL IN
  }

  // Test whether digit d is included in set
  bool in(digit d) const {
    assert((d >= 1) && (d <= 9));
    // FILL IN
  }

  // Return digit to which the set is assigned

  digit value(void) const {
    assert(assigned());
    // FILL IN
  }



  // Print digits still included
  void print(std::ostream& os) const;

  // Remove digit d from set (d must be still included)
  void prune(digit d) {
    assert(in(d));
        // FILL IN
}

  // Assign field to digit d (d must be still included)
  void assign(digit d) {
    assert(in(d));
    // FILL IN
  }
};



// Print field
inline std::ostream&
operator<<(std::ostream& os, const Field& f) {
  f.print(os); return os;
}

#endif

显然// FILL IN是我写的,bitset的含义是9位,其中所有这些都是最初设置为1.问题是我如何操作或使用它们。

哦,顺便说一下,这是一个数字:

#ifndef __SUDOKU_DIGIT_H__
#define __SUDOKU_DIGIT_H__
typedef unsigned char digit;
#endif

3 个答案:

答案 0 :(得分:4)

此初始化将_digits的位1 - 9设置为1.表达式(1 << n)表示向左移位n位。表达式a | b表示按位或ab

因此,详细地说,所有表达式(1 << n)产生具有全零的位模式,并且在 n 位置产生1,对于0 <0。 n&lt; 10.所有这些都是or',以产生位模式位1到9设置为1:

(1 << 1)   0010 |
(1 << 2)   0100 |
(1 << 3)   1000
======================
           1110

(未使用的位未显示)

答案 1 :(得分:4)

“位域”只是对内存中整数的解释,就好像它是一个位列表。您将分别设置,测试和重置此整数中的位,并且代码中的注释会告诉您每个函数中的确切操作。

你可以使用'&amp;'和'|'用于按位AND和OR,以及'&lt;&lt;'和'&gt;&gt;'用于将所有位向左和向右移位。这篇文章对您非常有帮助:http://en.wikipedia.org/wiki/Bitwise_operation

答案 2 :(得分:3)

4位:

0000

二进制中的

1是:

0001

移位用于选择单个位:

0001 << 0 = 0001 // first bit

0001 << 1 = 0010 // second bit

0001 << 2 = 0100 // third bit

或者用于设置各个位:

0000 | 0100 = 0100

用于检索位:

0111 & 0001 = 0001

这就是bitsets的工作原理。

示例:

unsigned int x = 0;
x |= 1 << 4; // set 5th bit
x |= 1 << 3; // set 4th bit
x |= 0x3; // set first 2 bits - 0x3 = 0011
unsigned int b = true;
x |= b << 7; // set 8th bit to value of b
if (x & (1 << 2)) { // check if 3rd bit is true
  // ...
} 
b = (x >> 3) & 1; // set b to value of 4th bit

Here is a way to count number of bits, along with other helpful algorithms

unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
  v &= v - 1; // clear the least significant bit set
}