Answered! Write a program (in java) that takes two binary strings and returns three binary strings. These three strings…

Write a program (in java) that takes two binary strings and returns three binary strings. These three strings represent the following binary operations between the input strings: AND, OR, XOR (make sure it works for strings of different length)

Here are two examples:

Input:

1001

1101

Output:

AND:     1001

OR:         1101

XOR:      0100

Input:

101101

1001

Output:

AND:     1001

OR:         101101

XOR:      100100

Expert Answer

Hi,

The desired operations are already present in java, here is a list of all the operators avaiable in java.

Operator    Name         Example     Result  Description
a & b       and          3 & 5       1       1 if both bits are 1.
a | b       or           3 | 5       7       1 if either bit is 1.
a ^ b       xor          3 ^ 5       6       1 if both bits are different.
~a          not          ~3          -4      Inverts the bits.

So, we can use this to get the required output, the only challenge being, we have to deal with strings, for that too we have the Integer class in java which can convert string to int, here is the complete solution.

import java.util.*;
import java.lang.*;
import java.io.*;

class Test
{
public static void main (String[] args) throws java.lang.Exception
{
String s1 = “101101”;
String s2 = “1001”;

//    INTEGER.PARSEINT(S,2) CONVERTS STRING S TO INTEGER
Integer xor=Integer.parseInt(s1, 2) ^ Integer.parseInt(s2, 2); // ^ IS FOR XOR
Integer or=Integer.parseInt(s1, 2) | Integer.parseInt(s2, 2);
Integer and=Integer.parseInt(s1, 2) & Integer.parseInt(s2, 2);

// INTEGER.toBinaryString DOES THE OPPOSITE OF WHAT WE DID ABOVE
System.out.println(“XOR:”+Integer.toBinaryString(xor));
System.out.println(“OR:”+Integer.toBinaryString(or));
System.out.println(“AND:”+Integer.toBinaryString(and));
}
}

Hope this was helpful let me know if you have any questions.

 

 

 

 

 

Hi,

The desired operations are already present in java, here is a list of all the operators avaiable in java.

Operator    Name         Example     Result  Description
a & b       and          3 & 5       1       1 if both bits are 1.
a | b       or           3 | 5       7       1 if either bit is 1.
a ^ b       xor          3 ^ 5       6       1 if both bits are different.
~a          not          ~3          -4      Inverts the bits.

So, we can use this to get the required output, the only challenge being, we have to deal with strings, for that too we have the Integer class in java which can convert string to int, here is the complete solution.

import java.util.*;
import java.lang.*;
import java.io.*;

class Test
{
public static void main (String[] args) throws java.lang.Exception
{
String s1 = “101101”;
String s2 = “1001”;

//    INTEGER.PARSEINT(S,2) CONVERTS STRING S TO INTEGER
Integer xor=Integer.parseInt(s1, 2) ^ Integer.parseInt(s2, 2); // ^ IS FOR XOR
Integer or=Integer.parseInt(s1, 2) | Integer.parseInt(s2, 2);
Integer and=Integer.parseInt(s1, 2) & Integer.parseInt(s2, 2);

// INTEGER.toBinaryString DOES THE OPPOSITE OF WHAT WE DID ABOVE
System.out.println(“XOR:”+Integer.toBinaryString(xor));
System.out.println(“OR:”+Integer.toBinaryString(or));
System.out.println(“AND:”+Integer.toBinaryString(and));
}
}

Hope this was helpful let me know if you have any questions.

Still stressed from student homework?
Get quality assistance from academic writers!