Question
Hi, I'm suppose to do basic bit-level operations using Unix-style programming. So far I have the entire code set up for this project, and when
Hi, I'm suppose to do basic bit-level operations using Unix-style programming. So far I have the entire code set up for this project, and when I try to "make" it, it's suppose to output the following:
gcc -Wall -g -m32 -c tester.c
gcc -Wall -g -m32 -c int2bitstr.c
gcc -Wall -g -m32 -9 xtest tester.o int2bitstr.o
But, when I try typing "make" in the terminal with the code I currently have, this is what I get.
Also, this is what the output should look like when typing "./xtest" in the terminal.
Enter integer to convert to bits: 1
1 : 00000000000000000000000000000001
0x1 : 0000 0000 0000 0000 0000 0000 0000 0001
1.000000 : 00111111100000000000000000000000
exp : 0
If you can help me with the provided code below, and point out the changes with comments, then this would be very appreciative. Thanks.
________________________________________
code
/*Tells the compiler to insert the contents of stdio.*/
#include
/*Defines the macro assert.*/
#include
/*floating point helpers*/
unsigned f2u(float f)
{
/*union contains the property where one member contains a value, and all the variables in it share a memory.*/
union {
/*unsigned declares u*/
unsigned u;
/*float declares f*/
float f;
} v;
/* v.u assigned to zero.*/
v.u = 0;
/*float value f written in the same memory location, to where it changes the saved value.*/
v.f = f;
/*the value that is converted to unsigned in is returned.*/
return v.u;
}
/*function accepts no arguments stating by void and returns an integer value.*/
int main(void)
{
/*arrays declared to hold both binary and hex value.*/
char str[33], hex[5];
/*i declared as an integer.*/
int i;
/*declaration of function, defined later.*/
void int2bitstr(int I, char *str);
/*declaration of function, defined later.*/
int get_exp_value(float f);
/*hex[4] assigned to a null terminator.*/
hex[4] = '\0';
/*an indefinite loop to where the program will keep asking for values until user does not end the program.*/
do
{
/*k declared as an integer.*/
int k;
/*Prompts the user to enter an integer to convert to bits.*/
printf("Enter integer to convert to bits: ");
/*assert to confirm the value is actually read and will throw an exception once the end of the file is reached*/
assert(scanf_s("%d", &i) == 1);
/*converts the int to string.*/
int2bitstr(i, str);
/*prints the bit string.*/
printf("%d : %s ", i, str);
/* Prints out binary numbers 4 digits at a time for hex confirmation */
/*NOTE: this prints out the hexadecimal representation of the number, which is what %x does in order to confirm that the conversion is right.*/
printf("0x%x : ", i);
/*prints the converted value, 4 bits at a time, and if the conversion turns out to be successful, then each group of 4 bits must correspond to the hex digit printed above base on previous command.*/
for (k = 0; k
{
/*new pointer to where it jumps 4 places in the str array in every iteration.*/
char *sp = str + k;
/*these copy four array elements of sp into hex array*/
hex[0] = sp[0];
hex[1] = sp[1];
hex[2] = sp[2];
hex[3] = sp[3];
/*prints the binary value corresponding to a hex digit*/
printf("%s ", hex);
}
/*prints the null*/
printf(" ");
/*converts integer to float representation*/
float f = i;
/*finds its bits representation*/
int2bitstr(f2u(f), str);
/*print the bit representation of float variables.*/
printf("%f : %s ", f, str);
/*prints what the power of 2 is closest to.*/
/*For example, 10 will be printed for 1024*/
printf("exp : %d ", get_exp_value(f));
} while (i);/*repeatedly executes a target statement as long as a given condition is true*/
/* signal normal completion */
return(0);
}
/* void in which I is input and str is output*/
void int2bitstr(int I, char *str) {
/*32-bit unsigned int data type that can hold integer values in range of 0 to 4,294,967,295.*/
unsigned int i = I;
/*j declared as an integer.*/
int j;
/* Starts from the least significant bit */
for (j = 31; j >= 0; j--)
{
/* The ampersand (&) is used to leave behind the least significant bit */
unsigned int fill = i & 1;
/* If then else statement that is used to fill in the bit assigned is 1, fill a 1 inside the index If the bit assigned is 0, fill a 0 inside the index */
if (fill == 1){
str[j] = '1';
}
else{
str[j] = '0';
}
/* Removes the least significant bit using right shift */
i = i >> 1;
}
/* Creates an empty spot for the last char */
str[32] = '\0';
}
/*where f is input, the exponent value of f needs to be calculated and returned.*/
int get_exp_value(float f) {
/* Floating point helpers*/
unsigned f2u(float f);
/*unsigned int ui assigned to f2u(f)*/
unsigned int ui = f2u(f);
/* Uses left and right shift to leave behind only the exponentbits */
ui = ui
ui = ui >> 24;/* Shifts left twenty-four times */
/* Substracts the bias */
ui = ui - 127;
/* signals completion*/
return(ui);
}
gcc -Wall -g -m32 -c int2bitstr.c In file included from int2bitstr.c:5:0: int2bitstr.c: In function 'main' int2bitstr.c:62:14: warning: implicit declaration of function 'scanf s' [-Wimplicit-function-declaration] assert (scant_s ("%d", &i) -1); int2bitstr.c:112:1: error: expected ;' before ')' token Makefile:39: recipe for target 'int2bitstr.o' failed make: [int2bitstr.o] Error 1Step 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