C++ PRPJECT
CPSC 121::First Project— Write your own bitset function
due October 16, 2013
Background: Bits are the way that numbers, letters, structures, and objects are stored in a computer.
Bits are also used as a compact way to set flags, as we have been using to set cout's flags when we want to change the format of numbers (e.g.,
#include <iostream> #include <iomanip // ... cout << right << fixed; // ...
// OR, equivalently, cout.setf( ios::right ); cout.setf( ios::fixed );
In our labs up to now, we have been using the class bitset, e.g., cout << bitset<32>( x ) << endl; to output the bit pattern of integers (i.e., the pattern in 1's and 0's, see below).
Unfortunately, the standard bitset does not allow us to dynamically set the number of bits that we output. In other words,
// 1010 1010 unsigned x = 0xaa; int nbits = 32; cout << bitset<nbits>( x ) << endl; is illegal.
In this project, you will write your own function: string bitset( int nbits,
unsigned x ) that returns a string holding the bits (0s and 1s) of the number x.
Your bitset function will allow the user to do this:
// 1010 0111 unsigned x = 0xa7; int nbits = 32; cout << bitsetfn( nbits, x ) << endl;
You will also write a program that uses your new function to show the bit patterns of numbers from 0 to 130, like so: 0 is: 0000000000000000 1 is: 0000000000000001 2 is: 0000000000000010 3 is: 0000000000000011 4 is: 0000000000000100 5 is: 0000000000000101 6 is: 0000000000000110 7 is: 0000000000000111 8 is: 0000000000001000 9 is: 0000000000001001 10 is: 0000000000001010 11 is: 0000000000001011 12 is: 0000000000001100 13 is: 0000000000001101 14 is: 0000000000001110 15 is: 0000000000001111 16 is: 0000000000010000 17 is: 0000000000010001 18 is: 0000000000010010 19 is: 0000000000010011 20 is: 0000000000010100 21 is: 0000000000010101 22 is: 0000000000010110 23 is: 0000000000010111 24 is: 0000000000011000 25 is: 0000000000011001 26 is: 0000000000011010 27 is: 0000000000011011 28 is: 0000000000011100 29 is: 0000000000011101 30 is: 0000000000011110 31 is: 0000000000011111 32 is: 0000000000100000 33 is: 0000000000100001 34 is: 0000000000100010 35 is: 0000000000100011
36 is: 0000000000100100 37 is: 0000000000100101 38 is: 0000000000100110 39 is: 0000000000100111 40 is: 0000000000101000 41 is: 0000000000101001 42 is: 0000000000101010 43 is: 0000000000101011 44 is: 0000000000101100 45 is: 0000000000101101 46 is: 0000000000101110 47 is: 0000000000101111 48 is: 0000000000110000 49 is: 0000000000110001 50 is: 0000000000110010 51 is: 0000000000110011 52 is: 0000000000110100 53 is: 0000000000110101 54 is: 0000000000110110 55 is: 0000000000110111 56 is: 0000000000111000 57 is: 0000000000111001 58 is: 0000000000111010 59 is: 0000000000111011 60 is: 0000000000111100 61 is: 0000000000111101 62 is: 0000000000111110 63 is: 0000000000111111 64 is: 0000000001000000 65 is: 0000000001000001 66 is: 0000000001000010 67 is: 0000000001000011 68 is: 0000000001000100 69 is: 0000000001000101 70 is: 0000000001000110 71 is: 0000000001000111 72 is: 0000000001001000 73 is: 0000000001001001 74 is: 0000000001001010 75 is: 0000000001001011 76 is: 0000000001001100 77 is: 0000000001001101 78 is: 0000000001001110 79 is: 0000000001001111 80 is: 0000000001010000 81 is: 0000000001010001 82 is: 0000000001010010
CPSC 120::First project: int bitsetfn( int nbits, unsigned x ) due Oct 16th at midnight Dr. Will McCarthy page 1 of 2
83 is: 0000000001010011 84 is: 0000000001010100 85 is: 0000000001010101 86 is: 0000000001010110 87 is: 0000000001010111 88 is: 0000000001011000 89 is: 0000000001011001 90 is: 0000000001011010 91 is: 0000000001011011 92 is: 0000000001011100 93 is: 0000000001011101 94 is: 0000000001011110 95 is: 0000000001011111 96 is: 0000000001100000 97 is: 0000000001100001 98 is: 0000000001100010 99 is: 0000000001100011 100 is: 0000000001100100 101 is: 0000000001100101 102 is: 0000000001100110 103 is: 0000000001100111 104 is: 0000000001101000 105 is: 0000000001101001 106 is: 0000000001101010 107 is: 0000000001101011 108 is: 0000000001101100 109 is: 0000000001101101 110 is: 0000000001101110 111 is: 0000000001101111 112 is: 0000000001110000 113 is: 0000000001110001 114 is: 0000000001110010 115 is: 0000000001110011 116 is: 0000000001110100 117 is: 0000000001110101 118 is: 0000000001110110 119 is: 0000000001110111 120 is: 0000000001111000 121 is: 0000000001111001 122 is: 0000000001111010 123 is: 0000000001111011 124 is: 0000000001111100 125 is: 0000000001111101 126 is: 0000000001111110 127 is: 0000000001111111 128 is: 0000000010000000 129 is: 0000000010000001
130 is: 0000000010000010
Due date: This project is due in two weeks, on Wednesday, October 16th, 2013, at midnight. It involves designing, implementing, and testing a substantial program for the subset of C++ that we are studying in this course, from start to finish. Your submission will be a written project report, including your C++ source code and output, submitted electronically to TITANium as a single PDF file. Note: You may work in pairs on this project. Should you work with a partner, please turn in one report with both names.
Important! The following types of submissions are impermissible, and will receive a score of zero.
✗ Late submissions ✗ Email submissions ✗ Source code that will not
compile without errors ✗ Falsified input/output or that
doesn't match the source code.
✗ Submissions that are plagiarized or that violate the below collaboration guidelines.
Late submissions: No late homework, labs, or project assignments will be accepted under any circumstances (score of 0). Don't be late.
When working in labs or when working on projects, you will work in a collaborative environment, optionally with one partner. Partners may work together freely. Each group should complete their own
work themselves, with very limited help from other individuals or sources. The following guidelines apply to collaboration with any person or resource _other than your partner_.
• You should be able to explain any part of your submission, and be able to explain why you wrote what you did. If working in a group, each member of the group should be able to explain any part of the submission, and not just be able to explain "his or her" subset.
• You may help each other understand the assignment and brainstorm general solutions, but must separate to develop your own detailed solution to the problem, and must individually type in your source code and project report.
• You may, of course, give each other technical support, for instance troubleshooting installing Visual Studio or logging into TITANium.
• You can share documented facts, such as the return value of a particular library function.
Given these requirements, any submissions with identical excerpts, or excerpts that are identical up to superficial rearrangements, will be considered highly suspect of plagiarism.
CPSC 120::First project: int bitsetfn( int nbits, unsigned x ) due Oct 16th at midnight Dr. Will McCarthy page 2 of 2