Converter
with source file Converter.cpp
and re-write all methods from Assignment 1 in C++.
Note the following:
main()
function the reproduces the original tests
Integer
, Character
, etc; use the primitive types
|
|
char getSymbol(long digit) { if (digit >= 0 && digit <= 9) { return (char)('0' + digit); } else { return (char)('A' + (digit - 10)); } } |
long getValue(char symbol) { if (symbol >= '0' && symbol <= '9') { return symbol - '0'; } else { return (symbol - 'A') + 10; } } |
map<?, ?> theNameOfMap; // no new, no =, etc. theNameOfMap[symbol] = value; // CS111: map[symbol] = value; value = theNameOfMap[symbol]; // CS111: value = map[symbol]; |
Bitset
with source file Bitset.cpp
.
Write class Bitset
and test it in a main()
function at the end of the file.
The class will have one data member of type unsigned short
. This is 2 bytes and the bits are indexed from right-to-left (unlike strings or arrays):
Note: If you use variables for intermediate quantities, make them of type15 2 1 0 x x x x .... x x x
unsigned short.
Note: Avoid loops and arithmetic operators where possible.
Here is the list of required methods:
constructor()
Initializes the bitset to 0.
|
constructor(unsigned short theBits)
Initializes the bitset to the given bits.
|
void print()
The method is given for now. It will be modified later:
// prints the value of the bitset as hex number // note: oct would print the octal version // dec would print the decimal version // there is no specifier for binary void print() { cout << hex << the-data-member << endl; cout << dec; // should always be the last line in this method } |
bool none()
[no loops] Returns true if all bits are 0.
|
bool any()
[no loops] Returns true if at least one bit is set to 1.
|
bool all()
[no loops] Returns true if all bits have value 1. No multi-digit numbers.
|
void flip()
[no loops] Inverts the bits in the set (0s to 1s and 1s to 0s). Do not use ~.
|
bool get(int index)
[no loops] Returns the value of the bit at the given index.
Consider bitshift operation of a suitable mask.
|
void set()
[no loops] Sets all bits to 1.
|
void set(int index)
[no loops] Sets the bit at the given index to 1.
|
void clear()
[no loops] Sets all bits to 0.
|
void clear(int index)
[no loops] Sets the bit at the given index to 0.
|
void swap()
[no loops] Swaps the values of the two halves (here the two bytes) of the data member.
|
void swapHi()
[no loops] Swaps the two halves in the high byte of the data member, i.e. the byte that corresponds to the higher powers of 2.
|
void swapLo()
[no loops] Swaps the two halves in the low byte of the data member, i.e. the byte that corresponds to the low powers of 2.
|
bool isPow2()
[no loops] Returns true if the bits stored in the bitset from a power of 2.
|
void clearLast1()
[no loops] Sets to 0 only the last 1 in the bitset, i.e. the 1 bit that corresponds to the lowest power of 2. The rest of the bits are unchanged.
|
int count()
[loop needed] Returns the number of bits that are set to 1.
Use a loop with a mask that is shifted by one position on each cycle.
|
void printBinary()
[loop needed] Prints the binary representation of the data member
// assuming the bitset has value 25, the output would be bits.printBinary(); 0b0000000000011001 |
void print()
Prints the bits in decimal, hexadecimal, octal, and binary in this format [implicit loop for binary]:
// assuming the bitset has value 25, the output would be bits.print(); [25, 0x19, 031, 0b0000000000011001] |
Converter.cpp
and Bitset.cpp
to the Moodle dropbox.