1. computeUnsignedBinary function: This function takes a static array, bit pattern length, and a non-negative integer number as its arguments The function must populate the array with the unsigned binary representation of the non-negative integer number argument in the given bit pattern length. Assume the all the arguments are 2. computeSignAndMagnitudeBinary function :This function takes a static array, bit pattern length, and an integer number as its arguments. The function must populate the array with the sign and magnitude binary representation of the integer number argument in the given bit pattern length Assume the all the arguments are valid The sign bit of a zero integer number must be set to 0 (not 1), see the sample run outputs below.
#include <iostream>
using namespace std;
void computeUnsignedBinary(unsigned bits[], unsigned length, unsigned x) {
for (unsigned i=0; i<length; i++) {
bits[length-1-i] = (x % 2 == 1) ? 1 : 0;
x /= 2;
}
}
void computeSignedAndMagnitudeBinary(unsigned bits[], unsigned length, int x) {
if (x < 0) {
x = -x;
bits[0] = 1;
}
else {
bits[0] = 0;
}
computeUnsignedBinary(&bits[1], length-1, x);
}
void printBinary(unsigned bits[], unsigned length) {
for (unsigned i=0; i<length; i++) {
cout << bits[i];
}
cout << endl;
}
int main() {
const unsigned N=16;
unsigned bits[N];
int x;
cout << "Enter an integer: ";
cin >> x;
computeSignedAndMagnitudeBinary(bits, N, x);
printBinary(bits, N);
return 0;
}
Comments
Leave a comment