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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:11:54+00:00 2026-06-11T17:11:54+00:00

Possible Duplicate: How can I multiply and divide using only bit shifting and adding?

  • 0

Possible Duplicate:
How can I multiply and divide using only bit shifting and adding?

I have to write functions to perform binary subtraction, multiplication, and division without using any arithmetic operators except for loop control. I’ve only written code in Java before now, so I’m having a hard time wrapping my head around this.

Starting with subtraction, I need to write a function with prototype

int bsub(int x, int y)

I know I need to convert y to two’s complement in order to make it negative and add it to x, but I only know how to do this by using one’s complement ~ operator and adding 1, but I can’t use the + operator.

The badd function was provided, and I will be able to implement it in bsub if I can figure out how to make y a negative number. The code for badd is shown below. Thanks in advance for any tips.

int badd(int x,int y){

int i;

char sum;
char car_in=0;
char car_out;
char a,b;

unsigned int mask=0x00000001;
int result=0;

for(i=0;i<32;i++){

  a=(x&mask)!=0;
  b=(y&mask)!=0;
  car_out=car_in & (a|b) |a&b;
  sum=a^b^car_in;

  if(sum) {
     result|=mask;
  }

  if(i!=31) {
     car_in=car_out;
  } else {
     if(car_in!=car_out) {
        printf("Overflow occurred\n");
     }
  }

  mask<<=1;
  }

 return result;
  }
  • 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-06-11T17:11:55+00:00Added an answer on June 11, 2026 at 5:11 pm

    Well, subtracting in bitwise operations without the + or - operators is slightly tricky, but can be done. You have the basic idea with the complement, but without using + it becomes slightly tricky.

    You can do it by first setting up addition with bit-wise only, then using that, you can do subtraction. Which is used for the complement, So the code looks like this:

    int badd(int n1, int n2){
        int carry, sum;
        carry = (n1 & n2) << 1; // Find bits that are used for carry
        sum = n1 ^ n2; // Add each bit, discard carry.
        if (sum & carry) // If bits match, add current sum and carry.
            return badd(sum, carry);
        else
            return sum ^ carry; // Return the sum.
    }
    
    int bsub(int n1, int n2){
        // Add two's complement and return.
        return badd(n1, badd(~n2, 1));
    }
    

    And then if we use the above code in an example:

    int main(){
    printf("%d\n", bsub(53, 17));
    return 0;
    }
    

    Which ends up returning 36. And that is how subtraction works with bitwise only operations.

    Afterwards multiplication and division get more complicated, but can be done; for those two operations, use shifts along with addition and/or subtraction to get the job done. You may also want to read this question and this article on how to do it.

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

Sidebar

Related Questions

Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: Can I have multiple background images using CSS? Is it possible to
Possible Duplicate: Can I have multiple background images using CSS? I am making a
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat
Possible Duplicate: Can I comment a JSON file? We are using a .json file
Possible Duplicate: jQuery - is it bad to have multiple $(document).ready(function() {}); Can you
Possible Duplicate: Multiple forms on ASP.NET page i have a doubt. can we place
Possible Duplicate: Can I multiply strings in java to repeat sequences? In Python, we
Possible Duplicate: Can Super deal with multiple inheritance? Python inheritance? I have a class
Possible Duplicate: Can you have multiple $(document).ready(function() sections? Is it OK to have multiple

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.