Added ShiftOutLeft() and ShiftOutRight()

This commit is contained in:
Bart Trzynadlowski 2017-09-23 15:03:06 +00:00
parent 7a2e6b393a
commit 61a0517bd9
3 changed files with 63 additions and 0 deletions

View file

@ -127,6 +127,26 @@ namespace Util
// Fill in the right with "no data"
memset(m_bits.data() + m_bits.size() - count, m_no_data, count);
}
// Shift right and eject right-most bit, shifting new bit into left side to
// preserve vector size
uint8_t BitRegister::ShiftOutRight(uint8_t bit)
{
uint8_t ejected = GetRightMost();
ShiftRight(1);
m_bits[0] = !!bit;
return ejected;
}
// Shift left and eject left-most bit, shifting new bit into right side to
// preserve vector size
uint8_t BitRegister::ShiftOutLeft(uint8_t bit)
{
uint8_t ejected = GetLeftMost();
ShiftLeft(1);
m_bits[m_bits.size() - 1] = !!bit;
return ejected;
}
void BitRegister::Reset()
{

View file

@ -33,6 +33,11 @@ namespace Util
void ShiftRight(size_t count);
void ShiftLeft(size_t count);
// Functions that preserve the register size, shifting a bit in one side
// and ejecting one out the other
uint8_t ShiftOutRight(uint8_t bit);
uint8_t ShiftOutLeft(uint8_t bit);
// Functions that insert bits, clipped against current register size
void SetBit(size_t bitPos, uint8_t value);
void Insert(size_t bitPos, const std::string &value);

View file

@ -190,6 +190,44 @@ int main(int argc, char **argv)
expected.push_back("101");
results.push_back(reg.ToBinaryString());
// Test
reg.Reset();
reg.AddToLeft(1);
reg.AddToLeft(0);
reg.AddToLeft(1);
reg.AddToLeft(1);
reg.AddToLeft(0);
reg.AddToLeft(1);
reg.AddToLeft(0);
expected.push_back("0101101");
results.push_back(reg.ToBinaryString());
reg.RemoveFromLeft();
expected.push_back("101101");
results.push_back(reg.ToBinaryString());
// Test
reg.Set("11000");
results.push_back(reg.ShiftOutLeft(0) == 0 ? "0" : "1");
results.push_back(reg.ShiftOutLeft(1) == 0 ? "0" : "1");
results.push_back(reg.ShiftOutLeft(0) == 0 ? "0" : "1");
results.push_back(reg.ToBinaryString());
expected.push_back("1");
expected.push_back("1");
expected.push_back("0");
expected.push_back("00010");
// Test
reg.Set("11001");
results.push_back(reg.ShiftOutRight(0) == 0 ? "0" : "1");
results.push_back(reg.ShiftOutRight(1) == 0 ? "0" : "1");
results.push_back(reg.ShiftOutRight(0) == 0 ? "0" : "1");
results.push_back(reg.ToBinaryString());
expected.push_back("1");
expected.push_back("0");
expected.push_back("0");
expected.push_back("01011");
// Test
reg.Reset();
reg.SetZeros(32);