Bitset
classBitset
class to a template to allow custom-width bitsetsBitset
class add const
:
const
, you can skip this initially, but it will might make things harder later.
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:
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:
|
Overloads of the bitwise operators:
|
Overloads of the shift operators:
|
Overloads of the comparison operators:
|
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] |
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
data member unsigned
remains as is; only short
is affected by the templatewidth
initialized in-place to the size of the type used for the Bitset
:
... width = 8*sizeof( the-type-of-the-bitset-data ); // 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 ]
short
many things need to change in code from Assignment 2long long
which requires a shift of 32 to get to the middle"mask = 1 << number;"
rewrite as "mask = T(1) << number;"
// 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.