Get the sign bits of integers and floating-point numbers in C language

Source: Internet
Author: User

1. Why to get the sign bit


Many times, we need to judge the value of the positive and negative, to do the corresponding logical processing. The conditional judgment statement can be a good completion of this requirement. Sometimes there is the following situation,

if (x > 0) {    x = x-1;} else {    x = 1-x;} if (x < 0) {   x = x;}

Either positive or negative is only the sign bit change of the numerical value or the sign bit change of the calculation result. But we need to use a judgment, the first condition judgment will affect the efficiency, followed by the format is not concise and beautiful. So, sometimes I hope we can solve the problem without condition judgment. The symbolic bit of the value is stored in the highest digit of the value, which can be used to avoid conditional judgments.



2. How to get the sign bit


There may be a number of ways. But to get the sign bit by shifting is the most immediate thought. The shift has left shifted right and shifted right because of a sign bit problem. So, there are 2 cases, signed right shift and unsigned right shift. Signed right shift empty fill sign bit, unsigned right shift vacancy 0. When the signed number, the sign bit to the right of the first position, the result-1 is negative, 0 is positive. When the unsigned number is moved to the first position on the right, the result 1 is negative and 0 positive.



3. A method of implementation


Because the floating-point number cannot be shifted, it is either strongly turned into a positive number, or it is disposed of as a group. Here we use as array processing.

First of all, we treat the numeric value regardless of the type as char[] array,

(Signed char*) &x

This value is split into multiple char types of space, and the sign bits are stored in the highest bit of char space.

((Signed char*) &x) [sizeof (x)-1]

We assume that the small-end storage mode, then the sign bit is in the last space of the char array. We get the char data for the signed bit and treat it as a signed number.

((Signed char*) &x) [sizeof (x)-1] >> 7


Shift right 7 to move the symbol to the right of the first bit, then the positive number is 0, negative is-1

Finally, the complete macro definition

/** * Get x sign bit only for Little-endian * if x >= 0 then  1 * if x <  0 then-1 */#define Mathutils_signbi T (x) (((Signed char*) &x) [sizeof (x)-1] >> 7 | 1)

The result with 1 or, is to map [0,-1] to [1,-1], so that we can write the first example:

X   = (x-1) * Mathutils_signbit (x) x *= mathutils_signbit (x)

Get the sign bits of integers and floating-point numbers in C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.