Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:00:12+00:00 2026-05-30T15:00:12+00:00

I’m having trouble with Floating Point Addition in C. We’re given two 16 bit

  • 0

I’m having trouble with Floating Point Addition in C.

We’re given two 16 bit numbers, we’re supposed to add them not worrying about cases where their signs are different or where the exponents are 0 or 31.
This is a homework assignment, but I’m late & I can’t figure out why it’s not working.

The Algrothim sort of looks like:

  1. Get the sign bits of each number, if their different return 0.
  2. Get the exponents of each number, find the bigger one
    2a. find out the difference between the two.
  3. in the 11th bit put in a implicit one into each of the mantissa’s
  4. Right shift the number with the smaller exponent by the difference of the exponents
  5. Take the sign and exponent of the larger number or it with the shifted mantissa.
  6. Return the result.

Here’s my code:

LC3_Word FLADD(LC3_Word A, LC3_Word B)
{
    // a debug variable, so we can turn errors on (debug = 1) and off (debug = 0)
    int debug = 1; 

    // a default return value 
    LC3_Word returnValue = 0x0000;

    if(debug)
    {
        printf("-----------------------------------\nBegin debug\n-----------------------------------\n");
        printf("Return value: %x \n",returnValue); 
        printf("LC3 words: A %x, B %x\n",A,B);
    }

    // Masks to extract sign, exponent and fraction 
    LC3_Word signBitMask = 0x8000; 
    LC3_Word expMask = 0x7C000; 
    LC3_Word mantissaMask = 0x03FF; 

    // Mask to get the sign with the exponent
    LC3_Word signExpMask = 0xFC00;

    // A mask for the implicit 1
    LC3_Word implicitOne = 0x0400; 

    // Getting the signs
    LC3_Word signA = AND(signBitMask,A); 
    LC3_Word signB = AND(signBitMask,B);

    // Getting the exponents 
    LC3_Word expA = AND(expMask,A); 
    LC3_Word expB = AND(expMask,B); 

    // Getting the mantissa's 
    LC3_Word mantA = AND(mantissaMask,A);
    LC3_Word mantB = AND(mantissaMask,B);

        if(debug)
        {
            printf("======================\n");
            printf("\tSignBitMask: %x\n\texpMask: %x\n\tmantissaMask: %x\n",signBitMask,expMask,mantissaMask);
            printf("\tSign EXP Mask: %x\n",signExpMask);
            printf("\tsignA: %x, signB: %x\n", signA, signB); 
            printf("\tImplicit One Mask: %x\n",implicitOne); 
            printf("\tExp of a: %x, Exp of b: %x\n", expA, expB); 
            printf("\tmantissa of A: %x,mantissa of B: %x\n",mantA,mantB);
            printf("======================\n");
        }

    // Getting each with it's sign bit and it's exponent 
    LC3_Word signExpA = AND(signExpMask,A); 
    LC3_Word signExpB = AND(signExpMask,B); 

        if(debug)
        {
            printf("signExpA of A: %i, signExpB of B: %i\n",signExpA,signExpB);
        }

    // if the signs are different, don't deal with this case 
    if(signA!=signB)
    {
        return 0; 
    }   

    // if the signs are the same, if not, just return the default value 
    if(signA==signB)
    {
        if(debug)
        {
            printf("We got into the if signs are the same block \n");
            printf("Sign a: %i, Sign b: %i \n",signA,signB); 
        }

        if(expA==expB)
        {
            if(debug)
            {
                printf("We got into the if exponents are the same block \n");
                printf("Exp a: %x, Exp b: %x \n",expA,expB); 
            }

            // exponents are the same
            // Add Mantissa B to A 
            mantA = ADD(mantB,mantA);
            if(debug)
            {
                printf("Addition of mantissa's %x\n",mantA); 
            }
            // store into the return value the logical and of the mantissa with the existing exponent and sign
            // might want to do an OR() not an AND()
            returnValue = OR(signExpA,mantA); 
        } // end if the eponents are the same 
        else {
            if(debug)
            {
                printf("The exponents are not the same block \n");
            }
            // Getting the size we need to shift by
            int sizeToShift = 0; 
            if(expA>expB)
            {
                // Mask the mantissa of B with a implicit 1, then right shift
                mantB = OR(implicitOne,mantB); 

                if(debug)
                {
                    printf("The exponent a is > b\n");
                }
                // need to shift B, getting the size of how much
                sizeToShift = expA-expB;

                if(debug)
                {
                    printf("size to shift: %d,\nmantissaB is: %x\n",sizeToShift,mantB); 
                }
                // right shifting the mantissa of b
                mantB = mantB >> sizeToShift; 

                if(debug)
                {
                    printf("mantissa of b shifted: %x\n",mantB);
                }
                returnValue = OR(signExpA,ADD(mantA,mantB));
            }// end if A > B in the exponent 
            else
            {
                // Mask the mantissa of A with a implicit 1, then right shift
                mantA = OR(implicitOne,mantA);
                if(debug)
                {
                    printf("The exponent B is > A\n");
                }
                // need to shift A, getting the size of how much
                sizeToShift = expB-expA;

                if(debug)
                {
                    printf("size to shift: %d,\nmantissaA is: %x\n",sizeToShift,mantA); 
                }
                // right shifting the mantissa of A
                mantA = mantA >> sizeToShift; 

                if(debug)
                {
                    printf("mantissa of A shifted: %x\n",mantA);
                }
                returnValue = OR(signExpB,ADD(mantA,mantB));
            }// end if B > A in the exponent        
        }// end if different exponents 
    } // end if the signs are the same

    if(debug)
    {
        printf("Return Value %x\n",returnValue);
        printf("-----------------------------------\nEnd debug\n-----------------------------------\n");
    }

    return returnValue;
}

Here are ADD, OR and AND,

LC3_Word AND(LC3_Word A, LC3_Word B)
{
    return (A&B);
}

LC3_Word OR(LC3_Word A, LC3_Word B)
{
    return (A|B);
}

LC3_Word ADD(LC3_Word A, LC3_Word B)
{
    return (A+B); 
}

When I add 2 + 3 in floating point I get 3 instead of 5.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:00:14+00:00Added an answer on May 30, 2026 at 3:00 pm
        if(expA==expB)
        {
            if(debug)
            {
                printf("We got into the if exponents are the same block \n");
                printf("Exp a: %x, Exp b: %x \n",expA,expB); 
            }
    
            // exponents are the same
            // Add Mantissa B to A 
            mantA = ADD(mantB,mantA);
            if(debug)
            {
                printf("Addition of mantissa's %x\n",mantA); 
            }
            // store into the return value the logical and of the mantissa with the existing exponent and sign
            // might want to do an OR() not an AND()
            returnValue = OR(signExpA,mantA); 
        } // end if the eponents are the same 
    

    This is wrong.

    You’re failing to account for the addition of the two implicit ones. When you’re adding 2 + 3 you’re adding 1.0 x 2^1 + 1.1 x 2^1 and you’re ignoring everything before the decimal point… So you end up with 0.0 + 0.1 = 0.1 and just stick a 1 at the front. You need to add the two implicit ones as well.

    Try something like this:

        if(expA==expB)
        {
            if(debug)
            {
                printf("We got into the if exponents are the same block \n");
                printf("Exp a: %x, Exp b: %x \n",expA,expB); 
            }
    
            // exponents are the same
            // Add Mantissa B to A 
            mantA = OR(implicitOne,mantA);
            mantB = OR(implicitOne,mantB);
    
            mantA = ADD(mantB,mantA);
    
            // You need to normalize this now. But shifting to the right by 1 will suffice.
            mantA >>= 1;
            ++expA;
            // ... add the sign and you're done...
    
            if(debug)
            {
                printf("Addition of mantissa's %x\n",mantA); 
            }
            // store into the return value the logical and of the mantissa with the existing exponent and sign
            // might want to do an OR() not an AND()
            returnValue = OR(signExpA,mantA); 
        } // end if the eponents are the same 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.