What does sign function mean in c language?

下次还敢
Release: 2024-04-13 18:09:32
Original
939 people have browsed it

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).

What does sign function mean in c language?

The sign function in C language

Introduction
The sign function in C language Function determines the sign of a given integer.

Function prototype
int sign(int x);

##Parameters

  • x: The integer whose sign is to be determined.

Return value

  • The return value can be one of the following three values:

      If If
    • x is positive, 1 is returned.
    • If
    • x is negative, -1 is returned.
    • Returns 0 if
    • x is zero.

How it worksThe sign function uses shift operations to determine the sign of a given integer.

    If the most significant bit (MSB) of
  • x is 0, then x is a positive number and the function returns 1.
  • If the most significant bit of
  • x is 1, then x is a negative number and the function returns -1.
  • If
  • x 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>
Copy after login

Output

<code>sign(5) = 1
sign(-12) = -1
sign(0) = 0</code>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template