?
Solved

Bitwise opperators

Posted on 2003-02-20
8
Medium Priority
?
189 Views
Last Modified: 2012-08-13
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
Comment
Question by:AqualostnC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Expert Comment

by:sdyx
ID: 7992163
It is against EE-Policy to do Homework-questions!
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 7992208
agree with sdyx.. also see comment on ur other topic
0
 

Expert Comment

by:cybervagrant
ID: 7996458
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
Industry Leaders: 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!

 
LVL 1

Expert Comment

by:TheBeaver
ID: 7996693
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
 

Author Comment

by:AqualostnC
ID: 8006672
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
 

Accepted Solution

by:
cybervagrant earned 200 total points
ID: 8014325
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
 
LVL 1

Expert Comment

by:TheBeaver
ID: 8016303
uh, this IS homework ... EE rules are: homework = no code
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8022200
its really bad..
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question