In: Computer Science
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.
#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();
}