(USING C PROGRAMMING & VISUAL STUDIOS) 3) (5 pts) Bits : Write a program that re
ID: 2266262 • Letter: #
Question
(USING C PROGRAMMING & VISUAL STUDIOS)
3) (5 pts) Bits: Write a program that reads two integers from the user and prints the results of applying the negation (~) operator to each, and the results of applying the bitwise and, or, xor, left shift, and right shift operators to the pair. It should accept negative inputs as well as positive. Show the results as hex values and as ints.
If the user types something other than numbers, print an error message.
For example (user input shown underlined):
Enter 2 integers: 5 10
~5 == 0xfffffffa == -6
~10 == 0xfffffff5 == -11
5 or 10 == 0x0000000f == 15
5 and 10 == 0x00000000 == 0
5 xor 10 == 0x0000000f == 15
5 << 10 == 0x00001400 == 5120
5 >> 10 == 0x00000000 == 0
Explanation / Answer
#include<stdio.h>
int main() {
long int decimalNumber_1,decimalNumber_2,remainder,quotient;
int i=1,j,temp;
printf("Enter any decimal number 1: "); //INput NUmber 1
scanf("%ld",&decimalNumber_1);
printf("Enter any decimal number 2: "); //INput NUmber 2
scanf("%ld",&decimalNumber_2);
printf("Equivalent hexadecimal number is: %X",decimalNumber_1);
printf("Equivalent hexadecimal number is: %X",decimalNumber_2);
printf("Complement Number _1 %X",~decimalNumber_1); // output = ~decimalNumber_1
printf("Complement Number _2 %X",~decimalNumber_2); // output = ~decimalNumber_2
printf("OR operation %X",(decimalNumber_1 | decimalNumber_2)); // output = decimalNumber_1 OR decimalNumber_2
printf("AND operation %X",(decimalNumber_1 & decimalNumber_2)); // output = decimalNumber_1 AND decimalNumber_2
printf("EX-OR operation %X",(decimalNumber_1 ^ decimalNumber_2)); // output = decimalNumber_1 EX_OR decimalNumber_2
printf("Left Shift operation %X",(decimalNumber_1*2)); // output = decimalNumber_1 << 1
printf("Right Shift operation %X",(decimalNumber_1/2)); // output = decimalNumber_1 >> 1
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.