Question

#include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned int * flag-holderIn C, thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The source code is given below:


/*
* File: BitPattern.c
*
*/

#include <stdio.h>
/* prototype of functions */
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;
/* various set_flag() calling */
set_flag(&flag_holder,3);
set_flag(&flag_holder,16);
set_flag(&flag_holder,31);
  
/* display_32_flag() calling */
display_32_flags(flag_holder);
  
/* various unset_flag() calling */
unset_flag(&flag_holder,31);
unset_flag(&flag_holder,3);
set_flag(&flag_holder,9);
  
/* display_32_flag() calling */
display_32_flags(flag_holder);
return (0);
}

void set_flag(unsigned int * flag_holder, int flag_position)
{
unsigned int a=1;
/* left shifting by flag_position means multiplying by pow(2,flag_position) */
a=a<<flag_position;
if(!check_flag(*flag_holder,flag_position))//if not set
*flag_holder= *flag_holder | a; // then set by using bitwise or
}
void unset_flag(unsigned int * flag_holder, int flag_position)
{
unsigned int a=1;
/* left shifting by flag_position means multiplying by pow(2,flag_position) */
a=a<<flag_position;
*flag_holder= *flag_holder - a ;// subtraction clears the particular bit position
  
}
int check_flag(unsigned int flag_holder, int flag_position)
{ unsigned int a=1;
/* left shifting by flag_position means multiplying by pow(2,flag_position) */
a=a<<flag_position;
if((flag_holder | a)==flag_holder) // if flag bit set
return 1; // then returns 1
else
return 0; // else returns 0
  
}
void display_32_flags(unsigned int flag_holder)
{ int p,count=0;
for(p=31;p>=0;p--)// iteration from total no of bits to 0
{   
printf("%u",flag_holder>>p);// printing bit
count++;
/* following if statement used to group bits in 4*/
if(count==4){
printf(" ");
count=0;
}
/* following statement is an alternative to flag_holder % pow(2,p) for remainder after quotient*/
flag_holder=flag_holder % (1<<p);
}
printf("\n");
}

/************************/

The source code screen shot is given below:

BitPattern.c 3 * File: 4 * 5 */ 7 #include <stdio.h> 8 /* prototype of functions */ 9 void set_flag (unsigned int * flag_hold38 39 a=a<<flag_position; if(! check flag(*flag holder, flag position))//if not set *flag_holder= *flag_holder | a; // then s71 /* following statement is an alternative to flag_holder % pow(2,p) for remainder after quotient*/ flag_holder=flag_holder

The output screen shot is given below:

student@D001 C070:-$ gcc BitPattern. student@D001_C070:-$ ./a.out 1000 0000 0000 0001 0000 0000 0000 1000 0000 0000 0000 0001

Add a comment
Know the answer?
Add Answer to:
In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This daily will allow you to practice more with the bit wise operators and shifts. Consider...

    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...

  • lab3RGB.c file: #include <stdio.h> #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack...

    lab3RGB.c file: #include <stdio.h> #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack; int r_unpack, g_unpack,b_unpack; int alpha = AlphaValue; printf("enter R value (0~255): "); scanf("%d",&r); printf("enter G value (0~255): "); scanf("%d",&g); printf("enter B value (0~255): "); scanf("%d",&b); while(! (r<0 || g<0 || b <0) ) { printf("A: %d\t", alpha); printBinary(alpha); printf("\n"); printf("R: %d\t", r); printBinary(r); printf("\n"); printf("G: %d\t", g); printBinary(g); printf("\n"); printf("B: %d\t", b); printBinary(b); printf("\n"); /* do the packing */ //printf("\nPacked: value %d\t", rgb_pack); printBinary(rgb_pack);printf("\n");...

  • Your program should be capable of creating a ppm image of the flag of Benin • The new flag gener...

    Your program should be capable of creating a ppm image of the flag of Benin • The new flag generated should use the proper width-to-height ratio as defined on Wikipedia for the flag. o For the colors, use pure red, and yellow, and a value of 170 for green. ▪ Pure color values are talked about in Lab 6. o The left green field should be 6/15ths of the width of the flag. Variables country_code should also include a new...

  • Topics: Arrays in C. For this assignment, you will write a C program that uses its...

    Topics: Arrays in C. For this assignment, you will write a C program that uses its first command line parameter to compute and display a histogram of characters that occur in it. Requirements: Your program must compile and run correctly using the gcc compiler on ale. You must write the corresponding function definitions for the following function prototypes: // set all elements of the histogram to zero void init_histogram(int histo[]); // construct the histogram from string void cons_histogram(char string[], int...

  • In C++: Functions to include: editPlayer: is a void fn, which takes an array of playerType...

    In C++: Functions to include: editPlayer: is a void fn, which takes an array of playerType and calls lookUpPlayer which returns the index of the target (-1 if not found). If found allows the user to edit any of the data members of the player via a menu system. lookUpPlayer: is a int fn, which takes an array of playerTypes, prompts the user for the name of the player and attempts to find them. If found, prints message "found" and...

  • Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global...

    Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global variables here. void array_stats() { // Insert your solution here. } #include <stdlib.h> #include <time.h> int main() { // Simulate the test setup process. srand( time( NULL ) ); for ( int i = 0; i < 32; i++ ) { val[i] = rand(); } val_count = rand(); val_mean = rand(); val_min = rand(); val_max = rand(); // Call submitted code. array_stats(); // Display...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {   ...

    Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {    printf("%s,%d\n", ny->name, ny->year); } int main() {    char * ny_format = "%s,%d\n"; #if PHASE >= 1    puts("Phase 1 tests");    // Make sure we can create an object and get the data back.    // We're creating the name as a local array so that we can    // make sure that create_name_year() does not merely make    // a copy of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT