bit representation of negative numbers

This is a discussion on bit representation of negative numbers within the C Programming forums, part of the General Programming Boards category; I was doing some bit operations and when i try to get the bit representation of a negative number say ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    bit representation of negative numbers

    I was doing some bit operations and when i try to get the bit representation of a negative number say -1 on my Intel processor the value that i get is

    11111111111111111111111111111111

    I had this impression that the msb is used to store the sign of a number so i was expecting that only the 0 (the value for 1) and 31st (the sign bit)
    bits should be 1 but then all the bits are 1. I am still unable to understand why is this the case?

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    computers store signed integers in 2's compliment
    Quote Originally Posted by Wikipedia
    The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically, from 2N for an N-bit two's complement). The two's complement of the number then behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way.
    A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value;[1] this system is the most common method of representing signed integers on computers.[2] In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement. An N-bit two's-complement numeral system can represent every integer in the range −2N-1 to +2N-1-1.
    The two's-complement system has the advantage of not requiring that the addition and subtraction circuitry examine the signs of the operands to determine whether to add or subtract. This property makes the system both simpler to implement and capable of easily handling higher precision arithmetic. Also, zero has only a single representation, obviating the subtleties associated with negative zero, which exists in ones'-complement systems.
    Which is a not very helpful way of saying that to change the sign, you invert every bit, then add 1.
    Last edited by abachler; 09-26-2009 at 10:38 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    I was doing some bit operations and when i try to get the bit representation of a negative number say -1 on my Intel processor the value that i get is

    11111111111111111111111111111111

    I had this impression that the msb is used to store the sign of a number so i was expecting that only the 0 (the value for 1) and 31st (the sign bit)
    bits should be 1 but then all the bits are 1. I am still unable to understand why is this the case?
    Notice: the lowest negative number is:
    10000000000000000000000000000000

    and 2^32 is ONE MORE THAN the highest possible unsigned 32-bit number (since 0 is an unsigned int):
    4294967296

    4294967296 - 1 = 429467295, in binary:
    11111111111111111111111111111111
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by abachler View Post
    Which is a not very helpful way of saying that to change the sign, you invert every bit, then add 1.
    Or, if you will, find the lowest significant 1 and invert all bits left of it.
    Of course, with endianess and everything, that might not work out well in computers, but for calculation in your head, it certainly works out alright.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    And furthermore, you should keep in mind that twos-complement isn't always the choice of encoding (though often enough it is), so if possible, you shouldn't base your code on any particular representation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing from positive to negative numbers
    By vopo in forum C++ Programming
    Replies: 19
    Last Post: 09-10-2008, 02:21 PM
  2. Using TextOut() to align positive & negative numbers
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2008, 07:12 AM
  3. Binary representation of numbers...
    By roc in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2003, 07:42 PM
  4. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21