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

1. unsigned lowBitPosition( unsigned n );Returns the position of the lowest-orde

ID: 3641066 • Letter: 1

Question

1. unsigned lowBitPosition( unsigned n );Returns the position of the lowest-order 1-bit in n. For example, if main sayslowBitPosition( 0xBABE0000U )then the function returns 17. If there are no 1-bits, then the function should return UINT_MAX.
2. unsigned lowBitValue( unsigned n );Returns the place value (contribution) of the lowest-order 1-bit in n. For example,lowBitValue( 0x000ABC00U )returns 1024, which is 2 to the 10th power. If there are no 1-bits, then the function should return 0.
3. unsigned highBitPosition( unsigned n );Returns the position of the highest-order 1-bit in n. For example, if main sayshighBitPosition( 0x0BABE000U )then the function returns 27. If there are no 1-bits, then the function should return 32.
4. unsigned highBitValue( unsigned n );Returns the place value (contribution) of the highest-order 1-bit in n. For example,Homework #2 Page 3 of 6highBitValue( 0x000ABC00U )returns 524288, which is 2 to the 19th power. If there are no 1-bits, then the function should return 0.
5. unsigned reverse( unsigned n );The function returns the result of reversing the order of bits in n. For instance, if n’s bits were00010011 01010111 10011011 11011111then the value the function returned would have bits11111011 11011001 11101010 11001000

Please include what main will have.
Ask if you have any questions!
Thank You in the front.

Explanation / Answer

please rate - thanks

there are 2 extra functions, I used as utilities

#include<stdio.h>
#include<conio.h>
#include<limits.h>
unsigned reverse(unsigned);
unsigned lowBitPosition(unsigned);
unsigned highBitPosition(unsigned);
unsigned lowBitValue(unsigned);
unsigned highBitValue(unsigned);
unsigned binaryAToU(const char*);
void show(unsigned);
int main()
{printf("%u ",lowBitPosition(0xBABE0000U ));
printf("%u ",highBitPosition(0x0BABE000U));
printf("%u ",lowBitValue(0x000ABC00U ));
printf("%u ",highBitValue(0x000ABC00U));
show(reverse(binaryAToU("00010011 01010111 10011011 11011111")));
getch();
return 0;
}
unsigned binaryAToU(const char* a)
{int i=0,len;
unsigned val=0,mask=1;
len=strlen(a)-1;
for(i=0;i<=len;i++)
    {if(*(a+len-i)=='1')
         val+=mask;
     if(*(a+len-i)!=' ')  
          mask*=2;   
     }
return val;
}
void show(unsigned a)
{unsigned int mask=0x80000000,b;
int i;
for(i=32;i>0;i--)
   {if(i%8==0&&i!=32)
      printf(" ");
   b=a&mask;
   if(b==0)
      printf("0");
   else
        printf("1");
   mask=mask/2;
    }
printf(" ");
}
unsigned reverse(unsigned a)
{unsigned mask=1,mask2=0x80000000,Value=0,b;
int i;
for(i=0;i<32;i++)
    {b=a&mask;           
    if(b!=0)
       Value+=mask2;
    mask2/=2;   
    mask=mask*2;              
    }
return Value;
}

unsigned lowBitPosition(unsigned a)
{unsigned mask=1,b;
int i;
if(a==0)
    return UINT_MAX;
for(i=0;i<32;i++)
   {b=a&mask;
    if(b!=0)
        return i;
    mask*=2;
    }
}
unsigned lowBitValue(unsigned a)
{unsigned mask=1,b;
int i;
if(a==0)
    return 0;
for(i=0;i<32;i++)
   {b=a&mask;
    if(b!=0)
        return mask;
    mask*=2;
    }
}
unsigned highBitValue(unsigned a)
{unsigned mask=0x80000000,b;
int i;
if(a==0)
    return 0;
for(i=31;i>=0;i--)
   {b=a&mask;
    if(b!=0)
        return mask;
    mask/=2;
    }
}
unsigned highBitPosition(unsigned a)
{unsigned mask=0x80000000,b;
int i;
if(a==0)
    return UINT_MAX;
for(i=31;i>=0;i--)
   {b=a&mask;
    if(b!=0)
        return i;
    mask/=2;
    }

}