2

I wrote a program that performs certain operations (AND, OR, NOR, NAND, NOT, XOR) on two binary numbers and returns the result. I used a stringbuilder to produce the result. The program's method uses a for loop to look through each character in BinaryA (because they both have the same length) and then uses an algorithim based on a truth table to get the result.

The only issue i'm having is that the result keeps outputting either 00000000 or 11111111 as a result. I'm not sure what my logic issue is:

public class Driver {

public static void main(String[] args) {
    String Operator = "AND";
    String binA = "10110101";
    String binB = "00110001";

    System.out.println(Operator + " and binary numbers " + binA + " and " + binB + " result in the output " + doOperation(Operator, binA, binB));

}

public static StringBuilder doOperation(String Operator, String BinaryA, String BinaryB)
{
    StringBuilder result = new StringBuilder();

    for(int i = 0; i < BinaryA.length(); i++)
    {
        if(Operator.equals("AND"))
        {
            if(BinaryA.equals("1") && BinaryB.equals("1"))
                result.append('1');
            else
                result.append('0');
        }
        else if (Operator.equals("OR"))
        {
            if(BinaryA.equals("0") && BinaryB.equals("0"))
                result.append('0');
            else
                result.append('1');

        }
        else if (Operator.equals("NAND"))
        {
            if(BinaryA.equals("1") && (BinaryB.equals("1")))
                result.append('0'); 
            else
                result.append('1');
        }
        else if (Operator.equals("NOR"))
        {
            if(BinaryA.equals("0") && (BinaryB.equals("0")))
                result.append('1');
            else
                result.append('0');
        }
        else if (Operator.equals("XOR"))
        {
            if(BinaryA.equals("1") && BinaryB.equals("0") || (BinaryA.equals("0")) && BinaryB.equals("1"))
                result.append('1');
            else
                result.append('0');

        }
    }
    return result;

}

}

OUTPUT: AND and binary numbers 10110101 and 00110001 result in the output 00000000

1
  • 1
    Parsing them as numbers would be a lot more efficient and less error-prone. Commented Feb 7, 2015 at 21:02

2 Answers 2

4

This won't work, BinaryA and BinaryB are Strings, but you want to compare individual chars:

if (BinaryA.equals("1") && BinaryB.equals("1"))

Instead, you should do a char-by-char traversal, something like this:

if (BinaryA.charAt(i) == '1' && BinaryB.charAt(i) == '1')

Change all the comparisons so they operate on the single char pointed by the current i index.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect!!!!! I made Such a silly mistake, but it happens to the best of us I suppose.
2

You should try this :

if(Operator.equals("AND"))
    {
        if(BinaryA.charAt(i)=='1'&&  BinaryB.charAt(i)=='')
            result.append('1');
        else
            result.append('0');
    }
    else if (Operator.equals("OR"))
    {
        if(BinaryA.charAt(i)=='0' && BinaryB.charAt(i)=='0')
            result.append('0');
        else
            result.append('1');

    }

1 Comment

you are right but i think is 1 not 0, any way. you are welcomed @c0der!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.