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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have to define a computational model, give an estimation… May 11, 2026 at 7:04 pm
  • Editorial Team
    Editorial Team added an answer Okay, the solution was to export the file from excel… May 11, 2026 at 7:04 pm
  • Editorial Team
    Editorial Team added an answer The RelayCommand isn't a RoutedCommand, which I think is where… May 11, 2026 at 7:04 pm

Related Questions

How can I subtract two integers in C without the - operator?
We've gotten a homework assignment in Java, which relates to inheritance. I don't have
I have a Ruby DateTime which gets filled from a form. Additionally I have
I'm looking for the best way to calculate a nodes balance in an AVL-tree
I'm trying to convert a date string into an age. The string is like:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.