Write a c++ program that allows the user to enter an unsigned integer (the maxim
ID: 3669767 • Letter: W
Question
Write a c++ 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.
*** PLLEASE ADD COMMENTS IF POSSIBLE FOR THE FUNCTIONS :)
For this exercise 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
Machine integer representation
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 0165EF15 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
On Intel CPUs little endian format is used. However, many CPUs use big endian format. All 2- and 4-byte integers as integers (as opposed to strings or character arrays) sent over the Internet must be in network standard format, which is big endian.
Byte (in hex)
15
EC
65
01
Byte Address
a
a+1
a+2
a+3
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
void binary(unsigned int u)
{
int upper;
if(u < 256)
upper = 128;
else
upper = 32768;
cout << setw(5) << u << ": ";
// check if bit is set starting from the highest bit
// (ex) upper = 128, 10000000, 01000000, 00100000, ..., 00000001
for(int i = upper; i > 0; i = i/2) {
if(u & i)
cout << "1 ";
else
cout << "0 ";
}
cout << " ";
}
unsigned int reverseBits(unsigned int num)
{
unsigned int NO_OF_BITS = sizeof(num) * 8;
unsigned int reverse_num = 0;
int i;
for (i = 0; i < NO_OF_BITS; i++)
{
if((num & (1 << i)))
reverse_num |= 1 << ((NO_OF_BITS - 1) - i);
}
return reverse_num;
}
int main()
{
unsigned int num;
cout <<"Enter a number that convert to binary format"<<endl;
cin>>num;
//static input here passing for check
binary(5);
binary(55);
// dynamic value passing through variable num
binary(num);
// unsigned int x = 2;
unsigned int formatnum;
cout<<"Enter a number reverse its format"<<endl;
reverseBits(formatnum);
getchar();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.