Question

C PROGRAM

When you print out the old and new bitsets, which are of type unsigned char, please use the %p control character in the printff statement rather than %x or %d or %c. The compiler will complain, but we don't always listen to them anyway. The difference is it will print the bitset out in hex with a preceeding %0x.

unsigned char bitset = 0x14 ;

printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14, which is what we want.

How to obtain input from the user and assign it to an unsigned char ?

One way to do this as follows:

               unsigned int Hoboo ; unsigned char bitset ;

             printf("Please enter your bitset using either 0x$$ or $$ where $ is a digit in base 16 (0 - F) -->") ;

              scanf("%x", &Hoboo) ; // interprets input as hex format

               bitset = (unsigned char) Hoboo ;

Whatever the user enters will be interpreted as a hex number. So entering 21 is 21 hex or 33 decimal. Similarly, the user could enter 0x21 which is also 33 decimal.

HW4 Write four functions in C: • ISSET (unsigned char BitSet, int BitPos) returns a 0 if the bit in the given Bit position is(?) This presentation has missing fonts. Show HW4 • PrintBits (unsigned char BitSet) prints the set/not set status of each bi(!) THIS presentation as MISSING TUMS. SHOW HW4 Please provide a while loop similar the following: While(1) { Please enter FuWhile(1) { INPUT PHASE Compute Phase (call the requested function with appropriate parameters) Output phase: if (request == 0

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

Find the code for the above question below, read the comments provides in the code for better understanding. If found helpful please do upvote this.

Note : In the code below bit 1 is the LSB

Code

//stdio is standard library for basic I/O opearertion

#include <stdio.h>

#include<stdlib.h>

#include<string.h>//for string opertions

//this method checks if the given bit ata a particular position is 0 or 1

//note that bit 1 is the LSB

int isset(unsigned char bitset, int pos){

    int val=bitset;//convert the bitset to int

    //now if 2^pos-1 & val == 1 then bit is set else not

    if (val & (1 << (pos - 1)))

        return 1;

    else

        return 0;

}

//sets the bit at a given postion to 1

//note that bit 1 is LSB

void addbit(unsigned char *bitset, int pos){

   int val=*bitset;//get teh decimal number of the bitset

   val |= 1 << pos-1;   //or with 2^pos-1 sets the bit at the given positon

   *bitset=(unsigned char)val;//changes the bitset to the modified value

}

//unsets the bit at a given postion to 1

//note that bit 1 is LSB

void delbit(unsigned char *bitset, int pos){

   int val=*bitset;//get teh decimal number of the bitset

   val &= ~(1 << pos-1);    //and with the negation with 2^pos-1 unsets the bit at the position

   *bitset=((unsigned char)val);//changes the bitset to the modified value

}

//print all teh bits in teh bitset

void printBits(unsigned char bitset){

    char output[8];//stores the bits in a character array

    itoa(bitset, output, 2);//convert the value to binary

    strrev(output);//reverse the array so that 0th index is the LSB

    printf("1st Bit is the LSB");

    //iterate and print all the bits

    for(int i=0;i<strlen(output);i++){

        printf("\nThe bit in position %d id %c",i+1,output[i]);

    }

}

//maind river code of the program

int main()

{

    //to get user input

    unsigned int Hoboo;

    unsigned char bitset;//bitset for the user input

      printf("Please enter your bitset using either 0x$$ or $$ where $ is a digit in base 16 (0 - F) -->");

      scanf("%x", &Hoboo); // interprets input as hex format

    

      bitset = (unsigned char)Hoboo;

//run a loop until user exits it

    while(1){

        int ch;

        //show choices to the user

        printf("\nPlease choose your option 0 - ISSET , 1 = ADD , 2 = DEL , 3 = PRINT , -1 = QUIT : ");

        scanf("%d",&ch);

        if(ch!=-1){

            

        

            if(ch==0){

                int pos;

                printf("\nEnter the bit position to check(1 is th LSB) :");

                scanf("%d",&pos);

                int val=isset(bitset,pos);//call the isset method to check if bit is set or not

                if(val==0)  //val=0 menas bit is set else not set

                    printf("Bit in position %d was not set\n",pos);

                else

                    printf("Bit in position %d was set\n",pos);

            }

            //this is for settign bit at the given pos to 1

            else if(ch==1){

                int pos;

                printf("\nEnter the bit position to set(1 is th LSB) :");

                scanf("%d",&pos);

                printf("\nOld Bitset %p ",bitset);

                addbit(&bitset, pos);

                printf(" , New Bitset %p",bitset);

            }

            //this is for settign bit at the given pos to 0

            else if(ch==2){

                int pos;

                printf("\nEnter the bit position to unset(1 is th LSB) :");

                scanf("%d",&pos);

                printf("\nOld Bitset %p ",bitset);

                delbit(&bitset, pos);

                printf(" , New Bitset %p",bitset);

            }

            //this if for printing teh bits

            else if(ch==3){

                printBits(bitset);

            }

        }

        else{

            exit(0);

        }

        

    }

   

    printf("%p\n\n",bitset);

    printBits(bitset);

    int chk=isset(bitset , 1);

    printf("\n%d",chk);

}

//stdio is standard library for basic I/O opearertion #include <stdio.h> #include<stdlib.h> #include<string.h>//for string op

//maind river code of the program int main() //to get user input unsigned int Hoboo; unsigned char bitset;//bitset for the usPIINILI SHILIILEI LIIC DIL PUSILIUL CU SCL115 LILLJD) . , scanf(%d,&pos); printf(\nold Bitset %p , bitset); addbit(&bitse

Output

Please enter your bitset using either ox$$ or $$ where $ is a digit in base 16 (0 - F) -->21, Please choose your option 0 - I

Check the output carefully , it is same as expected

Add a comment
Know the answer?
Add Answer to:
C PROGRAM When you print out the old and new bitsets, which are of type 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
  • Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code...

    Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • Edit, compile, and run the following programs on the UNIX shell: Write a program that takes...

    Edit, compile, and run the following programs on the UNIX shell: Write a program that takes in six commandline arguments and has four functions (described below) that use bitwise operators. The user should enter six space-separated commandline arguments: four characters (any ASCII character) followed by two integers. Anything else should print an error message telling the user what the correct input is and end the program. Convert the commandline input into "unsigned char" and "unsigned int" datatypes. Be careful with...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • Write a C program that does the following: Displays a menu (similar to what you see...

    Write a C program that does the following: Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square,Diamond (with selected height and selected symbol), or Quits if user entered Q.Apart from these 2 other shapes, add a new shape of your choice for any related character. Program then prompts the user to enter a number and...

  • In the Source Folder (src) of this project create a new C source file called "gcd.c"....

    In the Source Folder (src) of this project create a new C source file called "gcd.c". Once again, copy the contents of the "main.c" file above into the "gcd.c" source file. Modify this program to NOT ask the user to input the second Positive Integer, if the user has entered a program terminating value for the "first" input Postitive Integer. #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { //============================== setbuf(stdout, NULL); // turns standard output buffering off int...

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

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