Write a program that allows the user to enter an unsigned integer (the maximum v
ID: 3669869 • 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 and try to use the constants / code given! :)
Byte (in hex)
15
EC
65
01
Byte Address
a
a+1
a+2
a+3
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
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);
// helper to get hexadecimal char
char getHexChar(unsigned char);
int main() {
// prompt user to take input in base 10
unsigned int userInput;
cout << "Enter an unsigned integer in base 10 => ";
cin >> userInput;
// declare integer4 representation of number
integer4 num;
// set its integer representation as current number
num.intrep = userInput;
// iterate and grab 8-bits (byte) and add it to byterep of representation
for (int i = 0; i < INTSIZE; ++i) {
// grab last 8 bits using 11111111 with prefixed with 0s
num.byterep[i] = (userInput & ((((unsigned int) 1) << BYTESIZE) - 1));
// chop of last 8 bits (a byte)
userInput >>= BYTESIZE;
}
///////////////////////////////////////////////////////////
cout << "Hexadecimal: ";
for (int i = 0; i < INTSIZE; ++i) {
// print hexadecimal
prHex(num.byterep[i]);
// separate bytes with space
cout << ' ';
}
cout << endl;
cout << "Binary: ";
for (int i = 0; i < INTSIZE; ++i) {
// print binary
prBin(num.byterep[i]);
// separate bytes with space
cout << ' ';
}
cout << endl;
////////////////////////////////////////////////////////////
cout << "Reverse Endian Representation: " << endl;
cout << "Hexadecimal: ";
for (int i = INTSIZE - 1; i >= 0; --i) {
// print hexadecimal
prHex(num.byterep[i]);
// separate bytes with space
cout << ' ';
}
cout << endl;
cout << "Binary: ";
for (int i = INTSIZE - 1; i >= 0; --i) {
// print binary
prBin(num.byterep[i]);
// separate bytes with space
cout << ' ';
}
cout << endl;
return 0;
}
void prHex(unsigned char byte) {
// get last 4 bits
unsigned char firstNib = (byte & ((((unsigned int) 1) << NIBSIZE) - 1));
// get first 4 bits
unsigned char secondNib = (byte >> NIBSIZE);
// print it!
cout << getHexChar(secondNib) << getHexChar(firstNib);
}
void prBin(unsigned char byte) {
// iterate from 8th to 1st bit and print it
for (int i = BYTESIZE - 1; i >= 0; --i) {
// check bit at ith position and print it
if ((byte & (1 << i)) != 0) cout << '1';
else cout << '0';
}
}
char getHexChar(unsigned char x) {
// by definition of hexadecimal chars
if (x >= 0 && x <= 9) return char('0' + x);
return char('A' + (x - 10));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.