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

USING C!!!! M HW . tmd47560ucrno. 1 CS 3500 Lecture Bit is CS 3500 Assignment 3

ID: 3886683 • Letter: U

Question

USING C!!!!

M HW . tmd47560ucrno. 1 CS 3500 Lecture Bit is CS 3500 Assignment 3 x 0 Program to Convert Dec G chegg 5tudy Guided 5 × x × Secure I https://ucmo.blackboard.cor pid-4505 18 16093 /16 1/courses/201810CS350011906/CS%2U35 nt%203.pdf CS 3500 Assignment 3 Bit Reverse Read a numbar from the user into an unsignad char. Reversa the binary digits of the number antered and print out the resulting decimal numbr. For example, if 1 were entered, ts binary digits are 00000001, so the dgts reversed would be 1000000, which the number 12B in decimal. Print the binary digits for both numbers as well Note: %hhu is the lorrnat specfier that can be used with scarfto read an unsigned char as wel as with printf) to print an uneigned char Sample Runs: Bite: C0ODU001 Enter Number: 64 Bita: c1000000 Reversed: 2 Bita: Co000010 Exercise 2.15 Folow the instructons in the book Sample Run: Number: 0 Bita: C0o000000000000000000000000D0000 Enter a comand set, 2-clear, exit): 1 Enter a bit pealtion: 6 nber: 64 Enter a comand-set, 2lear, 0-exit)1 Enter a bit pos tion : 31 Nunber Bite: 1000000C000000000000000001000000 Enter a bit position: 3 Nunber: 6 Bita: C0000000000000 000000000001000000 Enter command (1-5et, 2 -clear, o "exit] : 0 627 PM :: 9/18/2017 Type here to search

Explanation / Answer

#include<stdio.h>


int Clear(int n, int k)
{
   if (k <= 0) return n;
   return (n & ~(1 << (k - 1)));
}

void display(int num){
    unsigned int size = sizeof(unsigned int);
    unsigned int mP = 1<<(size*8-1);
    int i=0,j;
    
    for(;i<size*8;++i){
       printf("%u ",num&mP ? 1 : 0);
       num = num<<1;
    }
}

int main(){
    int choice,bitno;
    int num = 0;
   
    display(num);
    do {
       printf(" 1.Set a bit ");
       printf("2.Clear a bit ");
       printf("1.Quit ");
       printf("Enter your choice :");
       scanf("%d",&choice);

       switch(choice) {
           case 1: printf("Enter which bit :");
                   scanf("%d",&bitno);
                   num |= (1<<bitno);
                   display(num);
                   break;
           case 2: printf("Enter which bit :");
                   scanf("%d",&bitno);
                   num = Clear(num, bitno+1);
                   display(num);
                   break;
           
       }
     
    } while(choice != 3);


}