Bitset
classBitset
class to a template to allow custom-width bitsetsBitset
class add const
:
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:
Before you complete thebitset1 = 0x????; // or 0b??..?? bitset2 = 0x????; // or 0b??..?? assert( (bitset1 X bitset2) == 0x???? ); == overload:
bitset1 = 0x????; // or 0b??..?? bitset2 = 0x????; // or 0b??..?? assert( (bitset1 X bitset2).getValue() == 0x???? ); |
Returns the bitset data member.
|
Overloads of the comparison operators (i.e. do they have same/diff value):
|
Overloads of the bitwise operators (i.e. apply bitwise operation to Bitsets):
|
Overloads of the shift operators (i.e. shifting left/right by given number of positions):
|
Overloads of the comparison operators based on the value (is one bigger/lesseq
than other):
|
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:
Note that the type ofbitset = hex/bin-value; cout << bitset; // displays [25, 0x19, 031] cout << "value: " << bitset << endl; // displays value: [25, 0x19, 031] 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.
|
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:
Bitset
:
... NAME = 8*sizeof( the-type-of-the-bitset-data ); // computes number of bits [ note: sizeof(char) gives 1, sizeof(short) gives 2, ..., sizeof(long long) gives 8 ] [ note: sizeof(Cat) gives sum of byte sizes of Cat data members ]
Bitset
: one of the constants should have all its bits set to 0 (i.e. has value 0); the other should have only the last bit set to 1 (i.e. has value 1); the third should have all of its bits set to 1short
many things need to change in code from Assignment 2// 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 );
Bitset.cpp
to the Moodle dropbox.