The sign function of C language returns the following value according to the sign of the given integer: positive number returns 1, negative number returns -1, zero returns 0. This function determines the integer sign through shift operation. MSB 0 means positive number (return 1), MSB 1 means negative number (return -1), MSB 0 means zero (return 0).
The sign function in C language
Introduction
The sign function in C language Function determines the sign of a given integer.
Function prototypeint sign(int x);
##Parameters
: The integer whose sign is to be determined.
Return value
is positive, 1 is returned.
is negative, -1 is returned.
is zero.
How it worksThe sign function uses shift operations to determine the sign of a given integer.
is 0, then
x is a positive number and the function returns 1.
is 1, then
x is a negative number and the function returns -1.
is zero, the most significant bit is 0 and the function returns 0.
Example
<code class="c">#include <stdio.h> int main() { int num1 = 5; int num2 = -12; int num3 = 0; printf("sign(%d) = %d\n", num1, sign(num1)); // 输出:sign(5) = 1 printf("sign(%d) = %d\n", num2, sign(num2)); // 输出:sign(-12) = -1 printf("sign(%d) = %d\n", num3, sign(num3)); // 输出:sign(0) = 0 return 0; }</code>
Output
<code>sign(5) = 1 sign(-12) = -1 sign(0) = 0</code>
The above is the detailed content of What does sign function mean in c language?. For more information, please follow other related articles on the PHP Chinese website!