/* Circular Shift Register In digital circuits, a circular shift register is a construct similar to an array that can shift its contents left or right as if they are arranged in a circle. During a right shift, each element in the register is moved one space to the right, and the last element goes to the beginning of the register. Similarly, during a left shift, each element is moved to the left and the first element goes to the end. For example, a shift register with the values [1,0,1,1,1,0] would behave as follows: Right shift by 1: [0,1,0,1,1,1] Right shift by 2: [1,0,1,0,1,1] Right shift by 5: [0,1,1,1,0,1] Left shift by 1: [0,1,1,1,0,1] Left shift by 3: [1,1,0,1,0,1] You must write a complete C program that models a circular shift register. Your program must read in the contents of the register and take in a shift direction and amount. It must then output the shifted contents of the register. You must define two functions that shift the register by 1 in the specified direction (one function for left and one for right shift). You must call the appropriate function repeatedly to shift the register by the specified amount. HINT: The register will contain at most 1024 bits. Do not worry about error checking -- assume all inputs are correctly formatted. ====== SAMPLE RUN #1 ====== Enter the register size: 5 Enter the register contents: 1 1 0 1 0 Enter the shift direction [-1 for left, 1 for right]: 1 Enter the shift amount: 3 The contents of the shifted register are: 0 1 0 1 1 =========================== ====== SAMPLE RUN #2 ====== Enter the register size: 10 Enter the register contents: 1 1 0 0 0 1 1 0 1 0 Enter the shift direction [-1 for left, 1 for right]: -1 Enter the shift amount: 4387 The contents of the shifted register are: 0 1 0 1 1 0 0 0 1 1 =========================== */ #include #include void shiftRight(int reg[], int size){ //Store the last value, move everything right, then put the temp value at the beginning int temp = reg[size - 1], i; for(i=size - 1; i > 0; i--){ reg[i] = reg[i - 1]; } reg[0] = temp; } void shiftLeft(int reg[], int size){ //Store the first value, move everything left, then put the temp value at the end int temp = reg[0], i; for(i=0; i < size - 1; i++){ reg[i] = reg[i + 1]; } reg[size - 1] = temp; } int main(int argc, char *argv[]){ int size, shiftdir, shiftamt, reg[1024], i; printf("Enter the register size: "); scanf("%d", &size); printf("Enter the register contents: "); for(i=0; i < size; i++){ scanf("%d", ®[i]); } printf("Enter the shift direction [-1 for left, 1 for right]: "); scanf("%d", &shiftdir); printf("Enter the shift amount: "); scanf("%d", &shiftamt); //Don't need to shift at all if it goes all the way around shiftamt %= size; //Left shift if(shiftdir < 0){ for(i=0; i < shiftamt; i++){ shiftLeft(reg, size); } } //Right shift else{ for(i=0; i < shiftamt; i++){ shiftRight(reg, size); } } printf("\nThe contents of the shifted register are:"); for(i=0; i < size; i++){ printf(" %d", reg[i]); } printf("\n"); system("pause"); return 0; }