Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to simulate a binary register that supports shift right and

ID: 3890070 • Letter: W

Question

Write a C++ program to simulate a binary register that supports shift right and shift left operations. The original value stored in the register, its size and what operation(s) to perform and how many times are all given via the command line arguments. Your program must use a class to simulate that register and it should use the stl vector to hold the value of the bits in the register. You must do this work, as any other assignment in this class in Linux and using g++ compiler. You must turn in ONLY ONE UNCOMPRESSED TEXT FILE (NO INCLUDES, NO BINARIES): the c++ file named shiftregister.cpp

ARGUMENTS MUST BE SUPPORTED IN ANY ORDER.

by default your register must inject 0 values unless told otherwise via command line arguments.

Here are some of the parameters to support as your command line arguments:

-i “010101”: Initial values stored in your register.

-s 6: Specifies the number of bits in your shift register.

-r 2: shift the register right by two positions.

-l 3: shift left register by 3 positions.

-v 1: value to inject in vacated bits if other than default.

-p: prints the value of bits in your register after performing all the operations. Bits must be printed as a non-spaced string of 0 or 1s and terminated by a new line.

Multiple operations may be specified and they must be performed in the order specified in the command line argument list.

Any violation of the specs is likely to result on a zero grade especially if they prevent your program from compiling or running without core dumps.

VERY IMPORTANT: your program must return 0 on success and -1 on failure, not following this direction will result in zero credit in the full assignment.

Explanation / Answer

#include <iostream>
#include <bitset>
using namespace std;
  
int main() {
unsigned short short1 = 4;
bitset<16> bitset1{short1}; // the bitset representation of 4
cout << bitset1 << endl; // 0000000000000100
  
unsigned short short2 = short1 << 1; // 4 left-shifted by 1 = 8
bitset<16> bitset2{short2};
cout << bitset2 << endl; // 0000000000001000
  
unsigned short short3 = short1 << 2; // 4 left-shifted by 2 = 16
bitset<16> bitset3{short3};
cout << bitset3 << endl; // 0000000000010000
}

If you left-shift a signed number so that the sign bit is affected, the result is undefined. The following example shows what happens in Visual C++ when a 1 bit is left-shifted into the sign bit position.

#include <iostream>
#include <bitset>
using namespace std;
  
int main() {
short short1 = 16384;
bitset<16> bitset1{short2};
cout << bitset1 << endl; // 0100000000000000   
  
short short3 = short1 << 1;
bitset<16> bitset3{short3}; // 16384 left-shifted by 1 = -32768
cout << bitset3 << endl; // 100000000000000
  
short short4 = short1 << 14;
bitset<16> bitset4{short4}; // 4 left-shifted by 14 = 0
cout << bitset4 << endl; // 000000000000000
}

Right shifts

#include <iostream>
#include <bitset>
using namespace std;
  
int main() {
unsigned short short11 = 1024;
bitset<16> bitset11{short11};
cout << bitset11 << endl; // 0000010000000000
  
unsigned short short12 = short11 >> 1; // 512
bitset<16> bitset12{short12};
cout << bitset12 << endl; // 0000001000000000
  
unsigned short short13 = short11 >> 10; // 1
bitset<16> bitset13{short13};
cout << bitset13 << endl; // 0000000000000001
  
unsigned short short14 = short11 >> 11; // 0
bitset<16> bitset14{short14};
cout << bitset14 << endl; // 0000000000000000}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote