Question

Write a program that allows the user to enter an unsigned integer (the maximum value of...

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!

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Write a program that allows the user to enter an unsigned integer (the maximum value of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT