473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

May 25 '06 #1
6 1667
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
May 25 '06 #2
In article <11**********************@i39g2000cwa.googlegroups .com>,
gamehack <ga******@gmail.com> wrote:
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.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
May 25 '06 #3
gamehack wrote:
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

May 25 '06 #4
On Thu, 25 May 2006 23:00:34 GMT, "Tomás" <No.Email@Address> wrote:
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.


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
May 25 '06 #5
gamehack wrote:

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.

--
pete
May 26 '06 #6
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

May 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DaVinci | last post by:
I did a google web & group search and didn't find an answer to my question so... here we go. Will try to keep it short. (I am not a complete beginner but am still pretty low on the food chain). ...
1
by: David Gaudine | last post by:
(This is a bit like the recent thread "PHP Switching Sessions".) I use session_start(). When I open my web-based application in two windows on the same system, there's a definite clash; I can't...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
8
by: The Cool Giraffe | last post by:
I'm playing around with pointers trying to figure out how the operator & works. My impression was that it can be used to create a pointer to a thingy. So, i created the following code and ran it....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.