slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Main Index PowerPoint Presentation
Download Presentation
Main Index

Loading in 2 Seconds...

play fullscreen
1 / 10

Main Index - PowerPoint PPT Presentation


  • 147 Views
  • Uploaded on

Lecture 7 – bitset. 1. Main Index. Contents. Bitset model : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 member[0] member[1] 操作: vectorIndex: 位 i 对应的 vector 下标;. 1 1 1 1 1 1 1 1. 1 1 1 1 1 1 1 1. 2. Main Index. Contents. Implementing the bitVector Class.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Main Index' - lok


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Lecture 7

– bitset

1

Main Index

Contents

  • Bitset model:
  • 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • member[0] member[1]
  • 操作:vectorIndex: 位i 对应的vector下标;

1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1

implementing the bitvector class

2

Main Index

Contents

Implementing the bitVector Class
  • bitMask() returns an unsigned character value containing a 1 in the bit position representing i.
bitvector
Bitvector

class bitVector

{

public:

bitVector(int numBits = 16);

bitVector(int b[], int n);

bitVector& operator= (int v);

bitVector& operator= (char c);

int size() const;

int bit(int i) const;

void set(int i);

void clear(int i);

void clear();

bitVector operator~ ();

void write(fstream& ostr);

void read(fstream& istr, int numBits);

friend ostream& operator<< (ostream& ostr, const bitVector& x);

bitvector1
Bitvector

private:

int numberOfBits;

int vectorSize;

vector<unsigned char> member;

int vectorIndex(int i) const;

unsigned char bitMask(int i) const;

};

bitvector2
Bitvector

int bitVector::vectorIndex(int i) const

{

return i >> 3; //i 右移3位,相当与除8

}

unsigned char bitVector::bitMask(int i) const

{

return 1 << (7 - (i & 7)); //掩码,可置位和清零

} //1左移0-7位

//余数为0则最左边的位置1,余7则最右边的位置1

bitVector::bitVector(int numBits): numberOfBits(numBits)

{

vectorSize = (numberOfBits+7) >> 3;

member.resize(vectorSize);

}

bitvector3
Bitvector

bitVector operator| (const bitVector& x, const bitVector& y)

{

int i;

if (x.numberOfBits != y.numberOfBits)

throw

rangeError("bitVector |: bit vectors are not the same size");

bitVector tmp(x.numberOfBits);

for (i = 0; i < x.vectorSize; i++)

tmp.member[i] = x.member[i] | y.member[i];

return tmp;

}

bitvector4
Bitvector

bitVector bitVector::operator~ ()

{

bitVector tmp(numberOfBits);

for (int i = 0; i < vectorSize; i++)

tmp.member[i] = ~member[i];

return tmp;

}

bitvector5
Bitvector

int bitVector::bit(int i) const //返回第i位的值

{

if (i < 0 || i >= numberOfBits)

throw indexRangeError(

"bitVector bit(): bit out of range", i, numberOfBits);

if ((member[vectorIndex(i)] & bitMask(i)) != 0)

return 1;

else

return 0;

}

bitvector6
Bitvector

void bitVector::set(int i)

{

if (i < 0 || i >= numberOfBits)

throw indexRangeError(

"bitVector set(): bit out of range", i, numberOfBits);

member[vectorIndex(i)] |= bitMask(i);

}

bitvector7
Bitvector

void bitVector::clear(int i)

{

if (i < 0 || i >= numberOfBits)

throw indexRangeError(

"bitVector clear(): bit out of range", i, numberOfBits);

member[vectorIndex(i)] &= ~bitMask(i);

}