Gettysburg College

CS 221
Computer Organization and Assembly Language Programming

Spring 2023

Assignment 3

Due: Thu, Feb 9, by 11:59pm

Readings

Description

This assignment will focus on C++ and bitwise operations in the context of C++ templates (generics) and operator overloading. The main tasks are: You could consider skipping around and completing at first the sections that seem easier.

const Safety and No Copy

As you write the code, for each method or independent function related to the Bitset class add const:

For each parameter of non-primitive type add & where appropriate to avoid copying of objects.

If you are struggling with const, you can skip this initially, but it will might make things harder later.

Operator Overloading (Part I)

At the end of class Bitset overload the following operators. Some will be overloaded as member methods and some as external functions.

testing the code

No cout, use assert.

Need to include cassert.

After you complete the == overload:

bitset1 = 0x????;  // or 0b??..??
bitset2 = 0x????;  // or 0b??..??
assert( (bitset1 X bitset2) == 0x???? );
Before you complete the == overload:
bitset1 = 0x????;  // or 0b??..??
bitset2 = 0x????;  // or 0b??..??
assert( (bitset1 X bitset2).getValue() == 0x???? );
??? getValue()

Returns the bitset data member.
==  !=

Overloads of the comparison operators:
  • == as method
  • != as external function; one line only, no method calls, use ==
&  |  ^  ~

Overloads of the bitwise operators:
  • & as method
  • | ^  as external functions
  • ~ both as method and as external function, but comment out the external function
<<  >>

Overloads of the shift operators:
  • << as method
  • >> as external function
<  >=

Overloads of the comparison operators:
  • < as method
  • >= as external function; one line only, no method calls, use ...

Operator Overloading (Part II)

At the end of class Bitset overload the following operators. Some will be overload as member methods and some as independent functions.

testing the code

No cout, use assert. Mostly will have this format:
bitset1 = 0x????;  // or 0b??..??
apply operation on bitset1
assert( bitset1 == 0x???? );
assert( bitset2 == 0x???? );  // if bitset2 involved in test
^=

Overload of the bitiwse operators as method.

It should now be possible to write:

bitset1 = 0x????;  // or 0b??..??
bitset2 = 0x????;  // or 0b??..??
bitset3 = bitset1 ^= bitset2;   // evaluates right to left
assert( ... );
assert( ... );
>>=

Overload of the shift operator as method.

It should now be possible to write:

bitset1 = 0x????;  // or 0b??..??
bitset1 >>= 5;
bitset2 = bitset1 >>= 5;  // evaluates right to left
assert( ... );
assert( ... );
+=

Adds unsigned short value to the bitset.

It should now be possible to write:

bitset = dec-value;
bitset += 5;
assert( ... );
assert( ... );
++

The prefix increment operator as method.

It should now be possible to write:

bitset = dec-value;
++bitset;
assert( ... );
++(++(++bitset));
assert( ... );
<<

Overload of the left shift operator for display as external function (cannot be done as method; why?).

Note that the type of cout is ostream and in the implementation should be paired with the &.

The code is easy -- copy the code from method print with minor modifications. The challenge here is to figure out the return type and number/type of parameters.

It should now be possible to write:

bitset = hex/bin-value;
cout << bitset << endl;    // displays   [25, 0x19, 031, 0b0000000000011001]

Custom Width Bitset, Templates

Make the Bitset class generic using a template. This would allow creating custom width bitsets:

Bitset<unsigned char> bitset1 = hex/bin-value;    // 1 byte bitset
Bitset<unsigned short> bitset2 = hex/bin-value;   // 2 byte bitset
Bitset<unsigned int> bitset3 = hex/bin-value;     // 4 byte bitset
Bitset<unsigned long long> bitset4 = hex/bin-value;    // 8 byte bitset

Here are things to keep in mind:

For testing, create one bitset of width 1 byte and one bitset of width 8 bytes and then check only the methods from Assignment 2 in this format:

// for non-void methods
bitset = hex/bin-value;
assert( bitset.nonVoidMethod() == hex/bin-value );

// for void methods
bitset = hex/bin-value;
bitset.voidMethod();
assert( bitset == hex/bin-value );


What to turn in

Upload Bitset.cpp to the Moodle dropbox.