How to know if a binary integral number represents a negative number?

前端 未结 5 442
别那么骄傲
别那么骄傲 2021-02-04 00:06

I am reading some C text. In the Negative and Positive Values session, the author mentioned several ways of representing a negative number in binary form.

I understood a

5条回答
  •  感情败类
    2021-02-04 01:05

    If you have the value in memory, cast it to a signed in the same size and test if it’s less than zero. So, if ((int)value < 0).

    If you’re trying to parse a binary constant from a string, you need to know the format of the number. However, two’s-complement has been universal for fifty years now. (The one exception is binary-compatible support for certain old Unisys mainframes that are still being used.) For that, you just need to look at the first bit (as the accepted answer says).

提交回复
热议问题