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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:58:03+00:00 2026-05-14T00:58:03+00:00

On paper, binary arithmetic is simple, but as a beginning programmer, I’m finding it

  • 0

On paper, binary arithmetic is simple, but as a beginning programmer, I’m finding it a little difficult to come up with algorithms for the addition, subtraction, multiplication and division of binary numbers.

I have two binary numbers stored as strings, assume that any leading zeroes have been dropped. How would I go about performing these operations on the two numbers?

Edit: I need to avoid converting them to an int or long.

  • 1 1 Answer
  • 1 View
  • 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-14T00:58:03+00:00Added an answer on May 14, 2026 at 12:58 am

    The following code implements binary addition without actually doing any arithmetic, binary or otherwise. The actual “addition” is done by lookupTable, and everything else is straight-up string manipulation. I wrote it with the intention of making it as instructive as possible, emphasizing the process instead of efficiency. Hope it helps.

    public class BinaryArithmetic {
        static String[] lookupTable = {
            "0+0+0=00",
            "0+0+1=01",
            "0+1+0=01", 
            "0+1+1=10",
            "1+0+0=01",
            "1+0+1=10",
            "1+1+0=10",
            "1+1+1=11",
        };
        static String lookup(char b1, char b2, char c) {
            String formula = String.format("%c+%c+%c=", b1, b2, c);
            for (String s : lookupTable) {
                if (s.startsWith(formula)) {
                    return s.substring(s.indexOf("=") + 1);
                }
            }
            throw new IllegalArgumentException();
        }
        static String zeroPad(String s, int length) {
            while (s.length() < length) {
                s = "0" + s;
            }
            return s;
        }   
        static String add(String s1, String s2) {
            int length = Math.max(s1.length(), s2.length());
            s1 = zeroPad(s1, length);
            s2 = zeroPad(s2, length);
            String result = "";
            char carry = '0';
            for (int i = length - 1; i >= 0; i--) {
                String columnResult = lookup(s1.charAt(i), s2.charAt(i), carry);
                result = columnResult.charAt(1) + result;
                carry = columnResult.charAt(0);
            }
            if (carry == '1') {
                result = carry + result;
            }
            return result;
        }
        public static void main(String args[]) {
            System.out.println(add("11101", "1001"));
        }
    }
    

    While we’re at it, I might as well do multiply too.

    static String multiply(String s1, String s2) {
        String result = "";
        String zeroSuffix = "";
        for (int i = s2.length() - 1; i >= 0; i--) {
            if (s2.charAt(i) == '1') {
                result = add(result, s1 + zeroSuffix);
            }
            zeroSuffix += "0";
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Standardizing the strength of passwords using relative terms like "weak"… May 17, 2026 at 12:27 am
  • Editorial Team
    Editorial Team added an answer I bet you are violating the same origin policy. For… May 17, 2026 at 12:26 am
  • Editorial Team
    Editorial Team added an answer The reason these types of errors are not caught by… May 17, 2026 at 12:26 am

Trending Tags

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

Top Members

Related Questions

This is a past exam paper on binary search trees I am attempting. I
Here is a paper, it is about estimating the perspective of binary image containing
You know, like Battlestar paper! I have given this a few goes but now
My C++ program creates a binary search tree. I know how to print out
How can I determine the statistical randomness of a binary string? Ergo, how can
Consider the problem of counting the number of structurally distinct binary search trees :
Let's say you have a List<List<Boolean>> and you want to encode that into binary
ok i know this is stupid but atleast im trying :) $result = shell_exec('C:/cygwin/bin/bash.exe
I'm reading a paper that talks about using a lerp function in image synthesis.
A landmark paper entitled Relativisations of the P =? NP Question by Theodore Baker,

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.