[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

11/29/2004 at 07:06PM PST, ID: 21224123
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

converting (2) short integers to an unsigned integer and displaying 32bit binary representation

Asked by zennx in C Programming Language

Tags: arry, bintodec, char

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;counter2--)
      {
            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.si[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;counter2--)
      {
            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

[+][-]11/29/04 08:45 PM, ID: 12702806

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/29/04 09:48 PM, ID: 12703028

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/30/04 08:09 AM, ID: 12707205

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: arry, bintodec, char
Sign Up Now!
Solution Provided By: imladris
Participating Experts: 2
Solution Grade: B
 
 
 
Loading Advertisement...
20091111-EE-VQP-91