INET Framework for OMNeT++/OMNEST
inet::BitVector Class Reference

#include <BitVector.h>

Public Member Functions

 BitVector ()
 
 BitVector (const char *bits)
 
 BitVector (unsigned int bits)
 
 BitVector (unsigned int bits, unsigned int fixedSize)
 
 BitVector (const BitVector &other)
 
 BitVector (const std::vector< uint8_t > &bytes)
 
unsigned int toDecimal () const
 
unsigned int reverseToDecimal () const
 
void appendBit (bool value)
 
void appendBit (bool value, int n)
 
void setBit (int pos, bool value)
 
void toggleBit (int pos)
 
bool getBit (int pos) const
 
void appendByte (uint8_t value)
 
unsigned int getSize () const
 
unsigned int getNumberOfBytes () const
 
const std::vector< uint8_t > & getBytes () const
 
int computeHammingDistance (const BitVector &u) const
 
BitVectoroperator= (const BitVector &rhs)
 
bool operator== (const BitVector &rhs) const
 
bool operator!= (const BitVector &rhs) const
 
std::string toString () const
 

Private Member Functions

int containerSize () const
 
void stringToBitVector (const char *str)
 
void copy (const BitVector &other)
 

Private Attributes

std::vector< uint8_t > bytes
 
int size
 

Friends

std::ostream & operator<< (std::ostream &out, const BitVector &bitVector)
 

Constructor & Destructor Documentation

◆ BitVector() [1/6]

inet::BitVector::BitVector ( )
15 {
16  size = 0;
17  bytes.push_back(uint8_t(0));
18 }

◆ BitVector() [2/6]

inet::BitVector::BitVector ( const char *  bits)
21 {
22  size = 0;
23  stringToBitVector(bits);
24 }

◆ BitVector() [3/6]

inet::BitVector::BitVector ( unsigned int  bits)
27 {
28  size = 0;
29  if (bits == 0)
30  appendBit(false);
31  while (bits > 0) {
32  appendBit(bits % 2);
33  bits /= 2;
34  }
35 }

◆ BitVector() [4/6]

inet::BitVector::BitVector ( unsigned int  bits,
unsigned int  fixedSize 
)
38 {
39  this->size = 0;
40  if (bits == 0)
41  appendBit(false);
42  while (bits > 0) {
43  appendBit(bits % 2);
44  bits /= 2;
45  }
46  if (getSize() < size)
47  appendBit(false, size - getSize());
48 }

◆ BitVector() [5/6]

inet::BitVector::BitVector ( const BitVector other)
inline
33 { copy(other); }

◆ BitVector() [6/6]

inet::BitVector::BitVector ( const std::vector< uint8_t > &  bytes)
inline
34 : bytes(bytes), size(bytes.size() * 8) {}

Member Function Documentation

◆ appendBit() [1/2]

◆ appendBit() [2/2]

void inet::BitVector::appendBit ( bool  value,
int  n 
)
100 {
101  while (n--)
102  appendBit(value);
103 }

◆ appendByte()

void inet::BitVector::appendByte ( uint8_t  value)
106 {
107  for (unsigned int i = 0; i < 8; i++)
108  appendBit(value & (1 << i));
109 }

◆ computeHammingDistance()

int inet::BitVector::computeHammingDistance ( const BitVector u) const
161 {
162  if (getSize() != u.getSize())
163  throw cRuntimeError("You can't compute Hamming distance between two vectors with different sizes");
164  int hammingDistance = 0;
165  for (unsigned int i = 0; i < getSize(); i++)
166  if (u.getBit(i) != getBit(i))
167  hammingDistance++;
168  return hammingDistance;
169 }

◆ containerSize()

int inet::BitVector::containerSize ( ) const
inlineprivate
24 { return bytes.size() * 8; }

Referenced by setBit().

◆ copy()

void inet::BitVector::copy ( const BitVector other)
private
195 {
196  size = 0;
197  bytes.clear();
198  for (unsigned int i = 0; i < other.getSize(); i++)
199  appendBit(other.getBit(i));
200 }

Referenced by operator=().

◆ getBit()

bool inet::BitVector::getBit ( int  pos) const
72 {
73  if (pos >= size)
74  throw cRuntimeError("Out of range with bit position %d", pos);
75  uint8_t field = bytes[pos / UINT8_LENGTH];
76  return field & (1 << (pos % UINT8_LENGTH));
77 }

Referenced by computeHammingDistance(), copy(), inet::physicallayer::Ieee80211LayeredOfdmTransmitter::createBitModel(), inet::physicallayer::Ieee80211LayeredOfdmReceiver::createCompleteBitModel(), inet::physicallayer::Ieee80211LayeredOfdmReceiver::createDataFieldBitModel(), inet::physicallayer::Ieee80211OfdmDecoder::createPacketModel(), inet::physicallayer::Ieee80211LayeredOfdmReceiver::createSignalFieldBitModel(), inet::physicallayer::ApskDecoder::decode(), inet::physicallayer::Ieee80211OfdmInterleaver::deinterleave(), inet::physicallayer::Ieee80211OfdmDemodulator::demodulate(), inet::physicallayer::ConvolutionalCoder::depuncturing(), inet::physicallayer::ConvolutionalCoder::encode(), inet::physicallayer::Ieee80211OfdmDecoder::getSignalFieldLength(), inet::physicallayer::Ieee80211LayeredOfdmReceiver::getSignalFieldLength(), inet::physicallayer::Ieee80211OfdmDecoder::getSignalFieldRate(), inet::physicallayer::ConvolutionalCoder::giveNextOutputSymbol(), inet::physicallayer::Ieee80211OfdmInterleaver::interleave(), inet::physicallayer::ApskModulator::modulate(), inet::physicallayer::Ieee80211OfdmModulator::modulate(), inet::operator<<(), operator==(), inet::ieee80211::QosAckHandler::processReceivedBlockAck(), inet::physicallayer::ConvolutionalCoder::puncturing(), reverseToDecimal(), inet::physicallayer::AdditiveScrambler::scramble(), toDecimal(), toString(), and inet::physicallayer::ConvolutionalCoder::traversePath().

◆ getBytes()

const std::vector<uint8_t>& inet::BitVector::getBytes ( ) const
inline

◆ getNumberOfBytes()

unsigned int inet::BitVector::getNumberOfBytes ( ) const
inline
45 { return bytes.size(); }

◆ getSize()

◆ operator!=()

bool inet::BitVector::operator!= ( const BitVector rhs) const
190 {
191  return !(rhs == *this);
192 }

◆ operator=()

BitVector & inet::BitVector::operator= ( const BitVector rhs)
172 {
173  if (this == &rhs)
174  return *this;
175  copy(rhs);
176  return *this;
177 }

◆ operator==()

bool inet::BitVector::operator== ( const BitVector rhs) const
180 {
181  if (rhs.getSize() != getSize())
182  return false;
183  for (unsigned int i = 0; i < getSize(); i++)
184  if (getBit(i) != rhs.getBit(i))
185  return false;
186  return true;
187 }

◆ reverseToDecimal()

unsigned int inet::BitVector::reverseToDecimal ( ) const
149 {
150  unsigned int dec = 0;
151  unsigned int powerOfTwo = 1;
152  for (int i = getSize() - 1; i >= 0; i--) {
153  if (getBit(i))
154  dec += powerOfTwo;
155  powerOfTwo *= 2;
156  }
157  return dec;
158 }

◆ setBit()

void inet::BitVector::setBit ( int  pos,
bool  value 
)
51 {
52  while (containerSize() <= pos)
53  bytes.push_back(uint8_t(0));
54  if (pos + 1 > size)
55  size = pos + 1;
56  uint8_t& field = bytes[pos / UINT8_LENGTH];
57  if (value)
58  field |= 1 << (pos % UINT8_LENGTH);
59  else
60  field &= ~(1 << (pos % UINT8_LENGTH));
61 }

Referenced by appendBit(), inet::ieee80211::RecipientBlockAckProcedure::buildBlockAck(), inet::physicallayer::Ieee80211OfdmInterleaver::deinterleave(), inet::physicallayer::ConvolutionalCoder::getPuncturedIndices(), and inet::physicallayer::Ieee80211OfdmInterleaver::interleave().

◆ stringToBitVector()

void inet::BitVector::stringToBitVector ( const char *  str)
private
124 {
125  int strSize = strlen(str);
126  for (int i = 0; i < strSize; i++) {
127  if (str[i] == '1')
128  appendBit(true);
129  else if (str[i] == '0')
130  appendBit(false);
131  else
132  throw cRuntimeError("str must represent a binary number");
133  }
134 }

Referenced by BitVector().

◆ toDecimal()

unsigned int inet::BitVector::toDecimal ( ) const
137 {
138  unsigned int dec = 0;
139  unsigned int powerOfTwo = 1;
140  for (unsigned int i = 0; i < getSize(); i++) {
141  if (getBit(i))
142  dec += powerOfTwo;
143  powerOfTwo *= 2;
144  }
145  return dec;
146 }

◆ toggleBit()

void inet::BitVector::toggleBit ( int  pos)
64 {
65  if (pos >= size)
66  throw cRuntimeError("Out of range with bit position %d", pos);
67  uint8_t& field = bytes[pos / UINT8_LENGTH];
68  field ^= 1 << (pos % UINT8_LENGTH);
69 }

Referenced by inet::physicallayer::LayeredErrorModelBase::computeBitModel(), and inet::physicallayer::Ieee80211OfdmErrorModel::corruptBits().

◆ toString()

std::string inet::BitVector::toString ( ) const
112 {
113  std::string str;
114  for (unsigned int i = 0; i < getSize(); i++) {
115  if (getBit(i))
116  str += "1";
117  else
118  str += "0";
119  }
120  return str;
121 }

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const BitVector bitVector 
)
friend
80 {
81  if (bitVector.getBit(0))
82  out << "1";
83  else
84  out << "0";
85  for (int i = 1; i < bitVector.size; i++) {
86  if (bitVector.getBit(i))
87  out << " 1";
88  else
89  out << " 0";
90  }
91  return out;
92 }

Member Data Documentation

◆ bytes

std::vector<uint8_t> inet::BitVector::bytes
private

◆ size

int inet::BitVector::size
private

The documentation for this class was generated from the following files:
inet::BitVector::getBit
bool getBit(int pos) const
Definition: BitVector.cc:71
inet::BitVector::appendBit
void appendBit(bool value)
Definition: BitVector.cc:94
inet::BitVector::setBit
void setBit(int pos, bool value)
Definition: BitVector.cc:50
inet::BitVector::getSize
unsigned int getSize() const
Definition: BitVector.h:44
inet::BitVector::bytes
std::vector< uint8_t > bytes
Definition: BitVector.h:20
inet::BitVector::copy
void copy(const BitVector &other)
Definition: BitVector.cc:194
inet::BitVector::containerSize
int containerSize() const
Definition: BitVector.h:24
UINT8_LENGTH
#define UINT8_LENGTH
Definition: BitVector.h:15
inet::BitVector::size
int size
Definition: BitVector.h:21
inet::BitVector::stringToBitVector
void stringToBitVector(const char *str)
Definition: BitVector.cc:123