2011-12-02 107 views
1

我剛剛得到了一個數獨求解器的框架,但我不明白他們使用的語法以及我應該如何繼續。他們稱它爲小事,但在尋找它時我沒有發現類似的東西。瞭解這個bitset實現(C++)

// 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 

顯然//填入的是我寫的,bitset的意思就是所有的人最初被設置爲1的問題是我怎麼操縱或使用它們9位。

噢,順便說一下,這是一個位:

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

谷歌,第一個結果:http://www.cplusplus.com/reference/stl/bitset/ – OSH

+0

@OrenS不一樣的東西。 –

+1

@OrenS。他需要發展他的「自己的」比特。您的評論沒有幫助。 – FailedDev

回答

4

A 「位字段」 只是在存儲器中的整數的解釋,如果它是比特的列表。您將逐個設置,測試和重置此整數中的位,並且代碼中的註釋會準確告訴您在每個函數中要做什麼。

您可以使用'&'和'|'按位AND和OR,'< <'和'>>'用於將所有位左移和右移。這篇文章對你很有幫助:http://en.wikipedia.org/wiki/Bitwise_operation

+0

針對被刪除的信息:我知道你不應該爲我做我的作業,我也不指望你。我的大部分問題都被標記爲「家庭作業」,僅僅是因爲我的大部分課程都在我的學習中,作業標籤告訴人們不要給我一個完整的答案,而是指向正確的方向。實際上,我在這裏要求的是我如何改變變量_digits。你(和其他幾個人)回答了這個問題,還有更多,所以謝謝。我仍然認爲我的問題在這裏是合法的。 – Rickard

+0

我絕對沒有問題,特別是如果你正確地標記它,你問一個與作業相關的問題。我說的是,如果每個人都這樣做,我們會被這些問題所困擾。下一次請確保在詢問之前做足夠的研究,至少花費5分鐘的搜索時間。搜索「C++ bitset」在第3個結果中返回這個(http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c)。 –

4

該初始化設定位1 - 9的_digits爲1 (1 << n)裝置1移位n位到左側的表達。表達式a | b意味着按位或ab

所以,詳細地說,所有表達式(1 << n)結果在一個位模式與所有零和1在Ñ個位置,爲0 <Ñ< 10的這些都是or「D一起,以得到位模式位1至9設置爲1:

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

(未使用的位中未示出)

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

這是位集如何工作。

實施例:

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 
}