#include <stdio.h>
int sign(int x) {
return 0 + (x >> 31) - (- x>>31);
}
int main()
{
int x;
printf("Please enter x: \n");
scanf("%d",&x);
int a = sign(x);
printf("returned value is: %d", a);
return a;
}
The logic what i wrote above for sign function is:
The shift operator converts every value which is less than 0 to -1. Every other number to 0,
When we do -x>>31 with x is positive number then it return -1.
But when we do for 0 then n>>31 and -n>>31 both returns 0.
So from above conclusion the formula that created is:
0 + (x >> 31) - (- x>>31)
Output:
1 )
Please enter x:
15
returned value is: 1
2)
Please enter x:
0
returned value is: 0
3)
Please enter x: -50
returned value is: -1
Get Answers For Free
Most questions answered within 1 hours.