copyLSB set all bits of result to least significant bit of

/*

copyLSB - set all bits of result to least significant bit of x

example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000

legal ops: ! ~ & ^ | + << >>

Max ops: 5

Rating: 2    */

int copyLSB(int x){

return 2;

}

How do I code this in C? Thank you

Solution

Copying least significant to all bits

1. First x is left-shifted to 31 bit left to remove all bits but not least significant bit

2. Next perform 31bit arthematic right-shift of x to copy the least significant bit to all positions

Code in c

#include <stdio.h>
int copyLSB(int x)

{

return (x << 31) >> 31;
//return !(x&1)+(~0);

}

int main()
{
printf(\"copyLSB(5) = %x\",copyLSB(5));

printf(\"copyLSB(0) = %x\",copyLSB(0));

return 0;
}

Hope this helps you!!

/* copyLSB - set all bits of result to least significant bit of x example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000 legal ops: ! ~ & ^ | + <<

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site