AqualostnC
asked on
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.
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.
It is against EE-Policy to do Homework-questions!
agree with sdyx.. also see comment on ur other topic
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 :)
I'd enjoy answering this question too, but I think it would come back to me :)
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.
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.
ASKER
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;c ounter++)
{
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;c ounter++)
{
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;
}
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;c
{
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;c
{
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
uh, this IS homework ... EE rules are: homework = no code
its really bad..