Complete the implementation of the update_bits_2 program. This program will help you learn how to query and update indiv
Posted: Fri Apr 29, 2022 6:33 am
Code template:
#include <stdio.h>
#include <stdlib.h>
// INSERT GLOBALS HERE.
void update_bits () {
// INSERT ASSIGNMENT OPERATIONS HERE.
}
void print_bits( const char * label, unsigned char val){
printf("%s bits = {", label);
int deja = 0;
for (int i = 0; i <= 7; i++) {
if ( (val & (1 << i)) )
{
printf("%s%d", (deja ? ",
" : ""), i);
deja = 1;
}
}
printf("}\n");
}
int main(int argc, char * argv[]) {
unsigned char init_val = 0;
unsigned char out_init = 0;
if (argc > 1) init_val = atoi(argv[1]) &
255;
if (argc > 2) out_init = atoi(argv[2]) &
255;
in_var_01 = init_val;
output_var_01 = out_init;
in_var_02 = init_val;
output_var_02 = out_init;
in_var_03 = init_val;
output_var_03 = out_init;
in_var_04 = init_val;
output_var_04 = out_init;
in_var_05 = init_val;
output_var_05 = out_init;
in_var_06 = init_val;
output_var_06 = out_init;
in_var_07 = init_val;
output_var_07 = out_init;
in_var_08 = init_val;
output_var_08 = out_init;
in_var_09 = init_val;
output_var_09 = out_init;
in_var_10 = init_val;
output_var_10 = out_init;
update_bits();
print_bits("Input Value
", init_val);
print_bits("Initial output Value", out_init);
printf("\nSearch for bit 0.\n");
printf("Set or clear bit 5:\n");
print_bits("output_var_01", output_var_01);
printf("\nSearch for bit 6.\n");
printf("Set or clear bit 5:\n");
print_bits("output_var_02", output_var_02);
printf("\nSearch for bits 0, 6.\n");
printf("Set or clear bit 6:\n");
print_bits("output_var_03", output_var_03);
printf("\nSearch for bits 3, 5.\n");
printf("Set or clear bit 4:\n");
print_bits("output_var_04", output_var_04);
printf("\nSearch for bits 0, 3, 5.\n");
printf("Set or clear bit 3:\n");
print_bits("output_var_05", output_var_05);
printf("\nSearch for bits 4, 6, 7.\n");
printf("Set or clear bit 3:\n");
print_bits("output_var_06", output_var_06);
printf("\nSearch for bits 0, 2, 3, 4.\n");
printf("Set or clear bit 7:\n");
print_bits("output_var_07", output_var_07);
printf("\nSearch for bits 3, 4, 5, 7.\n");
printf("Set or clear bit 3:\n");
print_bits("output_var_08", output_var_08);
printf("\nSearch for bits 0, 1, 3, 4,
7.\n");
printf("Set or clear bit 1:\n");
print_bits("output_var_09", output_var_09);
printf("\nSearch for bits 1, 4, 5, 6,
7.\n");
printf("Set or clear bit 7:\n");
print_bits("output_var_10", output_var_10);
return 0;
}
Complete the implementation of the update_bits_2 program. This program will help you learn how to query and update individual bits (or groups of bits) within variables. The program must define several global variables, as follows: • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_01 and output_var_01. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_02 and output_var_02. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_03 and output_var_03. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_04 and output_var_04. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_05 and output_var_05. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_06 and output_var_06. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_07 and output_var_07. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_08 and output_var_08. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_09 and output_var_09. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_10 and output_var_10. Do not assign values to these variables at the point of declaration.
After declaring the global variables, define function update_bits, as follows: • The function requires no arguments and returns nothing. • Within the function: o Modify the value of bit 5 of global variable output_var_01, such that it is equal to 1 if and only if bit 0 of in_var_01 is set. Leave all other bits unaltered. Do not alter in_var_01. Modify the value of bit 5 of global variable output_var_02, such that it is equal to 1 if and only if bit 6 of in_var_02 is set. Leave all other bits unaltered. Do not alter in_var_02. • Modify the value of bit 6 of global variable output_var_03, such that it is equal to 1 if and only if bits 0,6 of in_var_03 are all set. Leave all other bits unaltered. Do not alter in_var_03. o Modify the value of bit 4 of global variable output_var_04, such that it is equal to 1 if and only if bits 3, 5 of in_var_04 are all set. Leave all other bits unaltered. Do not alter in_var_04. o Modify the value of bit 3 of global variable output_var_05, such that it is equal to 1 if and only if bits 0, 3, 5 of in_var_05 are all set. Leave all other bits unaltered. Do not alter in_var_05. • Modify the value of bit 3 of global variable output_var_06, such that it is equal to 1 if and only if bits 4, 6, 7 of in_var_06 are all set. Leave all other bits unaltered. Do not alter in_var_06. • Modify the value of bit 7 of global variable output_var_07, such that it is equal to 1 if and only if bits 0, 2, 3, 4 of in_var_07 are all set. Leave all other bits unaltered. Do not alter in_var_07. o Modify the value of bit 3 of global variable output_var_08, such that it is equal to 1 if and only if bits 3, 4, 5, 7 of in_var_08 are all set. Leave all other bits unaltered. Do not alter in_var_08. o Modify the value of bit 1 of global variable output_var_09, such that it is equal to 1 if and only if bits 0, 1, 3, 4, 7 of in_var_09 are all set. Leave all other bits unaltered. Do not alter in_var_09. Modify the value of bit 7 of global variable output_var_10, such that it is equal to 1 if and only if bits 1, 4, 5, 6, 7 of in_var_10 are all set. Leave all other bits unaltered. Do not alter in_var_10.
Notes: • The results produced by this program look similar to the following image. The exact values requested in your version of the question will probably be different from those listed below. Use the values requested in your version of the question.
х /w Lawrence@Lawrence-PC / $./test01 255 Input Value bits = {0, 1, 2, 3, 4, 5, 6, 7} Initial output Value bits = {} Search for bit o. Set or clear bit 1: out_var_01 bits = {1} Search for bit 2. set or clear bit 0: out_var_02 bits = {0} Search for bits 0, 1. set or clear bit 0: out_var_03 bits = {0} Search for bits 5, 6. set or clear bit 3: out_var_04 bits = {3} Search for bits 0, 5, 6. Set or clear bit o: out_var_05 bits = {0} Search for bits 2, 5, 6. set or clear bit 0: out_var_06 bits = {0} Search for bits o, 3, 6, 7. set or clear bit 2: out_var_07 bits = {2} Search for bits 2, 4, 5, 7. Set or clear bit 3: out_var_08 bits = {3} Search for bits 0, 2, 3, 4, 7. set or clear bit 0: out_var_09 bits = {0} Search for bits 1, 3, 4, 6, 7. set or clear bit 2: out_var_10 bits = {2} Lawrence@Lawrence-PC /W $
х /w Lawrence@Lawrence-PC /W $./test01 63 255 Input Value bits = {0, 1, 2, 3, 4, 5} Initial output Value bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bit o. Set or clear bit 1: out_var_01 bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bit 2. set or clear bit 0: out_var_02 bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bits 0, 1. set or clear bit 0: out_var_03 bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bits 5, 6. set or clear bit 3: out_var_04 bits = {0, 1, 2, 4, 5, 6, 7} Search for bits 0, 5, 6. Set or clear bit o: out_var_05 bits = {1, 2, 3, 4, 5, 6, 7} Search for bits 2, 5, 6. set or clear bit o: out_var_06 bits = {1, 2, 3, 4, 5, 6, 7} Search for bits 0, 3, 6, 7. set or clear bit 2: out_var_07 bits = {0, 1, 3, 4, 5, 6, 7} Search for bits 2, 4, 5, 7. Set or clear bit 3: out_var_08 bits = {0, 1, 2, 4, 5, 6, 7} Search for bits 0, 2, 3, 4, 7. set or clear bit 0: out_var_09 bits = {1, 2, 3, 4, 5, 6, 7} Search for bits 1, 3, 4, 6, 7. set or clear bit 2: out_var_10 bits = {0, 1, 3, 4, 5, 6, 7} Lawrence@Lawrence-PC /W $
#include <stdio.h>
#include <stdlib.h>
// INSERT GLOBALS HERE.
void update_bits () {
// INSERT ASSIGNMENT OPERATIONS HERE.
}
void print_bits( const char * label, unsigned char val){
printf("%s bits = {", label);
int deja = 0;
for (int i = 0; i <= 7; i++) {
if ( (val & (1 << i)) )
{
printf("%s%d", (deja ? ",
" : ""), i);
deja = 1;
}
}
printf("}\n");
}
int main(int argc, char * argv[]) {
unsigned char init_val = 0;
unsigned char out_init = 0;
if (argc > 1) init_val = atoi(argv[1]) &
255;
if (argc > 2) out_init = atoi(argv[2]) &
255;
in_var_01 = init_val;
output_var_01 = out_init;
in_var_02 = init_val;
output_var_02 = out_init;
in_var_03 = init_val;
output_var_03 = out_init;
in_var_04 = init_val;
output_var_04 = out_init;
in_var_05 = init_val;
output_var_05 = out_init;
in_var_06 = init_val;
output_var_06 = out_init;
in_var_07 = init_val;
output_var_07 = out_init;
in_var_08 = init_val;
output_var_08 = out_init;
in_var_09 = init_val;
output_var_09 = out_init;
in_var_10 = init_val;
output_var_10 = out_init;
update_bits();
print_bits("Input Value
", init_val);
print_bits("Initial output Value", out_init);
printf("\nSearch for bit 0.\n");
printf("Set or clear bit 5:\n");
print_bits("output_var_01", output_var_01);
printf("\nSearch for bit 6.\n");
printf("Set or clear bit 5:\n");
print_bits("output_var_02", output_var_02);
printf("\nSearch for bits 0, 6.\n");
printf("Set or clear bit 6:\n");
print_bits("output_var_03", output_var_03);
printf("\nSearch for bits 3, 5.\n");
printf("Set or clear bit 4:\n");
print_bits("output_var_04", output_var_04);
printf("\nSearch for bits 0, 3, 5.\n");
printf("Set or clear bit 3:\n");
print_bits("output_var_05", output_var_05);
printf("\nSearch for bits 4, 6, 7.\n");
printf("Set or clear bit 3:\n");
print_bits("output_var_06", output_var_06);
printf("\nSearch for bits 0, 2, 3, 4.\n");
printf("Set or clear bit 7:\n");
print_bits("output_var_07", output_var_07);
printf("\nSearch for bits 3, 4, 5, 7.\n");
printf("Set or clear bit 3:\n");
print_bits("output_var_08", output_var_08);
printf("\nSearch for bits 0, 1, 3, 4,
7.\n");
printf("Set or clear bit 1:\n");
print_bits("output_var_09", output_var_09);
printf("\nSearch for bits 1, 4, 5, 6,
7.\n");
printf("Set or clear bit 7:\n");
print_bits("output_var_10", output_var_10);
return 0;
}
Complete the implementation of the update_bits_2 program. This program will help you learn how to query and update individual bits (or groups of bits) within variables. The program must define several global variables, as follows: • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_01 and output_var_01. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_02 and output_var_02. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_03 and output_var_03. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_04 and output_var_04. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_05 and output_var_05. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_06 and output_var_06. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_07 and output_var_07. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_08 and output_var_08. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_09 and output_var_09. Do not assign values to these variables at the point of declaration. • Declare two global variables, each of them suitable to store a single byte. The names of the variables should be in_var_10 and output_var_10. Do not assign values to these variables at the point of declaration.
After declaring the global variables, define function update_bits, as follows: • The function requires no arguments and returns nothing. • Within the function: o Modify the value of bit 5 of global variable output_var_01, such that it is equal to 1 if and only if bit 0 of in_var_01 is set. Leave all other bits unaltered. Do not alter in_var_01. Modify the value of bit 5 of global variable output_var_02, such that it is equal to 1 if and only if bit 6 of in_var_02 is set. Leave all other bits unaltered. Do not alter in_var_02. • Modify the value of bit 6 of global variable output_var_03, such that it is equal to 1 if and only if bits 0,6 of in_var_03 are all set. Leave all other bits unaltered. Do not alter in_var_03. o Modify the value of bit 4 of global variable output_var_04, such that it is equal to 1 if and only if bits 3, 5 of in_var_04 are all set. Leave all other bits unaltered. Do not alter in_var_04. o Modify the value of bit 3 of global variable output_var_05, such that it is equal to 1 if and only if bits 0, 3, 5 of in_var_05 are all set. Leave all other bits unaltered. Do not alter in_var_05. • Modify the value of bit 3 of global variable output_var_06, such that it is equal to 1 if and only if bits 4, 6, 7 of in_var_06 are all set. Leave all other bits unaltered. Do not alter in_var_06. • Modify the value of bit 7 of global variable output_var_07, such that it is equal to 1 if and only if bits 0, 2, 3, 4 of in_var_07 are all set. Leave all other bits unaltered. Do not alter in_var_07. o Modify the value of bit 3 of global variable output_var_08, such that it is equal to 1 if and only if bits 3, 4, 5, 7 of in_var_08 are all set. Leave all other bits unaltered. Do not alter in_var_08. o Modify the value of bit 1 of global variable output_var_09, such that it is equal to 1 if and only if bits 0, 1, 3, 4, 7 of in_var_09 are all set. Leave all other bits unaltered. Do not alter in_var_09. Modify the value of bit 7 of global variable output_var_10, such that it is equal to 1 if and only if bits 1, 4, 5, 6, 7 of in_var_10 are all set. Leave all other bits unaltered. Do not alter in_var_10.
Notes: • The results produced by this program look similar to the following image. The exact values requested in your version of the question will probably be different from those listed below. Use the values requested in your version of the question.
х /w Lawrence@Lawrence-PC / $./test01 255 Input Value bits = {0, 1, 2, 3, 4, 5, 6, 7} Initial output Value bits = {} Search for bit o. Set or clear bit 1: out_var_01 bits = {1} Search for bit 2. set or clear bit 0: out_var_02 bits = {0} Search for bits 0, 1. set or clear bit 0: out_var_03 bits = {0} Search for bits 5, 6. set or clear bit 3: out_var_04 bits = {3} Search for bits 0, 5, 6. Set or clear bit o: out_var_05 bits = {0} Search for bits 2, 5, 6. set or clear bit 0: out_var_06 bits = {0} Search for bits o, 3, 6, 7. set or clear bit 2: out_var_07 bits = {2} Search for bits 2, 4, 5, 7. Set or clear bit 3: out_var_08 bits = {3} Search for bits 0, 2, 3, 4, 7. set or clear bit 0: out_var_09 bits = {0} Search for bits 1, 3, 4, 6, 7. set or clear bit 2: out_var_10 bits = {2} Lawrence@Lawrence-PC /W $
х /w Lawrence@Lawrence-PC /W $./test01 63 255 Input Value bits = {0, 1, 2, 3, 4, 5} Initial output Value bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bit o. Set or clear bit 1: out_var_01 bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bit 2. set or clear bit 0: out_var_02 bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bits 0, 1. set or clear bit 0: out_var_03 bits = {0, 1, 2, 3, 4, 5, 6, 7} Search for bits 5, 6. set or clear bit 3: out_var_04 bits = {0, 1, 2, 4, 5, 6, 7} Search for bits 0, 5, 6. Set or clear bit o: out_var_05 bits = {1, 2, 3, 4, 5, 6, 7} Search for bits 2, 5, 6. set or clear bit o: out_var_06 bits = {1, 2, 3, 4, 5, 6, 7} Search for bits 0, 3, 6, 7. set or clear bit 2: out_var_07 bits = {0, 1, 3, 4, 5, 6, 7} Search for bits 2, 4, 5, 7. Set or clear bit 3: out_var_08 bits = {0, 1, 2, 4, 5, 6, 7} Search for bits 0, 2, 3, 4, 7. set or clear bit 0: out_var_09 bits = {1, 2, 3, 4, 5, 6, 7} Search for bits 1, 3, 4, 6, 7. set or clear bit 2: out_var_10 bits = {0, 1, 3, 4, 5, 6, 7} Lawrence@Lawrence-PC /W $