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 5997029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:12:17+00:00 2026-05-23T00:12:17+00:00

I understand that in order to do a subtraction you should do a 2’s

  • 0

I understand that in order to do a subtraction you should do a 2’s complement transformation to the second number .
Is there a dedicated Hardware for that checks the MSB and if it is found to be 1 it does the transformation ?

Also , Is this system used for subtraction of floating points ?

  • 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-23T00:12:18+00:00Added an answer on May 23, 2026 at 12:12 am

    The Two’s Complement operation is implemented in most languages with the unary - operator. It is only used with signed integer types. It can be implemented in an ALU as either a distinct negation (e.g. NEG) instruction or rolled into another operation, for example when you use a subtract (e.g. SUB) instruction instead of an add (e.g. ADD) instruction.

    Your first question is unclear because “the last bit” could refer to either the most-significant bit (MSB) or least significant bit (LSB). In a signed integer, the MSB indicates sign; checking for a negative is usually implemented as the N bit in the condition code register, which is updated from the result of the last instruction executed (though several instructions do not change the condition code register). Computing the two’s complement only if the original number is negative is the absolute value (e.g. ABS) operation. Checking the LSB just tells you if the integer is even or odd.

    Floating point numbers use a separate sign bit, so 0 and -0 are distinct values. Two’s compliment does not work with floating point values; a different approach must be used.

    EDIT: An example. Consider the following C code:

    #include <stdlib.h>
    
    int do_math(int a, int b)
    {
        return a - b;
    }
    
    int main(int argc, char* const argv[])
    {
        if(argc < 2)
            return 0;
        return do_math(atoi(argv[1]), atoi(argv[2]));
    }
    

    This can be run with:

    $ gcc -O0 foo.c -o foo
    $ ./foo 20 10; echo $?
    10
    

    On x86_64, the function do_math() contains the following code:

    _do_math:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %edi, -4(%rbp)
        movl    %esi, -8(%rbp)
        movl    -8(%rbp), %edx
        movl    -4(%rbp), %eax
        subl    %edx, %eax
        leave
        ret
    

    The first two lines are the preamble, setting up the stack for the function. The next four lines fetch the input parameters from the stack (since optimization was disabled, parameters weren’t passed in registers).

    Then the key instruction: subl, which takes the second parameter (%eax, the x86’s Extended AX register, 32 bits in size) and subtracts it from the first parameter (%edx, the x86’s Extended DX register, also 32 bits in size), storing the result back into %edx. In the ALU, the subl instruction takes the first parameter as-is and adds the two’s complement of the second parameter. It calculates the two’s complement by inverting the second parameter’s bits (similar to the ~ operator in C) and then using a dedicated adder to add 1. This step could be pipelines, it could be optimized so both it and the final addition complete in one cycle, or they could go a step further and roll the two’s complement logic into the ALU’s adder chain.

    The last two lines clean up the stack and return. (The x86 calling conventions store the result in %edx.

    EDIT 2: Use the -S option to gcc to generate an assembly file (same name as input file except .c suffix is replaced with .s). For example: gcc -O0 foo.c -S (Had I not turned off the optimizer with -O0, the entire do_math() function could have been inlined into main(), making it much harder to see.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that there are several ways to blend XNA and WPF within the
While I understand that in order for an iPhone application to be accepted on
I understand that Haskell's filter is a high order function (meaning a function that
I understand that in order to return a string from a function I have
I understand that arrays in C are allocated in row-major order. Therefore, for a
As I understand it you should use the Order property of the DataMember attribute
I totally understand that in order to learn javascript I need to know how
I understand that I can order a data.frame as such: test = data.frame(A=c(4,2,4), B=c(8,3,2))
Are there any considerations that should be taken into account when designing a new
I understand that some countries have laws regarding website accessibility. In general, what are

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.