Question
I have three different C files, however I DO NOT need to edit the main or header. I need to complete the 3rd file below
I have three different C files, however I DO NOT need to edit the main or header. I need to complete the 3rd file below so that, given a bit string 'n' and a number of bits 'b' chosen by the user, it sign extends the number 'n' to fill up exactly 'b' bits and then negates that binary number. Comments are helpful...Also
- only edit the .c file (not main or header)
- the getBitString function must use the getchar function to let the user enter one charecter
- signExtend does not need to check for errors in its arguments
- the negate function should be just a two step algorithm to negate the given binary number
Example Output:
//header
#ifndef LAB4 #define LAB4
static const int MAX_WIDTH = 16; // the maximum number of bits in numbers
int getBitString(char input[]); void signExtend(int n, char input[], char output[]); void negate(int num_bits, char input[], char output[]);
#endif
//main
#include
#include "lab4.h"
int main(void) { char input[MAX_WIDTH + 1]; // bits entered by the user int input_length = 0; // number of bits entered by the user
char number[MAX_WIDTH + 1]; // user input after sign extension int num_bits = 0; // number of bits after sign extension
char negated[MAX_WIDTH + 1]; // result of negating number printf("Enter an integer in binary with up to 16 bits: "); input_length = getBitString(input);
num_bits = -1; while (num_bits MAX_WIDTH ) { printf("Choose the number of bits between %d and %d: ", input_length,MAX_WIDTH); scanf("%d",&num_bits); }
signExtend(num_bits, input, number); printf(" n = %s ",number);
negate(num_bits,number,negated);
printf("-n = %s ",negated); return 0; }// main
//FILE TO EDIT
#include
#include "lab4.h"
/* Wait for the user to enter between 0 and MAX_WIDTH bits followed by a newline character, and store these bits in the input array as a null terminated string. If the user enters more than MAX_WIDTH characters, then the user's input is truncated to MAX_WIDTH bits. Return the number of bits entered by the user (not counting the newline character) You can assume that input is large enough to store the input string. */ int getBitString(char input[]) {
/* to be completed */
return 0; // this line should be modified }// getBitString
/* Given an input bit string and an integer n greater than or equal to the length of input, store in output a copy of the input bit string after sign-extending it to n bits You can assume that output is large enough to store the sign-extended input string. */ void signExtend(int n, char input[], char output[]) {
/* to be completed */
}// signExtend
/* given an input bit string representing an integer n in two's complement representation of length num_bits, store in output the num_bits long two's complement representation of -n */ void negate(int num_bits, char input[], char output[]) {
/* to be completed */ }// negate
Enter an integer in binary with up to 16 bits: 0101 Choose the number of bits between 4 and 16: 6 n 000 101 Enter an integer in binary with up to 16 bits: 0101 Choose the number of bits between 4 and 16: 6 n 000 101Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started