Question

In: Computer Science

using C language Create a bitwise calculator that ask user for input two input, first int,...

using C language

Create a bitwise calculator that ask user for input two input, first int, bitwise operation, second int (i.e 5 & 9)

only those bitwise operation are allowed: & ~ ^ | << >>. If user uses wrong operators stop program and ask again.

Convert the first int and second int into 8 BITS binary (00000000) and use bitwise operator given by user to either AND, OR, XOR, etc (ie 1001 & 1111)

Solutions

Expert Solution

#include <stdio.h>

#define bits 8

int main(void) {

   // declare variables
   int a, b;
   char op;
   int binA[bits] = {0, 0, 0, 0, 0, 0, 0, 0}; 
   int binB[bits] = {0, 0, 0, 0, 0, 0, 0, 0};
   int res[bits];
    int i = bits - 1;
   int temp;


   // read numbers
   printf("Enter first operand: ");
   scanf("%d", &a);
   printf("Enter second operand: ");
   scanf("%d", &b);

   // read operator
   printf("Enter operation (&, ~, ^, |, <<, >>) (enter < for << and > for >>): ");
   scanf(" %c", &op);

   // operator validation
   while (op != '&' && op != '~' && op != '^' && op != '<' && op != '>' && op != '|') {
      printf("Invalid operation!!");

      printf("\nEnter operation (&, ~, ^, |, <<, >>) (enter < for << and > for >>): ");
      scanf("%c", &op);
   }
   

   // converting first number to binary
   temp = a;
    while (temp > 0) { 
        binA[i] = temp % 2; 
        temp = temp / 2; 
        i--;
    }

   // converting second number to binary
    i = bits - 1; 
   temp = b;
    while (temp > 0) { 
        binB[i] = temp % 2; 
        temp = temp / 2; 
        i--; 
    }


   // display the bits
   printf("\n%d in %d-bits binary = ", a, bits);
   for(i = 0; i < bits; i++) {
      printf("%d", binA[i]);
   }

   printf("\n%d in %d-bits binary = ", b, bits);
   for(i = 0; i < bits; i++) {
      printf("%d", binB[i]);
   }

   // apply the operation accordingly
   // and store the result in the res array
   if (op == '&') {
      for (i = 0; i < bits; i++) {
         res[i] = binA[i] & binB[i];
      }
   }
   else if (op == '^') {
      for (i = 0; i < bits; i++) {
         res[i] = binA[i] ^ binB[i];
      }
   }
   else if (op == '|') {
      for (i = 0; i < bits; i++) {
         res[i] = binA[i] | binB[i];
      }
   }
   else if (op == '<') {
      for (i = 0; i < bits; i++) {
         res[i] = binA[i] << binB[i];
      }
   }
   else if (op == '>') {
      for (i = 0; i < bits; i++) {
         res[i] = binA[i] >> binB[i];
      }
   }
   else {
      for (i = 0; i < bits; i++) {
         if (binA[i] == 1) {
            binA[i] = 0;
         }
         else {
            binA[i] = 1;
         }

         if (binB[i] == 1) {
            binB[i] = 0;
         }
         else {
            binB[i] = 1;
         }
      }
   }


   // display result 

   // if operation is ~ we display both numbres after applying ~ opertaion
   if (op == '~') {
      printf("\n\nResult\n");

      printf("~%d = ", a);
      for (i = 0; i < bits; i++) {
         printf("%d", binA[i]);
      }

      printf("\n~%d = ", b);
      for (i = 0; i < bits; i++) {
         printf("%d", binB[i]);
      }
   }
   
   else if (op == '<') {
      printf("\n\nResult\n");
      printf("%d << %d = ", a, b);
      for (i = 0; i < bits; i++) {
         printf("%d", res[i]);
      }
   }

   else if (op == '>') {
      printf("\n\nResult\n");
      printf("%d >> %d = ", a, b);
      for (i = 0; i < bits; i++) {
         printf("%d", res[i]);
      }
   }

   // else display the result
   else {
      printf("\n\nResult\n");
      printf("%d %c %d = ", a, op, b);
      for (i = 0; i < bits; i++) {
         printf("%d", res[i]);
      }
   }

   return 0;
}

FOR HELP PLEASE COMMENT.
THANK YOU


Related Solutions

Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this: int x = scan.nextInt(); ☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using scan.nextDouble();...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Code using assembly language Create a program using the Irvine32 procedures were the user can input...
Code using assembly language Create a program using the Irvine32 procedures were the user can input a list of 32-bit unsigned integers an “x” number of times, then display these integers to the console in reverse order. Hint: Use loops and PUSH & POP instructions. Extra Challenge: Inform the user with a message what to do; also, tell them what they are seeing.
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
C LANGUAGE Ask user how many scores there are, then ask for each score. Then calculate...
C LANGUAGE Ask user how many scores there are, then ask for each score. Then calculate the average score and scores below 60. Then display the average score and number of scores below 60
Language C++ Ask the user to enter their weight (in pounds) and their height in inches....
Language C++ Ask the user to enter their weight (in pounds) and their height in inches. Calculate their Body Mass Index (BMI) and tell them whether they are underweight, overweight, or at optimal weight. BMI formula: weight * 703 / (height * height) Optimal weight is a BMI from 19 to 26. Lower is underweight, higher is overweight. Prompts: Enter your weight (in pounds): [possible user input: 144] Enter your height (in inches): [posible user input: 73] Possible Outputs: Your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT