Question

JAVA: Write a simple program that takes as input 2 16-bit strings, X and Y. Compute...

JAVA: Write a simple program that takes as input 2 16-bit strings, X and Y. Compute and and display the bitwise AND, bitwise OR, and bitwise XOR of the two strings.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Bitwise {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter first 16-bit number: ");
        String X = in.next();
        System.out.print("Enter second 16-bit number: ");
        String Y = in.next();
        String xor="", or="", and="";
        char c1,c2;
        for(int i = 0; i < X.length(); i++) {
            c1 = X.charAt(i);
            c2 = Y.charAt(i);
            if(c1 == '0' || c2 == '0') {
                and += "0";
            } else {
                and += "1";
            }
            if(c1 == '0' && c2 == '0') {
                or += "0";
            } else {
                or += "1";
            }
            if((c1 == '0' && c2 == '1') || (c1 == '1' && c2 == '0')) {
                xor += "1";
            } else {
                xor += "0";
            }
        }
        System.out.println("AND = " + and);
        System.out.println("OR = " + or);
        System.out.println("XOR = " + xor);
    }

}

Add a comment
Know the answer?
Add Answer to:
JAVA: Write a simple program that takes as input 2 16-bit strings, X and Y. Compute...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string...

    JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string of someone's full name and prints it out last name first. You may use the available string manipulation methods. Example: "George Washington" "Washington, George" 2. Write a program that will take a string of someone's full name, and break it into separate strings of first name and last name, as well as capitalize the first letter of each. Example: "joseph smith", "Joseph" "Smith"

  • Write a simple java program that takes input from the user as a fully parenthesized expression...

    Write a simple java program that takes input from the user as a fully parenthesized expression and converts it to a binary tree. Ex: (1+2 * (6-4)) or (A+B / (D-C)) When the tree is built, it should print the tree in some way.

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • Write an algorithm that takes two strings X and Y and a positive integer k as...

    Write an algorithm that takes two strings X and Y and a positive integer k as input, and determines whether Y can be turned into X by at most k insertions or deletions. For example: let X = “abacad", Y = “cebacad" and k = 3 then your algorithm should give a yes answer because Y can be turned into X in 3 steps by a left-to-right processing as follows: delete leftmost c delete “e" insert “a" Give an tight...

  • JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The...

    JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The method should display the contents of the pets array on one line. Each name should be separated by a space. Use an enhanced for loop to iterate the array. Make sure to declare modifier, parameters and return values. Given an array String[] pets = {"Lucky", "Slinger", "Beast", "Trumpet", "Lulu", "Shadow", "Daisy"} A sample run when calling displayPets(pets) from main() would look like: Pets names:...

  • **IN JAVA** Many user-created passwords are simple and easy to guess. Write a program that takes...

    **IN JAVA** Many user-created passwords are simple and easy to guess. Write a program that takes a simple password and makes it stronger by replacing characters using the key below, and by appending "q*s" to the end of the input string. i becomes ! a becomes @ m becomes M B becomes 8 o becomes . Ex: If the input is: mypassword the output is: Myp@ssw.rdq*s

  • Write A Program called Math a. Takes as input two Strings from the Command Line args[0],...

    Write A Program called Math a. Takes as input two Strings from the Command Line args[0], args[1] b. Convert these two strings to doubles. c. Add them together. d. If the second number is no-Zero divide them. e. Report on the results.

  • Using Java write a program that takes a string input from the user and then outputs...

    Using Java write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word.

  • In JAVA: Write the definition/implementation of a method named MyMethod that takes 2 strings and return...

    In JAVA: Write the definition/implementation of a method named MyMethod that takes 2 strings and return the total length of the 2 strings. For example, if the method is called with strings “hello” and “there”, the method returns 10. Show how you would call this method (I don't need the full program, just a proper statement to call the method).

  • Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from...

    Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT