PROGRAM DESCRIPTION:

In this recitation assignment, will write a complete C program that will prompt the user to enter an ASCII printable character and then print out its decimal (base 10), hexadecimal (base 16) and all 8 binary digits of its binary (base 2) representation.

You may assume that the user enters a valid ASCII printable character when prompted. Examples of printable characters include A, B, C, #, $, &, +, 0, 1, 2, <, a, b, c, and so forth, and they typically range in decimal value from 33 to 126. You do not have to do any error checking for non-printable characters.

Although not required, you may find Chapter 2 on Bits, Bytes, and Data Types in the System Programming with C and Unix optional reference textbook by Adam Hoover to be helpful.

For this recitation assignment, complete the following tasks. You may receive guidance from your TA or fellow students.

1. Prompt the user to enter a printable ASCII character using printf and then read in the user's response using scanf, storing your character as an unsigned char.

2. For the decimal and hexadecimal bases, simply take advantage of the format specifiers in printf to print the decimal and hexadecimal representations. See man 3 printf or any of the reference or tutorial material provided on Canvas for help if you're having trouble with this.

3. Although there are several ways to accomplish printing out the binary representation, you are being asked to accomplish this functionality using bitwise operators as described below:

  • Since it binary representation can only be 8 digits in length, use a for loop that decrements the control variable from 7 down to 0, inclusively.
  • The right shift operator (>>) is used to shift bits to the right. Use this operator to right shift the printable character by the amount in the control variable (i.e., the number being decremented in each iteration of the for loop) and store in a temporary integer variable.
  • Next, the bitwise and operator (&) will set a bit to 1 if and only if both of the corresponding bits in the operands are 1. For example, if x=0110 and y=1011, then x&y=0010. Now, if the bitwise and of the temporary integer variable from (b) above and the integral literal value 1 is true, simply print "1" (with no newline) to the terminal; otherwise, print 0 (with no newline). After all 8 binary digits are printed (including leading 0s), then print a newline to the terminal.

SAMPLE OUTPUT

$ ./ a.out
Enter an ASCII character: A The ASCII value
of A is:
dec -- 65
hex -- 41
bin -- 01000001
$ ./ a.out
Enter an ASCII character: a The ASCII value
of a is:
dec -- 97
hex -- 61
bin -- 01100001
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.