Asignments and different signness of the variables | | |
Hello all,
I was debugging some code today and a few sign problems popped up. So I
wondered how are assignments defined between variable with different
signness. For example:
unsigned char a = 10;
signed char b;
b = a;
Or does the actual signness matter? The only significance I can think
of is how the numbers are interpretered so the actual assignment just
copies the bits. Am I right?
Regards,
gamehack | | | | re: Asignments and different signness of the variables
gamehack posted:
[color=blue]
> So I wondered how are assignments defined between variable with different
> signness.[/color]
signed char a = 56;
unsigned char b = a;
1) When going from signed to unsigned:
1.A) If the source is positive, then the target becomes equal to the
source.
1.B) If the source is negative, the result is implementation-defined.
2) When going from unsigned char to signed char:
2.A) If the source is within range, then the target becomes equal to
the source.
2.B) If it's not within range, the result is implementation-defined.
Basically, if it can store the value, then it will. If it can't, then the
resultant value differs by system.
-Tomás | | | | re: Asignments and different signness of the variables
In article <1148597366.431922.165260@i39g2000cwa.googlegroups .com>,
gamehack <gamehack@gmail.com> wrote:[color=blue]
>I was debugging some code today and a few sign problems popped up. So I
>wondered how are assignments defined between variable with different
>signness. For example:
>unsigned char a = 10;
>signed char b;
>b = a;[/color]
[color=blue]
>Or does the actual signness matter? The only significance I can think
>of is how the numbers are interpretered so the actual assignment just
>copies the bits. Am I right?[/color]
No, it does matter. The result is well defined when one is going
*to* unsigned, but the result is implementation defined when one
is going from unsigned to signed, unless the value fits anyhow.
In practice, likely on most machines you will encounter, the bits
will just be copied, but that's up to the implementation. A student-
oriented compiler might deliberately fault, for example.
In C there are three possible representations of integral values,
and the "copy the bits" heuristic is only valid for one of the three.
It is possible that you may never encounter an actual system with
one of the two other valid representations, but it could happen.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson | | | | re: Asignments and different signness of the variables
gamehack wrote:[color=blue]
> Hello all,
>
> I was debugging some code today and a few sign problems popped up. So I
> wondered how are assignments defined between variable with different
> signness. For example:
> unsigned char a = 10;
> signed char b;
> b = a;
>
> Or does the actual signness matter? The only significance I can think
> of is how the numbers are interpretered so the actual assignment just
> copies the bits. Am I right?[/color]
Assignment copies values, not the representation of those values. (As
a side note, the value bits representating the positive values for a
given signed type are is the same for the corresponding unsigned type
meaning that the value 10 will have the same representation in both a
signed char and unsigned char, an int and an unsigned int, etc.) If
you assign a value to an unsigned variable that cannot be represented
by the variable's type the result will be modulo the maximum value the
type can prepresent plus one, i.e. if UCHAR_MAX is 255, the assignment
a = 258 will result in the value 2 being assigned to a (258 modulo
255+1). Trying to assign a value to a signed type that cannot be
represented is undefined.
Robert Gamble | | | | re: Asignments and different signness of the variables
On Thu, 25 May 2006 23:00:34 GMT, "Tomás" <No.Email@Address> wrote:
[color=blue]
>gamehack posted:
>[color=green]
>> So I wondered how are assignments defined between variable with different
>> signness.[/color]
>
>
>signed char a = 56;
>
>unsigned char b = a;
>
>
>1) When going from signed to unsigned:
>
> 1.A) If the source is positive, then the target becomes equal to the
>source.
> 1.B) If the source is negative, the result is implementation-defined.[/color]
No, in all cases the signed value is converted to a value within the
range of the unsigned variable by adding or subtracting
max_unsigned_value+1.
[color=blue]
>
>
>2) When going from unsigned char to signed char:
>
> 2.A) If the source is within range, then the target becomes equal to
>the source.
> 2.B) If it's not within range, the result is implementation-defined.
>
>
>Basically, if it can store the value, then it will. If it can't, then the
>resultant value differs by system.
>
>-Tomás[/color]
Remove del for email | | | | re: Asignments and different signness of the variables
gamehack wrote:[color=blue]
>
> Hello all,
>
> I was debugging some code today and a few sign problems popped up.
> So I
> wondered how are assignments defined between variable with different
> signness. For example:
> unsigned char a = 10;
> signed char b;
> b = a;[/color]
The right operand of the assignment operator,
is converted to the type of the left operand.
For the above case
b = a;
means exactly the same thing as
b = (signed char)a;
Since the value of a, is less than SCHAR_MAX,
there is no change in value.
[color=blue]
> Or does the actual signness matter? The only significance I can think
> of is how the numbers are interpretered so the actual assignment just
> copies the bits. Am I right?[/color]
Bits are not taken into account,
as Robert Gamble and Barry Schwarz and Walter Roberson
have already replied.
Since the type of b is unsigned char, this statement:
b = -1; /* The type of (-1) is int */
or this statement
b = (signed char)-1;
results in b having a value of of UCHAR_MAX,
regardless of whether the representation of negative integers
is two's complement, one's complement or signed magnitude.
--
pete | | | | re: Asignments and different signness of the variables
Barry Schwarz posted:
[color=blue]
> No, in all cases the signed value is converted to a value within the
> range of the unsigned variable by adding or subtracting
> max_unsigned_value+1.[/color]
Yes you're correct, but so am I, because "max_unsigned_value" is
implementation-specific.
Meet me half way : )
-Tomás |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|