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!
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more