Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that allows the user to enter an unsigned integer (the maximum v

ID: 3669693 • Letter: W

Question

Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary.

Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the decimal number 23456789 is 0165EC15 in hex. In little endian format the number is stored in main memory as follows:

Byte (in hex)

15

EC

65

01

Byte Address

a

a+1

a+2

a+3

In big endian format the 4-byte integer is stored with the most significant byte is stored in the least address:

Byte (in hex)

01

65

EC

15

Byte Address

a

a+1

a+2

a+3

You may NOT use the C++ hex formatting capability of the extraction operator (<<). Write your own routines to print a number in hexadecimal and binary using bit operators: <<, >>, &, |, and/or ~.

It is recommended that you use the following constants, a union to represent 4-byte integers, and print prototypes as shown here:

const int INTSIZE = 4; // in bytes

const int BYTESIZE = 8; // in bits

const int NIBSIZE = 4; // nibble, in bits

union integer4 {

   unsigned int intrep;

   unsigned char byterep[INTSIZE];

};

void prHex (unsigned char);

void prBin (unsigned char);

You will find that is very important that you use unsigned chars. In most machine architectures, negative integers are represented in 2s complement format with the most significant bit a 1. To convert a binary (byte) 1, or 00000001, to negative -1 in binary, use the following procedure:

1)find the 1s complement; that is, flip all the 1s and 0s: 11111110;

2)add 1 to the result: 11111110 + 00000001 = 11111111, or 0xFF.

Hence a 0xFF as an unsigned char is 255 (decimal), and 0xFF as a (signed) char is -1 (decimal).

Here is a sample run:

Enter an unsigned integer in base 10 => 23456789

In hex:

15 EC 65 01

In binary:

00010101 11101100 01100101 00000001

Reverse endian:

In hex:

01 65 EC 15

In binary:

00000001 01100101 11101100 00010101

Would be appreciated if you add comments in each line of code!

Byte (in hex)

15

EC

65

01

Byte Address

a

a+1

a+2

a+3

Explanation / Answer

         switch(buffer[i]){
             case '0': printf("0000"); break;
             case '1': printf("0001"); break;
             case '2': printf("0010"); break;
             case '3': printf("0011"); break;
             case '4': printf("0100"); break;
             case '5': printf("0101"); break;
             case '6': printf("0110"); break;
             case '7': printf("0111"); break;
             case '8': printf("1000"); break;
             case '9': printf("1001"); break;
             case 'A': printf("1010"); break;
             case 'B': printf("1011"); break;
             case 'C': printf("1100"); break;
             case 'D': printf("1101"); break;
             case 'E': printf("1110"); break;
             case 'F': printf("1111"); break;
             case 'a': printf("1010"); break;
             case 'b': printf("1011"); break;
             case 'c': printf("1100"); break;
             case 'd': printf("1101"); break;
             case 'e': printf("1110"); break;
             case 'f': printf("1111"); break;
             default: printf(" Invalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
         }
         i++;
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote