Question: Need some help finishing these functions: #include BinaryInt.h #include // Internal representation stores bits so that coefficient of 2^i is at index i. /** *

Need some help finishing these functions:

#include "BinaryInt.h" #include  // Internal representation stores bits so that coefficient of 2^i is at index i. /** * Initializes BinInt[] so that the elements correspond to the binary * representation of the low 32 bits of integer Src. * * Pre: BinInt[] is of dimension 32 * Post: For i = 0 to 31, BinInt[i] == 2^i bit of Src */ void BI_Create(uint8_t BinInt[], int64_t Src) { int32_t Mask = 0x01; for (int pos = 0; pos < NUM_BITS; pos++) { BinInt[pos] = ( Src & Mask) ? 1 : 0; Mask = Mask << 1; } } /** * Computes sum of the signed/unsigned integer values. * * Pre: Sum[], Left[] and Right[] are of dimension 32 * Left[] and Right[] store 2's complement or unsigned representations * DR matches the representation of both Left[] and Right[] * Post: Sum[] == Left[] + Right[], if possible * Ret: false if overflow occurs when computing sum of Left[] and Right[]; * true otherwise */ bool BI_Add(uint8_t Sum[], const uint8_t Left[], const uint8_t Right[], enum DataRep DR) { return false; } /** * Computes difference of the signed/unsigned integer values. * * Pre: Diff[], Left[] and Right[] are of dimension 32 * Left[] and Right[] store 2's complement or unsigned representations * DR matches the representation of both Left[] and Right[] * Post: Diff[] == Left[] - Right[], if possible * Ret: false if overflow occurs when computing difference of Left[] * and Right[]; true otherwise */ bool BI_Sub(uint8_t Diff[], const uint8_t Left[], const uint8_t Right[], enum DataRep DR) { return false; } /** * Computes negation of 2's complement representation of integer value. * * Pre: Neg[] and Right[] are of dimension 32 * Right[] stores a 2's complement representation * Post: Neg[] = -Right[] * Ret: false if negation cannot be correctly represented; * true otherwise */ bool BI_Neg(uint8_t Neg[], const uint8_t Right[]) { return false; } /** * Converts a BinInt[] to its decimal representation. * * Pre: Num[] is of dimension 32 * Num[] stores a 2's complement or unsigned representation * DR matches the representation of Num[] * Ret: The decimal value. */ int64_t BI_ToDecimal(uint8_t Num[], enum DataRep DR) { return 0; } /** * Prints the binary representation, with formatting. * * Pre: fp is open on an output stream * BinInt[] is of dimension 32 and stores a 2's complement * or unsigned representation * prefix and suffix are each NULL or point to a C-string * Post: the bits represented in BinInt[] have been written, preceded by * prefix (if not NULL) and followed by suffix (if not NULL) */ void BI_fprintf(FILE* fp, const uint8_t BinInt[], char* prefix, char* suffix) { if ( prefix != NULL) fprintf(fp, prefix); for (int pos = NUM_BITS - 1; pos >= 0; pos--) { if ( pos < NUM_BITS - 1 && pos % 4 == 3 ) fprintf(fp, " "); fprintf(fp, "%"PRIu8, BinInt[pos]); } if ( suffix != NULL) fprintf(fp, suffix); } 

THIS IS THE HEADER FILE FOR REFERENCE:

#ifndef BIN_INT_H #define BIN_INT_H #include  #include  #include  #define NUM_BITS 32 enum DataRep {SIGNED, UNSIGNED}; /** * Initializes BinInt[] so that the elements correspond to the binary * representation of the low 32 bits of integer Src. * * Pre: BinInt[] is of dimension 32 * Post: For i = 0 to 31, BinInt[i] == 2^i bit of Src */ void BI_Create(uint8_t BinInt[], int64_t Src); /** * Computes sum of the signed/unsigned integer values. * * Pre: Sum[], Left[] and Right[] are of dimension 32 * Left[] and Right[] store 2's complement or unsigned representations * DR matches the representation of both Left[] and Right[] * Post: Sum[] == Left[] + Right[], if possible * Ret: false if overflow occurs when computing sum of Left[] and Right[]; * true otherwise */ bool BI_Add(uint8_t Sum[], const uint8_t Left[], const uint8_t Right[], enum DataRep DR); /** * Computes difference of the signed/unsigned integer values. * * Pre: Diff[], Left[] and Right[] are of dimension 32 * Left[] and Right[] store 2's complement or unsigned representations * DR matches the representation of both Left[] and Right[] * Post: Diff[] == Left[] - Right[], if possible * Ret: false if overflow occurs when computing difference of Left[] * and Right[]; true otherwise */ bool BI_Sub(uint8_t Diff[], const uint8_t Left[], const uint8_t Right[], enum DataRep DR); /** * Computes negation of 2's complement representation of integer value. * * Pre: Neg[] and Right[] are of dimension 32 * Right[] stores a 2's complement representation * Post: Neg[] = -Right[] * Ret: false if negation cannot be correctly represented; * true otherwise */ bool BI_Neg(uint8_t Sum[], const uint8_t Right[]); /** * Converts a BinInt[] to its decimal representation. * * Pre: Num[] is of dimension 32 * Num[] stores a 2's complement or unsigned representation * DR matches the representation of Num[] * Ret: The decimal value. */ int64_t BI_ToDecimal(uint8_t Num[], enum DataRep DR); /** * Prints the binary representation, with formatting. * * Pre: fp is open on an output stream * BinInt[] is of dimension 32 and stores a 2's complement * or unsigned representation * prefix and suffix are each NULL or point to a C-string * Post: the bits represented in BinInt[] have been written, preceded by * prefix (if not NULL) and followed by suffix (if not NULL) */ void BI_fprintf(FILE* fp, const uint8_t BinInt[], char* prefix, char* suffix); #endif 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!