• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Bitwise opperators

Ok i have a project due and dont' know how to start it, so here is the question:

Write a function that returns x with the n bits that begin at postition p set to the rightmost n bits of y, leaving the other bits unchanged. Do not implement wrap-around. The function prototype should be as follows:

unsigned long setbits(unsigned longx, int p, int n, unsigned long y);

Your test driver shall prompt the user for n, p, and y. Assume that y is being entered in HEX and use the appropriate scanf argument. Then call the function and save the result as the operand and display the changed operand in 8 digit HEX format.
0
AqualostnC
Asked:
AqualostnC
  • 2
  • 2
  • 2
  • +2
1 Solution
 
sdyxCommented:
It is against EE-Policy to do Homework-questions!
0
 
akshayxxCommented:
agree with sdyx.. also see comment on ur other topic
0
 
cybervagrantCommented:
I made more in my Fortran 77 class than it cost me.  I see things are still the same.  I answered the other question for you against policy it seems.  Get yourself a copy of " The C Programming Language" by Kernighan and Ritche, its a great lead in to the language and programming in general.

I'd enjoy answering this question too, but I think it would come back to me :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TheBeaverCommented:
To set bits you would have to use the OR function '|' with a mask of the bits you want to set.

To start the mask at the correct position, you would have to shift the mask to the left using <<

To creat a mask on n bits, you need to shift 1 n bits to the left and minus 1...

4-bits = (1<<4)-1;
       = 16-1
       = 15
       = 00001111 in binary

The trick to this question is the shift !!! As p sounds like the leftmost bit of the mask and not the right. Therefore you should shift right (p - n) times.

As this is an assignment, we should let you use your brain to figure out the immplementation from there.
0
 
AqualostnCAuthor Commented:
sorry, this is what i have so far, but don't know whats wrong with it or if i even did the right thing.


unsigned long setbits(unsigned long x,int p,int n,unsigned long y)
{
unsigned long temp, mask,q;
int counter;
temp=0;
mask=1;
if(n==0)
{
  if ((y&mask)?1:0)
  temp=1;
  else
  temp=0;
}
else
{
   if(n>=32)
    n=32;
mask<<=(n-1);
for(counter=1;counter<=n;counter++)
{
if ((x&mask)?1:0)
   q=1;
else
   q=0;
temp=temp+q;
temp<<=1;
mask>>=1;
}
}            /*temp has the right n bits from y*/
y=temp;
if (p<n)
{
x>>=p;
x<<=p;
x=x+temp;
printf("%d",x);
}
else            /*saving the lower bits of x so we can transfer selected*/
{                  /*bits in while keeping unaffected bits same*/
temp=0;
mask=1;
mask<<=(p-n);
for(counter=1;counter<=n;counter++)
{
if ((x&mask)?1:0)
   q=1;
else
   q=0;
temp=temp+q;
temp<<=1;
mask>>=1;
}
x>>=(p+1);
x<<=(p+1);
y<<=(p-1);
x=x+y+temp;
}        
printf("%d",x);
return 0;
}

0
 
cybervagrantCommented:
Your getting much more involved than necessary :)

#include <stdio.h>

unsigned long setbits(unsigned long x, int p, int n, unsigned long y)  {

   // Be careful about input errors, this lacks error checking //  

   // This implements quick and dirty shifting instead of & masking //

   if ( p < 31 ) x = x << ( 31 - p ); // If p == 31 skip shift //  

   // Slide unwanted bits off the left end //

   if ( n < 32 ) x = x >> ( 32 - n ); // If n == 32 skip shift //

   // Slide wanted bits to the right edge //

   // Unwanted left most bits filled with 0s, x is ready //

   if ( n < 32 ) { y = y >> n; y = y << n; } // If n == 32 skip shifts //

   // Drop n bits off the right edge//

   else y = 0; // For case n == 32 //

   // n Right most bits filled with 0s, y is ready //

   y = y | x;  // Sets to 1 any bits in y that are set to one in x //

return y; // Done! //

}


void main ()  {

  unsigned long x, y;

  int p, n;

  for ( ; ; )

  {
     printf("Enter X, P, N and Y:\n\n");
     scanf ( "%lx %i %i %lx", &x, &p, &n, &y );
     y = setbits( x, p, n, y );
     printf("\n%8.8X \n\n", y);
  }

return;

}

Get the book, its really good!
0
 
TheBeaverCommented:
uh, this IS homework ... EE rules are: homework = no code
0
 
akshayxxCommented:
its really bad..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now