Question

In: Computer Science

Have to use C language Pseudocode A #define called BITS should be set at the top...

Have to use C language
Pseudocode
A #define called BITS should be set at the top of the program. It should be set to 8 when the program is submitted. This
define should be used throughout the entire program when setting/using array sizes/max element. This define will also
be used in the output to print 8-bit vs 16 bit. Your program should function whether the define is set to 8 or to 16.
Part of the grading process will be to change the define from 8 to 16 and to recompile your program to see if your program still
runs properly. See sample output.
main()
Print instructions (see sample output below)
Prompt for and store both numbers and the operator. Use only one scanf() to store all three values.
While either number is less than 0 or more than 255, continue to prompt for input until a valid number is entered (hint
– use a while loop). See sample output below.
Call function ConvertDecimalToBinary() to convert the first number to binary.
Call function ConvertDecimalToBinary() to convert the second number to binary.
If the entered operator is an allowed operator, then convert the result to binary and print the decimal result and
binary result as seen in the sample code.
ConvertDecimalToBinary()
Return type : void
Parameters :
int containing the decimal value to be converted
char array (hint : the array is passed empty from main() and this function fills it so when the function
finishes, the array back in main() will contain the values added in the function).
This function will use a method of decimal to binary conversion called “Divide in Half, Ignore the Remainder”. Please
watch the following video for a demonstration of the method.
https://youtu.be/XdZqk8BXPwg
Create a local int array. This array will store the result of each divide by 2 which will be accomplished using
bitshifting instead of division to divide the number in half. Use a bitmask to determine if an array element if odd (1) or
even (0). YOU MUST USE THIS METHOD IN THIS ASSIGNMENT.
Using a for loop, loop over the int array and write each element into the char array that was passed in. Hint : keep
in mind what the ASCII value is for the number zero when writing the int array element into the char array. If you
store the number 65 in a char, it will be ‘A’ so if you want to store the number 0 in a char array, you will need to …?
HINT : make sure your char arrays are one bigger than the number of BITS so that you have room for the null terminator so
that %s prints correctly.
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 2 & 3
In base 10...
2 & 3 = 2
In 8-bit base 2...
00000010
&
00000011
========
00000010
--------------------------------------------------------------------------------------
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 2 | 3
In base 10...
2 | 3 = 3
In 8-bit base 2...
00000010
|
00000011
========
00000011
--------------------------------------------------------------------------------------
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 2 ^ 3
In base 10...
2 ^ 3 = 1
In 8-bit base 2...
00000010
^
00000011
========
00000001
--------------------------------------------------------------------------------------
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 2 << 3
In base 10...
2 << 3 = 16
In 8-bit base 2...
00000010 << 3
00010000
--------------------------------------------------------------------------------------
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 2 >> 3
In base 10...
2 >> 3 = 0
In 8-bit base 2...
00000010 >> 3
00000000
--------------------------------------------------------------------------------------
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 100 >> 3
In base 10...
100 >> 3 = 12
In 8-bit base 2...
01100100 >> 3
00001100
--------------------------------------------------------------------------------------
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 2 * 3
Operator * is not supported by this calculator
Change
#define BITS 8
to
#define BITS 16
Bitwise Calculator
Enter two base 10 values with a bitwise operator to see the decimal result
and the binary result. The format is
FirstNumber BitwiseOperator SecondNumber
For example, enter the expression
2 & 3
This calculator can used with &, |, ^, << and >>
Please note that the spaces between numbers and operator is essential
and the two entered values must be between 0 and 255
Enter expression 4 & 6
In base 10...
4 & 6 = 4
In 16-bit base 2...
0000000000000100
&
0000000000000110
========
0000000000000100

If you have questions please ask .. Thank you.

Solutions

Expert Solution

#include <stdio.h>

#define BITS 8

char binary[BITS + 1];

int operator[]={38,60,62,94,124};

void convertDecimalToBinary(int num)

{

int i = 0;

while(i < BITS)

{

if((num & (1 << i) )== (1 << i))

binary[BITS - i - 1] = '1';

else

binary[BITS - i - 1] = '0';

i++;

}

binary[BITS] = '\0';

}

void bitwiseCalc()

{

int k = 0,j = 0;

char expr[10];

int firstNum=0,secondNum=0,bitwiseOperator,resultNum;

char firstNumber[BITS+1],secondNumber[BITS+1],resultNumber[BITS+1];

printf("Enter the expression");

gets(expr);

puts(expr);

while(expr[j] != 32)

{

firstNum += expr[j++] - 48;

}

for(int i = 0; i < 5;i++)

{

if(expr[j+1] == operator[i])

{

bitwiseOperator = expr[j+1];

if(expr[j+1] == 60 || expr[j+1] == 62)

{

for(int z = j+4;expr[z] != '\0';z++)

{

secondNum = secondNum*10 + expr[z] - 48;

}

}

break;

}

}

if (expr[j+1] != 60 && expr[j+1] != 62)

{

for(int z = j+3;expr[z] != '\0';z++)

{

secondNum = secondNum*10 + expr[z] - 48;

}

}

convertDecimalToBinary(firstNum);

for(int x = 0; x <= BITS + 1;x++)

{

firstNumber[x] = binary[x];

}

convertDecimalToBinary(secondNum);

for(int x = 0; x <= BITS + 1;x++)

{

secondNumber[x] = binary[x];

}

printf("\nIn base 10...\n");

if(bitwiseOperator == 60 ||bitwiseOperator == 62)

printf("%d %c%c %d",firstNum,bitwiseOperator,bitwiseOperator,secondNum);

else

printf("%d %c %d",firstNum,bitwiseOperator,secondNum);

for(int i = 0 ; i < 5 ; i++)

{

if(bitwiseOperator == operator[i])

switch(i)

{

case 0:

resultNum = firstNum & secondNum;

break;

case 1:

resultNum = firstNum << secondNum;

break;

case 2:

resultNum = firstNum >> secondNum;

break;

case 3:

resultNum = firstNum ^ secondNum;

break;

case 4:

resultNum = firstNum | secondNum;

break;

}

}

printf(" = %d\n",resultNum);

convertDecimalToBinary(resultNum);

for(int x = 0; x <= BITS + 1;x++)

{

resultNumber[x] = binary[x];

}

printf("In %d-bit base 2...\n",BITS);

for(int i = 0;i < BITS;i++)

{

printf("%c",firstNumber[i]);

}

if(bitwiseOperator == 60 || bitwiseOperator == 62)

{

printf(" %c%c %d\n",bitwiseOperator,bitwiseOperator,secondNum);

}

else

{

printf("\n %c\n",bitwiseOperator);

for(int i = 0;i < BITS;i++)

{

printf("%c",secondNumber[i]);

}

}

printf("\n========\n");

for(int i = 0;i < BITS;i++)

{

printf("%c",resultNumber[i]);

}

}

int main(){

bitwiseCalc();

}


Related Solutions

Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Also please discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
In the R programming language, we would like to use the data set called iris to...
In the R programming language, we would like to use the data set called iris to build a simple linear regression model to predict Sepal.Length based on Petal.Length. Calculate the least squares regression line to predict Sepal.Length based on Petal.Length. Interpret the slope of the line in the context of the problem. Remember that both variables are measured in centimeters. Plot the regression line in a scatterplot of Sepal.Length vs. Petal.Length. Test H1: ??1 ≠ 0 at ?? = 0.05...
Using C (not C++): setFirst - returns value with n upper bits set to 1 and...
Using C (not C++): setFirst - returns value with n upper bits set to 1 and 32-n lower bits set to 0 * You may assume 0 <= n <= 32 * Example: setFirst(4) = 0xF0000000 * Legal ops: ! ~ & ^ | + << >> (NO IF OR FOR LOOPS) * Max ops: 10 * Rating: 2
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should have #two parameters: a dictionary and a string. The string may be #a key to the dictionary. The value associated with that key, #in turn, may be another key to the dictionary. # #Keep looking up the keys until you reach a key that has no #associated value. Then, return that key. # #For example, imagine if you had the following dictionary. #This one...
hello i have question in c++ language Q1: create a class called RightTriangleShape, and it has...
hello i have question in c++ language Q1: create a class called RightTriangleShape, and it has data member called height which initialized to 3 by the constructor of the class. It has also the following function members: Void setHeight() to read, and set the height. Void getHeight() to print the value of height. void leftBottom_RTraingle(), prints shape a void leftTop_RTraingle(), prints shape b. void RightTop_RTraingle(), prints shape c. void RigtBottom_RTraingle(), prints shape d. The functions from 3 to 6 have...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT