Hi, I really need help badly because this project has been long over-due and I can't seem to solve this problem. I was asked to create a program that accepts any floating point integer, using a union I cast the floating point as an unsigned integer into a binary converter function I made. This function converts it to binary but also breaks it up into 3 parts for 32bit representation. 1st part is the signed bit, 2nd part is the exponent part (excess 127) and the 3rd part is the mantisa. Ok, so no problem doing this program here is the code:
union numType
{
float f;
unsigned int ui;
long int u1;
};
union numType u;
int main()
{ int sign=0;
printf("Please enter a number, 0 to exit ");
scanf("%fl",&u.f);
while(u.f!=0)
{
if (u.f<0)
{
u.f = u.f * (-1);
sign = 1;
}
else
sign = 0;
printf("%d\n",u.ui);
printf("%d\n",u.u1);
bintodec(u.ui,sign);
printf("\nPlease enter a number, 0 to exit ");
scanf("%fl",&u);
}
return 0;
};
bintodec(int value, int sign)
{
int counter,counter2,numb,arry
[32];
numb = value;
for(counter=0;counter<=31;
counter++)
{
//printf("\nvalue going into array is %d",arry[counter]);
arry[counter]=numb % 2;
numb = numb / 2;
}
printf("\n");
if (sign == 1)
arry[31] = 1;
else
arry[31] = 0;
printf("signed bit = %d",arry[31]);
printf("\nsecond part = ");
for(counter2=30;counter2>=
23;counter
2--)
{
printf("%d",arry[counter2]
);
}
printf("\nthird part = ");
for(counter2=22;counter2>=
0;counter2
--)
{
printf("%d",arry[counter2]
);
}
}
As you can see to handle the negative and positive numbers, instead of putting it back through as a signed integer if detected I just multiplied it by -1 to make it positive and then made a flag to mark if it were positive or negative. Ok now, my problem lies in the 2nd part of the project if you are still with me by this point. (I just wanted to get you familiar with what I was doing in the begining).
The next part of the program basically does the same thing, but instead it needs to convert 2 short integers to an unsigned integer (like in the first part) and display in the 32 bit representation. In addition the same thing with 4 characters in an array. And it also needs to do the same for 2 unsigned short integers etc... Doesn't sound too hard. Here's the new union:
union numType
{
short int si[2];
long int ls1;
unsigned short int us1;
unsigned short int us2;
char arry[3];
unsigned char arryc[3];
float f;
unsigned int ui;
};
union numType u;
My first problem was that I could not get the union to hold the (2) values for the short integers, but 'sunnycoder' kindly pointed out that I need to use it as an array in the union. I got that problem solved, but now before I even try to put the number though the binary converter I check to see what the value it is. It will only display the first short integer as an unsigned integer. Here's my code and the program is not complete but is where I left off
#include <stdio.h>
union numType
{
short int si[2];
long int ls1;
unsigned short int us1;
unsigned short int us2;
char arry[3];
unsigned char arryc[3];
float f;
unsigned int ui;
};
union numType u;
int main()
{ int sign=0;
printf("Please enter 2 short integer numbers, 0 to exit ");
scanf("%d%d",&u.si[1],&u.s
i[2]);
while(u.si[1] && u.si[2]!=0)
{
printf("value 1 = %d, value 2 = %d", u.si[1], u.si[2]);
printf("unsigned int = %d",u.ui);
/*if (u.f<0)
{
u.f = u.f * (-1);
sign = 1;
}
else
sign = 0; */
//bintodec(u.s,sign);
printf("\nPlease enter 2 short integer numbers, 0 to exit ");
scanf("%d%d",&u.si[1], &u.si[2]);
}
return 0;
};
bintodec(int value1,int value2,int sign)
{
int counter,counter2,numb,arry
[32];
numb = value1;
for(counter=0;counter<=31;
counter++)
{
arry[counter]=numb % 2;
numb = numb / 2;
}
printf("\n");
if (sign == 1)
arry[31] = 1;
else
arry[31] = 0;
printf("signed bit = %d",arry[31]);
printf("\nsecond part = ");
for(counter2=30;counter2>=
23;counter
2--)
{
printf("%d",arry[counter2]
);
}
printf("\nthird part = ");
for(counter2=22;counter2>=
0;counter2
--)
{
printf("%d",arry[counter2]
);
}
}
Note, that I set it up just to check the values but you get the idea. Please help, I really need to finish this project up as there are only a few more weeks left of class. Thanks to any that can help me