Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class BitStrings {
- static int commArray[];
- static String bitStringOne;
- static String bitStringTwo;
- static String operator = " ";
- static String command;
- static String promptOne = "\nPlease enter an 8-bit string: ";
- static String promptTwo = "\nPlease enter a command: ";
- static String[] tokens = new String[2];
- static Scanner in = new Scanner(System.in);
- public static void main(String[] args) {
- while (true)
- {
- System.out.println(promptOne);
- bitStringOne = in.next();
- // Place scanner on the next line. If we don't do this, the next time we call nextLine(), we will get null.
- in.nextLine();
- // If the bit string is 8 bits long, continue.
- if (bitStringOne.length() == 8)
- {
- while (promptUser())
- {
- }
- }
- // Otherwise, prompt the user again.
- else
- {
- System.out.println("The string is not 8 bits long!");
- }
- }
- }
- public static boolean promptUser()
- {
- System.out.println(promptTwo);
- command = in.nextLine();
- // If command contains a space character, split it into tokens.
- if (command.indexOf(' ') != -1)
- {
- tokens = command.split(" ");
- operator = tokens[0];
- bitStringTwo = tokens[1];
- // If the bit string is not 8 bits long, prompt user again.
- if (bitStringTwo.length() != 8)
- {
- System.out.println("The string is not 8 bits long!");
- return true;
- }
- // If the operator is 'and', perform AND operation.
- if (operator.equalsIgnoreCase("and"))
- {
- AND(bitStringOne, bitStringTwo);
- }
- // If the operator is 'or', perform OR operation.
- else if (operator.equalsIgnoreCase("or"))
- {
- OR(bitStringOne, bitStringTwo);
- }
- // If the user enters an invalid command, prompt again.
- else
- {
- System.out.println("Invalid command!");
- return true;
- }
- }
- else
- {
- // If user types 'q', exit the program.
- if (command.equalsIgnoreCase("q"))
- {
- System.exit(0);
- }
- // If user types 'n', prompt the user for a new first bitstring.
- else if (command.equalsIgnoreCase("n"))
- {
- return false;
- }
- // If user types 'not', perform NOT operation.
- else if (command.equalsIgnoreCase("not"))
- {
- NOT(bitStringOne);
- }
- // If user types an invalid command, prompt again.
- else
- {
- System.out.println("Invalid command!");
- return true;
- }
- }
- return true;
- }
- public static void AND(String str1, String str2)
- {
- int numArray1[] = new int[str1.length()];
- int numArray2[] = new int[str2.length()];
- int numArray3[] = new int[numArray1.length];
- String output = "";
- for (int k = 0; k < str1.length(); k++) {
- numArray1[k] = Character.digit(str1.charAt(k), 10);
- }
- for (int l = 0; l < str2.length(); l++) {
- numArray2[l] = Character.digit(str2.charAt(l), 10);
- }
- ///////////////////////////////////////////////////////////////////
- for(int i =0; i<numArray1.length; i++){
- if(numArray1[i] == 1 && numArray2[i]== 1){
- numArray3[i] = numArray1[i];
- }
- else
- {
- numArray3[i] = 0;
- }
- }
- for (int i = 0; i< numArray3.length; i++){
- output+= numArray3[i];
- }
- System.out.println("Result of operation AND: " + output);
- }
- public static void OR(String str1, String str2)
- {
- int numArray1[] = new int[str1.length()];
- int numArray2[] = new int[str2.length()];
- int numArray3[] = new int[numArray1.length];
- String output = "";
- for (int k = 0; k < str1.length(); k++) {
- numArray1[k] = Character.digit(str1.charAt(k), 10);
- }
- for (int l = 0; l < str2.length(); l++) {
- numArray2[l] = Character.digit(str2.charAt(l), 10);
- }
- ///////////////////////////////////////////////////////////////////
- for(int i =0; i<numArray1.length; i++){
- if(numArray1[i] == 1 || numArray2[i] == 1){
- numArray3[i] = 1;
- }
- else
- {
- numArray3[i] = 0;
- }
- }
- for (int i = 0; i< numArray3.length; i++){
- output+= numArray3[i];
- }
- System.out.println("Result of operation OR: " + output);
- }
- public static void NOT(String str1)
- {
- int numArray1[] = new int[str1.length()];
- int numArray2[] = new int[numArray1.length];
- String output = "";
- for (int k = 0; k < str1.length(); k++) {
- numArray1[k] = Character.digit(str1.charAt(k), 10);
- }
- // Swap the bits
- for(int i =0; i<numArray1.length; i++){
- if(numArray1[i] == 1){
- numArray2[i] = 0;
- }
- else
- {
- numArray2[i] = 1;
- }
- }
- for (int i = 0; i< numArray2.length; i++){
- output+= numArray2[i];
- }
- System.out.println("Result of operation NOT: " + output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement