This daily will allow you to practice more with the bit wise operators and shift
ID: 3668723 • Letter: T
Question
This daily will allow you to practice more with the bit wise operators and shifts. Consider the following modification of the main program from daily 3: #include 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(unsigned int flag_holder); int main(int argc, char* argv[]) { unsigned int flag_holder = 0; set_flag(&flag_holder, 3); set_flag(&flag_holder, 16); set_flag(&flag_holder, 31); display_32_flags(flag_holder); unset_flag(&flag_holder, 31); unset_flag(&flag_holder, 3); set_flag(&flag_holder, 9); display_32_flags(flag_holder); return 0; } Write the code for the definition of unset_flag and display_32_flags so that the output of your program looks like the following: 1000 0000 0000 0001 0000 0000 0000 1000.
You can think of the unset_flag function as taking an integer and making sure that the nth bit is a 0. You may find the ~ operator useful. It is used to “flip the bits” of a number making all the zero values 1’s and all the 1’s zeroes. As in the previous daily, the shifting operators and the bitwise and ( & ) and or ( | ) may also be useful. If you are doing multiplication or division then you are doing it wrong. The display_32_flags function should just print the information to the screen as was given in the previous assignment (just turn it into a function instead).
Explanation / Answer
Referencing variables
Substitute the value of a variable into a command by dereferencing it with the dollar sign ($), as in echo $flag. In some cases, however, you must use the name of a value, such as unset flag, instead of the dollar sign.
Removing variables
Use the unset command to remove variables.
The following commands show how variables are set and referenced:
fm_shell> set search_path ".
/usr/synopsys/libraries" .
/usr/synopsys/libraries fm_shell> adir = "/usr/local/lib"
/usr/local/lib
fm_shell> set my_path "$adir $search_path"
/usr/local/lib . /
usr/synopsys/libraries
fm_shell> unset adir
fm_shell> unset my_path
Note: You can also set and unset environment variables in the GUI by entering them into the command bar or selecting File > Environment from the console window.
Unset flag. This command switches flag off. The original state of flag is restored when the current module ends. Variants: 1. Local Unset flag. This command switches flag off. The original state of flag is restored when the current section ends. 2. Global Unset flag. This command switches flag off. The original state of flag is not restored at the end of the module. Additionally, if set in a file, flag is switched on when the file is Require-d.