Renamed shift in/out functions to add/remove to be less ambiguous about their functionality

This commit is contained in:
Bart Trzynadlowski 2017-09-23 14:42:05 +00:00
parent 86631695d7
commit 7a2e6b393a
3 changed files with 58 additions and 36 deletions

View file

@ -43,13 +43,13 @@ namespace Util
} }
// Shift a bit into the right side, growing the vector by 1 // Shift a bit into the right side, growing the vector by 1
void BitRegister::ShiftInRight(uint8_t bit) void BitRegister::AddToRight(uint8_t bit)
{ {
m_bits.push_back(!!bit); m_bits.push_back(!!bit);
} }
// Shift a bit into the left side, growing the vector by 1 // Shift a bit into the left side, growing the vector by 1
void BitRegister::ShiftInLeft(uint8_t bit) void BitRegister::AddToLeft(uint8_t bit)
{ {
m_bits.push_back(0); m_bits.push_back(0);
ShiftRight(1); ShiftRight(1);
@ -57,15 +57,15 @@ namespace Util
} }
// Shift left by 1, returning ejected bit (shrinks vector) // Shift left by 1, returning ejected bit (shrinks vector)
uint8_t BitRegister::ShiftOutLeft() uint8_t BitRegister::RemoveFromLeft()
{ {
uint8_t ejected = GetLeftMost(); uint8_t ejected = GetLeftMost();
ShiftOutLeft(1); RemoveFromLeft(1);
return ejected; return ejected;
} }
// Shift left and lose bits (shrinks vector) // Shift left and lose bits (shrinks vector)
void BitRegister::ShiftOutLeft(size_t count) void BitRegister::RemoveFromLeft(size_t count)
{ {
if (count >= m_bits.size()) if (count >= m_bits.size())
{ {
@ -80,15 +80,15 @@ namespace Util
} }
// Shift right by 1, returning ejected bit (shrinks vector) // Shift right by 1, returning ejected bit (shrinks vector)
uint8_t BitRegister::ShiftOutRight() uint8_t BitRegister::RemoveFromRight()
{ {
uint8_t ejected = GetRightMost(); uint8_t ejected = GetRightMost();
ShiftOutRight(1); RemoveFromRight(1);
return ejected; return ejected;
} }
// Shift right and lose bits (shrinks vector) // Shift right and lose bits (shrinks vector)
void BitRegister::ShiftOutRight(size_t count) void BitRegister::RemoveFromRight(size_t count)
{ {
// Shifting right means we lose lower bits (which are higher in the // Shifting right means we lose lower bits (which are higher in the
// vector), which means we just trim the vector from the right side // vector), which means we just trim the vector from the right side
@ -251,4 +251,21 @@ namespace Util
os << "[ " << reg.Size() << ": " << reg.ToHexString() << " ]"; os << "[ " << reg.Size() << ": " << reg.ToHexString() << " ]";
return os; return os;
} }
BitRegister::BitRegister()
{
}
BitRegister::BitRegister(size_t count)
{
SetZeros(count);
}
BitRegister::BitRegister(size_t count, uint8_t bit)
{
if (bit == 0)
SetZeros(count);
else
SetOnes(count);
}
} // Util } // Util

View file

@ -21,12 +21,12 @@ namespace Util
} }
// Functions that grow/shrink the bit register // Functions that grow/shrink the bit register
void ShiftInRight(uint8_t bit); void AddToRight(uint8_t bit);
void ShiftInLeft(uint8_t bit); void AddToLeft(uint8_t bit);
uint8_t ShiftOutLeft(); uint8_t RemoveFromLeft();
void ShiftOutLeft(size_t count); void RemoveFromLeft(size_t count);
uint8_t ShiftOutRight(); uint8_t RemoveFromRight();
void ShiftOutRight(size_t count); void RemoveFromRight(size_t count);
// Functions that preserve the current register size, shifting in the "no // Functions that preserve the current register size, shifting in the "no
// data" value as needed // data" value as needed
@ -50,6 +50,11 @@ namespace Util
std::string ToBinaryString() const; std::string ToBinaryString() const;
std::string ToHexString() const; std::string ToHexString() const;
friend std::ostream &operator<<(std::ostream &os, const BitRegister &reg); friend std::ostream &operator<<(std::ostream &os, const BitRegister &reg);
// Constructors
BitRegister();
BitRegister(size_t count);
BitRegister(size_t count, uint8_t bit);
private: private:
/* /*

View file

@ -89,17 +89,17 @@ int main(int argc, char **argv)
reg.Reset(); reg.Reset();
reg.Set("110010101"); reg.Set("110010101");
expected.push_back("1"); expected.push_back("1");
results.push_back(reg.ShiftOutRight() == 1 ? "1" : "0"); results.push_back(reg.RemoveFromRight() == 1 ? "1" : "0");
expected.push_back("11001010"); expected.push_back("11001010");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
expected.push_back("0"); expected.push_back("0");
results.push_back(reg.ShiftOutRight() == 1 ? "1" : "0"); results.push_back(reg.RemoveFromRight() == 1 ? "1" : "0");
expected.push_back("1100101"); expected.push_back("1100101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
expected.push_back("1"); expected.push_back("1");
results.push_back(reg.ShiftOutRight() == 1 ? "1" : "0"); results.push_back(reg.RemoveFromRight() == 1 ? "1" : "0");
expected.push_back("110010"); expected.push_back("110010");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
@ -107,28 +107,28 @@ int main(int argc, char **argv)
reg.Reset(); reg.Reset();
reg.Set("110010101"); reg.Set("110010101");
expected.push_back("1"); expected.push_back("1");
results.push_back(reg.ShiftOutLeft() == 1 ? "1" : "0"); results.push_back(reg.RemoveFromLeft() == 1 ? "1" : "0");
expected.push_back("10010101"); expected.push_back("10010101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
expected.push_back("1"); expected.push_back("1");
results.push_back(reg.ShiftOutLeft() == 1 ? "1" : "0"); results.push_back(reg.RemoveFromLeft() == 1 ? "1" : "0");
expected.push_back("0010101"); expected.push_back("0010101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
expected.push_back("0"); expected.push_back("0");
results.push_back(reg.ShiftOutLeft() == 1 ? "1" : "0"); results.push_back(reg.RemoveFromLeft() == 1 ? "1" : "0");
expected.push_back("010101"); expected.push_back("010101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
// Test // Test
reg.Reset(); reg.Reset();
reg.Set("0x12345"); reg.Set("0x12345");
reg.ShiftOutLeft(1); reg.RemoveFromLeft(1);
expected.push_back("0010010001101000101"); expected.push_back("0010010001101000101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
reg.ShiftOutLeft(2); reg.RemoveFromLeft(2);
expected.push_back("10010001101000101"); expected.push_back("10010001101000101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
@ -146,11 +146,11 @@ int main(int argc, char **argv)
// Test // Test
reg.Reset(); reg.Reset();
reg.Set("0x12345"); reg.Set("0x12345");
reg.ShiftOutRight(1); reg.RemoveFromRight(1);
expected.push_back("0001001000110100010"); expected.push_back("0001001000110100010");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
reg.ShiftOutRight(2); reg.RemoveFromRight(2);
expected.push_back("00010010001101000"); expected.push_back("00010010001101000");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
@ -167,26 +167,26 @@ int main(int argc, char **argv)
// Test // Test
reg.Reset(); reg.Reset();
reg.ShiftInRight(1); reg.AddToRight(1);
reg.ShiftInRight(1); reg.AddToRight(1);
reg.ShiftInRight(0); reg.AddToRight(0);
reg.ShiftInRight(1); reg.AddToRight(1);
reg.ShiftInRight(0); reg.AddToRight(0);
expected.push_back("11010"); expected.push_back("11010");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
// Test // Test
reg.Reset(); reg.Reset();
reg.ShiftInRight(1); reg.AddToRight(1);
reg.ShiftInRight(1); reg.AddToRight(1);
reg.ShiftInRight(0); reg.AddToRight(0);
reg.ShiftInRight(1); reg.AddToRight(1);
reg.ShiftInRight(0); reg.AddToRight(0);
reg.ShiftOutRight(); reg.RemoveFromRight();
expected.push_back("1101"); expected.push_back("1101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());
reg.ShiftOutLeft(); reg.RemoveFromLeft();
expected.push_back("101"); expected.push_back("101");
results.push_back(reg.ToBinaryString()); results.push_back(reg.ToBinaryString());