Hello, I\'m done with my bit-level operations code done in C, and have it runnin
ID: 3889654 • Letter: H
Question
Hello, I'm done with my bit-level operations code done in C, and have it running like it should. Can you explain what each line of code in the program does by using comments, and make some possible corrections base the following rules?
_____________________________________________
Operations that are not allowed in int2bitstr:
* Switch statements, function calls, and macro invocations.
* Addition, subtraction, division, modulus, and multiplication.
___________________________________________________
Operations that are not allowed in get_exp_value:
*Loops, switch statements, function calls (except unsigned f2u (float f)), and macro invocations.
*Division, modulus, and multiplication. Subtraction is allowed.
* Relative comparison operators (<, >, <=, and >=). Only both Equality (==) and inequality (!=) tests are allowed.
____________________________________________
Below is my current code, and providing feedback would be appreciative. Thanks.
_____________________________________________
#include <stdio.h> /*Tells the compiler to insert the contents of sdio.*/
#include <assert.h>
unsigned f2u(float f)
{
union {
unsigned u;
float f;
} v;
v.u = 0;
v.f = f;
return v.u;
}
int main(void)
{
char str[33], hex[5];
int i;
void int2bitstr(int I, char *str);
int get_exp_value(float f);
hex[4] = '';
do
{
int k;
printf("Enter integer to convert to bits: ");
assert(scanf_s("%d", &i) == 1);
int2bitstr(i, str);
printf("%d : %s ", i, str);
/* Prints out binary numbers 4 digits at a time for hex confirmation */
printf("0x%x : ", i);
for (k = 0; k < 32; k += 4)
{
char *sp = str + k;
hex[0] = sp[0];
hex[1] = sp[1];
hex[2] = sp[2];
hex[3] = sp[3];
printf("%s ", hex);
}
printf(" ");
float f = i;
int2bitstr(f2u(f), str);
printf("%f : %s ", f, str);
printf("exp : %d ", get_exp_value(f));
} while (i);
/* signal normal completion */
return(0);
}
void int2bitstr(int I, char *str) {
unsigned int i = I;
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] = '';
}
int get_exp_value(float f) {
unsigned f2u(float f);
unsigned int ui = f2u(f);
/* Uses left and right shift to leave behind only the exponentbits */
ui = ui << 1; /* Shifts left once */
ui = ui >> 24;/* Shifts left twenty-four times */
/* Substracts the bias */
ui = ui - 127;
return(ui);
}
Explanation / Answer
Please find the code with comments.
#include <stdio.h> /*Tells the compiler to insert the contents of sdio.*/
#include <assert.h>
unsigned f2u(float f)
{
/* union has the property that only one member may contain a value. All variables share memory */
union {
unsigned u;
float f;
} v;
v.u = 0; //v.u is assigned 0
v.f = f; // now in same memory location, float value f is written. This changes the saved value.
return v.u; // The value converted to unsigned int is returned. This would be very different from the actual integer value of the float variable, because of representation of float variables in memory */
}
int main(void){
char str[33], hex[5]; //declare arrays to hold binary value and hex value
int i;
void int2bitstr(int I, char *str); //declaration of function, defined later
int get_exp_value(float f); //declaration of function, defined later
hex[4] = '';
do
{ //indefinite loop, so that program keeps asking for values until user does not end the program
int k;
printf("Enter integer to convert to bits: "); //prompting user to enter a value
assert(scanf_s("%d", &i) == 1); /*assert to confirm the value is actually read. This will throw and exception when, for example, end of file is reached */
int2bitstr(i, str); //converting int to bit string
printf("%d : %s ", i, str); //printing the bit string
/* Prints out binary numbers 4 digits at a time for hex confirmation */
printf("0x%x : ", i); /* prints out the hexadecimal representation of the number. %x in printf does that. Doing this to confirm that conversion is correct */
/*Now printing the converted value, 4 bits at a time. If the conversion was successful, each group of 4 bits must correspond to the hex digit printed above it (in the previous command) */
for (k = 0; k < 32; k += 4)
{
char *sp = str + k; //New pointer, it jumps 4 places in the str array in every iteration
/* 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];
printf("%s ", hex); //print the binary value corresponding to a hex digit
}
printf(" ");
float f = i; //convert integer to float representation
int2bitstr(f2u(f), str); // for this float value, find its bits representation
printf("%f : %s ", f, str); //print the bit representaton of float variable
printf("exp : %d ", get_exp_value(f)); //print what power of 2 is closest to this value. Eg, for 1024, it will print 10.
} while (i);
/* signal normal completion */
return(0);
}
void int2bitstr(int I, char *str) {
unsigned int i = I;
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] = '';
}
int get_exp_value(float f) {
//from floating point representation of an integer number, it is easy to find its log value. We need to left shift the number once (to remove sign bit) , right shift it 24 times, and then subtract 127 to get the log2(num) value. */
unsigned f2u(float f);
unsigned int ui = f2u(f);
/* Uses left and right shift to leave behind only the exponentbits */
ui = ui << 1; /* Shifts left once */
ui = ui >> 24;/* Shifts left twenty-four times */
/* Substracts the bias */
ui = ui - 127;
return(ui);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.