Question: Write a print_hex function in C language thatprints a 32-bit word out in hexadecimal, using onlyprintf's %c or %d format specifiers.It works by selecting (using

Write a print_hex function in C language thatprints a 32-bit word out in hexadecimal, using onlyprintf's "%c" or "%d" format specifiers.It works by selecting (using masking and shifting) each 4-bit group of bits in the word and then printing the value of that 4-bit group as a hexdigit (between 0 and F, inclusive).

// complete this constant definition for the mask

#define FOUR_BIT_BASK // FILL IN HERE

// It takes as a parameter a pointer p of type void *, so that p can be

// a pointer to anything (i.e. you can pass in the address of anything).

void print_hex(void *p)

{

// copy the value that p points to into an unsigned integer variable.

unsigned int x = *((unsigned int *) p);

// In a loop, select four bits at a time using a mask. Then, printthe value of that four-bit group using a single hex digit.

// IMPORTANT: Do NOT use a bunch of "if" statements to map thebits to a hex digit. Either use the value of the bits toindex into a an array of characters or the following method:

// - if the value of the bits is between 0 and 9, just print the value

// - otherwise (i.e. the valus is greater than 9, print the value asan ASCII character (you'll need to add something to the value).

} // end of print_hex

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!