Asignments and different signness of the variables

G

gamehack

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
 
T

Tomás

gamehack posted:
So I wondered how are assignments defined between variable with different
signness.


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
 
W

Walter Roberson

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?

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.
 
R

Robert Gamble

gamehack said:
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?

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
 
B

Barry Schwarz

gamehack posted:



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.

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.
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


Remove del for email
 
P

pete

gamehack said:
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;

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.
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?

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.
 
T

Tomás

Barry Schwarz posted:

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.


Yes you're correct, but so am I, because "max_unsigned_value" is
implementation-specific.

Meet me half way : )


-Tomás
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top