Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

void set_flag(unsigned int flag_holder[], int flag_position); void unset_flag(un

ID: 3671179 • Letter: V

Question

void set_flag(unsigned int flag_holder[], int flag_position); void unset_flag(unsigned int flag_holder[], int flag_position); int check_flag(unsigned int flag_holder[], int flag_position); void display_32_flags_as_array(unsigned int flag_holder); void display_flags(unsigned int flag_holder[], int size); //This is the main inside the client int main(int argc, char* argv[]) { unsigned int flag_holder[5] = { 0 };//Set the first integer to zero and all others to zero by default. set_flag(flag_holder, 3); set_flag(flag_holder, 16); set_flag(flag_holder, 31); set_flag(flag_holder, 87); display_flags(flag_holder, 5); printf(" "); unset_flag(flag_holder, 31); unset_flag(flag_holder, 3); set_flag(flag_holder, 99); set_flag(flag_holder, 100); display_flags(flag_holder, 5); return 0; } Here I have changed the functions so that they take an array of integers instead of just one integer. This allows me to imagine that I have a long array of bits instead of an array of integers. The functions can now set, unset, check and display flags for any bit in the array of 5 integers that I have made (and should work for any size array as long as your bit index is in bounds of your array). I also changed the display behavior. Daily 4 displayed the flags as you would see them in a binary number but since this program is moving away from the idea of a binary number to store bits and moving toward the idea of having an array of bits the display_32_flags_as_array function will display the [0] bit first then [1] and so on up to 31 whereas the display_32_flags function in daily 4 displays the [31] bit first and down to [0]. Similarly the display_flags function now takes an array of integers and displays one integer per line using the display_32_flags_as_array function. . Your output should look exactly like the following 0001 0000 0000 0000 1000 0000 0000 0001 0000 0000 0000 0000 0000

Explanation / Answer

A few compilers has a special feature implemented within recognizing literal binary numbers by prefix "0b..." preceding the number, although most compilers (C/C++ standards) don't have such feature and if it is the case, the above code is an alternative solution.

- Total preprocessor driven, not spending processor time in pointless operations (like "?.. :..", "<<", "+") to the executable program (it may be performed hundred of times in the final application);
- It works "mainly in C" compilers and C++ as well (template+enum solution works only in C++ compilers);
- It has only the limitation of "longness" for expressing "literal constant" values. There would have been earlyish longness limitation (usually 8bits:0-255) if one had expressed constant values by parsing resolve of "enum solution"(usually 255 = reach enum definition limit), differently, "literal constant" limitations, in the compiler allows greater numbers;
- Some other solutions demand exagerated number of constant definitions (#define's in my opinion) including long or several header files (in most cases not easily readable and understandable, and make the project become unnecessarily confused and extended, like that using "BOOST_BINARY()");
- Simplicity of the solution: easily readable, understandable and adjustable for other cases (could be extended for grouping 8 by 8 too);