17

How are negative number represented in 32-bit signed integer? Is it two's or one's complement? or the last bit on the left is like a flag? For example: (-10)

Ahmad Farid
  • 14,398
  • 45
  • 96
  • 136

7 Answers7

18

Most computers these days use two's complement for signed integers, but it can vary by hardware architecture, programming language, or other platform-specific issues.

For a two's-complement representation, the most-significant ("leftmost") bit is referred to as the sign bit, and it will be set for a negative integer and clear for a non-negative integer. However, it is more than just a "flag". See the Wikipedia article for more information.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
6

I think the answer is 0110, preceeded by 1 repeated 28 times, therefore it looks like:

1111 1111 1111 1111 1111 1111 1111 0110;

Steps:

  1. bit representation for 10 is:

    0000 0000 0000 0000 0000 0000 0000 1010;

  2. 0->1 and 1->0 for all the bits:

    1111 1111 1111 1111 1111 1111 1111 0101;

  3. add 1 to the last bit, and propagate to the bit ahead, done!

    1111 1111 1111 1111 1111 1111 1111 0110;

===

You can verify by adding it with 10, and you will get 0 for all bits. As mentioned above, it is 2-based and follows two's complement.

Community
  • 1
  • 1
yanggao
  • 231
  • 4
  • 7
4

Usually it's twos-complement.

duffymo
  • 305,152
  • 44
  • 369
  • 561
4

From the C99 standard:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; there shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M = N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:

— the corresponding value with sign bit 0 is negated (sign and magnitude);

— the sign bit has the value -(2N) (two’s complement);

— the sign bit has the value -(2N - 1) (ones’ complement).

Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones’ complement), is a trap representation or a normal value. In the case of sign and magnitude and ones’ complement, if this representation is a normal value it is called a negative zero.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
1
0xFFFFFFFF = -1
0xFFFFFFFE = -2
0xFFFFFFFD = -3
... 

& so on

zed_0xff
  • 32,417
  • 7
  • 53
  • 72
0

The most significant bit (last bit on the left) is set for negative numbers.

kirk.burleson
  • 1,211
  • 2
  • 18
  • 29
0

Just for reference: negation -> adding one.

Take 5 as an example in 8 bits, quoted from wiki

to convert 5 to -5: 0000 0101 - flip -> 1111 1010 - add one -> 1111 1011

There is a trick to convert a number from positive to negative or vice verse:

Adding them ignoring their signed bit (the leftmost bit) will give you 2^N (where N is the amount of the bits to represent the number).

As the example above in 8-bit representation the sum of 5 (0000 0101) and -5 (1111 1011) will give you 1 0000 0000 which is (2 ^ 8).

Hearen
  • 7,420
  • 4
  • 53
  • 63