C Using Bit Operators Methodone Returns Value N Upper Bits Set 1 32 N Lower Bits Set 0 May Q37180587

In C using bit operators

methodOne- returns value with n upper bits set to 1
   and 32-n lower bits set to 0
   You may assume 0 <= n <= 32
   Example: methodOne(4) = 0xF0000000
   Legal ops: ! ~ & ^ | + << >>


Answer


#include <stdio.h>int methodOne(int n) { return ((1 << 31) >> n) << 1;}int main() { printf(“0x%xn”, methodOne(4)); return 0;}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.