# How can compiler determine signed unsigned

• 04-27-2007
sawer
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?
• 04-27-2007
KONI
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.
• 04-27-2007
Salem
> -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)
• 04-27-2007
brewbuck
Quote:

Originally Posted by sawer
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.
• 04-27-2007
CornedBee
Quote:

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.
• 04-27-2007
brewbuck
Quote:

Originally Posted by CornedBee
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 :p