理解bitset实现(c++)

Understanding this bitset implementation (C++)

本文关键字:c++ 实现 bitset 理解      更新时间:2023-10-16

我刚得到这个框架的数独求解器,但我不明白他们使用的语法和我应该如何进行。他们称它为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 's是我写的,bitset的含义是9位,其中所有的初始值都被设置为1。问题是我如何操纵或使用它们。

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

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

"位域"只是对内存中的整数的解释,就好像它是位的列表一样。您将分别设置、测试和重置这个整数中的位,代码中的注释告诉您在每个函数中具体要做什么。

您可以使用'&'和'|'来进行逐位的and和OR操作,使用'<<'和'>>'来左右移动所有的位。这篇文章可能对你很有帮助:http://en.wikipedia.org/wiki/Bitwise_operation

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

因此,详细地说,所有表达式(1 << n)的结果都是一个位模式,在n位置上都是0和1,对于0 or="d一起,以产生位模式位1到9设置为1:</p">

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

(未使用的位不显示)

4 bits:

0000

1在二进制中是:

0001

移位用于选择单个位:

0001 << 0 = 0001 // first bit

0001 << 1 = 0010 // second bit

0001 << 2 = 0100 // third bit

Or用于设置单个位:

0000 | 0100 = 0100

和用于检索位:

0111 & 0001 = 0001

bitset就是这样工作的

的例子:

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

下面是一种计算比特数的方法,以及其他有用的算法:

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
}