Gettysburg College

CS 221
Computer Organization and Assembly Language Programming

Fall 2024

Assignment 3

Due: Thu, Sep 19, 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

This section could be skipped initially. See the class example.

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.

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 the built-in C++ assert (not the one from Assignment 2). 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 (i.e. do they have same/diff value):
  • ==     as method
  • !=     as external function; one line only, no method calls, use ...
&  |  ^  ~

Overloads of the bitwise operators (i.e. apply bitwise operation to Bitsets):
  • &       as method
  • | ^  as external functions
  • ~       both as method and as external function, but comment out the external function
  • |       once you complete the above, implement | without using | (hint:use De Morgan's Law)
<<  >>

Overloads of the shift operators (i.e. shifting left/right by given number of positions):
  • <<     as method
  • >>     as external function
<  >=

Overloads of the comparison operators based on the value (is one bigger/lesseq than other):
  • <       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 operator 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( ... );
++

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?). To simplify the code it does not show the binary version. It should now be possible to write:

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

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

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

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.