Thread: How can compiler determine signed unsigned

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    How can compiler determine signed unsigned

    Hi
    How can assembler determine that a number is signed or unsigned?
    we are looking first bit of a number for positive or negative.
    +0 i.e. 0000 0000
    -0 i.e. 1000 0000
    But how can compiler understand if it is signed or unsigned?

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    The compiler considers each variable that isn't specified as being signed (or unsigned depending of your compiler). So for every integer where you don't specify it explicitly, it automatically assumes that the first bit sets the sign.

    It depends on the compiler you use if it automatically assumes that the number is signed or unsigned.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > -0 i.e. 1000 0000
    There is no -0 in a twos-complement representation (the most common one in use).

    Whether a number is signed or unsigned depends on the instructions the compiler chooses to use, based on the code you have written.

    As many people know, dividing by 2 is the same as shifting right by one.
    So for an unsigned int, the compiler might choose a SHR instruction (which shifts in a zero)
    for a signed int, the compiler might choose a ASR instruction (which shifts in the sign bit)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sawer View Post
    Hi
    How can assembler determine that a number is signed or unsigned?
    we are looking first bit of a number for positive or negative.
    +0 i.e. 0000 0000
    -0 i.e. 1000 0000
    But how can compiler understand if it is signed or unsigned?
    Weird -- looks like you're on a one's complement system. Most modern system are two's complement. If all you have is a bit pattern, there is no way to tell. At the compiler level, signed/unsigned is a property of a variable. But at the machine level, it is a property of an operation, not a value. For instance, you can multiply two values in either a signed sense or an unsigned sense.

    So, the COMPILER knows because you declare it one way or the other. The machine has no clue, it is just told to perform specific operations which give the results indicated by the source code.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Weird -- looks like you're on a one's complement system.
    No, -0 in 1's complement is 11111111. 10000000 is a simple sign/value representation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    No, -0 in 1's complement is 11111111. 10000000 is a simple sign/value representation.
    Ah, you're right. Shows how much exposure I have to that stuff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsigned Long returning signed value
    By dinklebaga in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 06:07 AM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM
  5. DIfference between signed and unsigned integers ?
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 01-15-2002, 09:27 AM