$24
In this lab assignment, you will write a program that represents binary numbers as an array of an enumerated type. You also will need convert to and from the corresponding bitsets.
Create the source file verbose_binary.cpp and the header file verbose_binary.h
Create a scoped enumeration VerboseBinary in the header file with the constants corresponding to the power of two: One,Two,Four,Eight,Sixteen as well as Null. (You will need to decide on the appropriate type to use in the enumeration).
In *.h
enum class!?! VerboseBinary : short;!?!
In *.cpp:
enum class!?! VerboseBinary : short!?! {
One,Two,Four,Eight,SixteenNull=0,One=1,Two=2,Four=4,Eight=8,Sixteen=16aswellasNull!?! };
Book, p.1018, 19.3. Enumerations
Enumerations let us group together sets of integral constants.
Like classes, each enumeration defines a new type.
Enumerations are literal types (§ 7.5.6, p. 299).
C++ has two kinds of enumerations: scoped and unscoped. The new standard introduced scoped enumerations. We define a scoped enumeration using the
keywords enum class (or, equivalently, enum struct), followed by the enumeration name and a comma-separated list of enumerators enclosed in curly braces. A semicolon follows the close curly:
enum class open_modes {input, output, append};
We define an unscoped enumeration by omitting the class (or struct) keyword. The enumeration name is optional in an unscoped enum:
enum color {red, yellow, green}; // unscoped enumeration
unnamed, unscoped enum
enum {floatPrec = 6, doublePrec = 10, double_doublePrec = 10};
By default, enumerator values start at 0 and each enumerator has a value 1 greater than the preceding one. However, we can also supply initializers for one or more enumerators:
Click here to view code image enum class intTypes {
charTyp = 8, shortTyp = 16, intTyp = 16, longTyp = 32, long_longTyp = 64 };
As we see with the enumerators for intTyp and shortTyp, an enumerator value need not be unique. When we omit an initializer, the enumerator has a value 1 greater than the preceding enumerator.
#ifndef VERBOSE_BINARY_H_
#define VERBOSE_BINARY_H_
#include <bitset
using std::bitset;
// functions declarations here
#endif
p.944, Defined Terms
bitset Standard library class that holds a collection of bits of a size that is known at compile time, and provides operations to test and set the bits in the collection.
p.211, 4.8. The Bitwise Operators
The bitwise operators take operands of integral type that they use as a collection of bits. These operators let us test and set individual bits. As we’ll see in § 17.2 (p. 723), we can also use these operators on a library type named bitset that represents a flexibly sized collection of bits.
As usual, if an operand is a “small integer,” its value is first promoted (§ 4.11.1, p.
160) to a larger integral type. The operand(s) can be either signed or unsigned.
p.213, Bitwise AND, OR, and XOR Operators
The AND (&), OR (|), and XOR (^) operators generate new values with the bit pattern composed from its two operands:
p.890, Table 17.2. Ways to Initialize a bitset
p.893, Table 17.3. bitset Operations
Create a function Bitset<5 convert( !?! ) which accepts an array of size 6 of the type of your enumeration and returns the corresponding Bitset<5 value. Unused values in the array should be indicated by a terminating Null (similar idea to old-style c-strings, please see example below). You will have to replace !?! with the correct type. Implement this function in verbose_binary.cpp and declare it in the header verbose_binary.h.
bitset<5 convert(VerboseBinary!?! vb[6]) {
short!?! accu = 0;
for (int i=0; i<6; ++i ) {
if (vb[i]==VerboseBinary::Null)!?! break;!?!
accu += static_cast<int(vb[i]); } return accu;
}
Create a function void convert( !?!, !?! ) which accepts an array of size 6 of the type of your enumeration and the corresponding Bitset<5 value. Unused values in the array should be indicated by a terminating Null (similar idea to old-style c-strings, please see example below). You will have to replace !?! with the correct type. Implement this function in verbose_binary.cpp and declare it in the header verbose_binary.h.
2^0=1; 2^1=2; 2^2=4; 2^3=8; 2^4=16; total= 5 bits; add 0; //In *.h file:
void convert(bitset<5 bs, VerboseBinary vb[6]); //In *.cpp file:
void convert(bitset<5 bs, VerboseBinary vb[6] ) { int pos = 0;
if (bs.test(4)) vb[pos++]=VerboseBinary::Sixteen; if (bs.test(3)) vb[pos++]=VerboseBinary::Eight; if (bs.test(2)) vb[pos++]=VerboseBinary::Four;!?!
if (bs.test(1)) vb[pos++]=VerboseBinary::Two; if (bs.test(0)) vb[pos++]=VerboseBinary::One; vb[pos]=VerboseBinary::Null;
}
Unused values in the array should be indicated by a terminating Null (similar idea to old-style c-strings, please see example below).
Example Run:
Enter two numbers between 0-31:
31 17
Sixteen, Eight, Four, Two, One, Null Sixteen, One, Null Eight, Four, Two, Null
main.cpp #include <iostream #include <bitset #include "verbose_binary.h" int main() {
int a,b; bitset<5 aBs,bBs;
std::cout << "Enter two numbers between 0-31:" << std::endl;
std::cin a b;
if (a<0 || a31) return -1; if (b<0 || b31) return -2; aBs = static_cast<bitset<5(a); bBs = static_cast<bitset<5(b);
std::cout << aBs << " " << bBs << std::endl;
main.cpp VerboseBinary aVB[6]; VerboseBinary bVB[6]; convert(aBs,aVB); convert(bBs,bVB); print(aVB); print(bVB); xorEquals(aVB,bVB);
print(aVB);
return 0;
}
/** Do not alter
Your solution must work with this main program **/
Create a function void print( !?! ) that accepts a VerboseBinary array of size 6 and prints it to console as a binary number. Again, implement this function in verbose_binary.cpp and declare it in the header verbose_binary.h.
// in *.h file
void print(VerboseBinary!?! vb[6]);
// in *.cpp file
void print( VerboseBinary!?! vb[6] ) {
for (int i=0; !?!i<6; ++i ) {
if (vb[i]==VerboseBinary::Sixteen) cout <<"Sixteen"; if (vb[i]==VerboseBinary::Eight) cout << "Eight"; if (vb[i]==VerboseBinary::Four)!?! cout << "Four";
if (vb[i]==VerboseBinary::Two) cout << "Two"; if (vb[i]==VerboseBinary::One) cout << "One"; if (vb[i]==VerboseBinary::Null) {
cout << "Null" <<!?! endl; break; } cout << ", ";
}
Create a function void xorEquals( !?!, !?! ) which takes two arrays of size 6 of type VerboseBinary. The function should performs a binary xor operation on the two parameters and put the result in the first parameter.
// In *.h file
void xorEquals(VerboseBinary!?! aVB[6],
VerboseBinary!?! bVB[6]);
// in *.cpp file
void xorEquals(VerboseBinary!?! aVB[6], VerboseBinary!?! bVB[6]) {
bitset<5 res = convert(aVB) ^ convert(bVB); convert(res,aVB);
}
Test your implementation with the supplied main function in the separate file main.cpp The main function will ask for console input of two integer numbers between 0-31, will print the numbers VerboseBinary to console, then will call the above function xorEquals and print the result to console.
Example Run:
Enter two numbers between 0-31:
31 17
Sixteen, Eight, Four, Two, One, Null Sixteen, One, Null
Eight, Four, Two, Null
You must hand-in exactly the three files: verbose_binary.cpp and verbose_binary.h in a zip archive. Do not hand-in any other files or use any other type of archive. No word files or pdfs (or similar word processing files) will be accepted.