In: Computer Science
Using C (not C++):
setFirst - returns value with n upper bits set to 1 and 32-n lower bits set to 0
* You may assume 0 <= n <= 32
* Example: setFirst(4) = 0xF0000000
* Legal ops: ! ~ & ^ | + << >> (NO IF OR FOR LOOPS)
* Max ops: 10
* Rating: 2
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly
indented for better understanding.
#include <stdio.h>
unsigned int setFirst(int n)
{
// take the number as 0.
unsigned int num=0;
// left shift the number of bits n and subtract 1 from the
result.
num = (1 << (n)) - 1;
// shift the bits to right and return the answer
return num<<(32-n);
}
int main()
{
printf("n=4. It's Hex representation is %x\n",setFirst(4));
printf("n=8. It's Hex representation is %x\n",setFirst(8));
printf("n=15. It's Hex representation is %x\n",setFirst(15));
return 0;
}
==========