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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:30:26+00:00 2026-05-11T12:30:26+00:00

How can I subtract two integers in C without the – operator?

  • 0

How can I subtract two integers in C without the - operator?

  • 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. 2026-05-11T12:30:27+00:00Added an answer on May 11, 2026 at 12:30 pm
    int a = 34; int b = 50; 

    You can convert b to negative value using negation and adding 1:

    int c = a + (~b + 1);  printf('%d\n', c);  -16 

    This is two’s complement sign negation. Processor is doing it when you use ‘-‘ operator when you want to negate value or subtrackt it.

    Converting float is simpler. Just negate first bit (shoosh gave you example how to do this).

    EDIT:

    Ok, guys. I give up. Here is my compiler independent version:

    #include <stdio.h>  unsigned int adder(unsigned int a, unsigned int b) {     unsigned int loop = 1;     unsigned int sum  = 0;     unsigned int ai, bi, ci;      while (loop) {         ai = a & loop;         bi = b & loop;         ci = sum & loop;         sum = sum ^ ai ^ bi;      // add i-th bit of a and b, and add carry bit stored in sum i-th bit         loop = loop << 1;         if ((ai&bi)|(ci&ai)|(ci&bi)) sum = sum^loop; // add carry bit     }      return sum; }  unsigned int sub(unsigned int a, unsigned int b) {     return adder(a, adder(~b, 1));    // add negation + 1 (two's complement here) }   int main() {     unsigned int a = 35;     unsigned int b = 40;      printf('%u - %u = %d\n', a, b, sub(a, b)); // printf function isn't compiler independent here      return 0; } 

    I’m using unsigned int so that any compiler will treat it the same.

    If you want to subtract negative values, then do it that way:

     unsgined int negative15 = adder(~15, 1); 

    Now we are completly independent of signed values conventions. In my approach result all ints will be stored as two’s complement – so you have to be careful with bigger ints (they have to start with 0 bit).

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

Sidebar

Related Questions

Is there a TIMESTAMPDIFF() equivalent for PostgreSQL? I know I can subtract two timestamps
How can I subtract two dates when one of them is nullable? public static
Why can't subtract two time objects? For example, 12:00 - 11:00 = 1:00 from
How can I subtract two dates and get the total hours of the TimeSpan
How do I find the maximum (or minimum) of two integers in Postgres/SQL? One
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET

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.