473,406 Members | 2,707 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,406 software developers and data experts.

Explicit unsigned/signed conversion: ANSI/ISO rules?

Seems like a simple thing to find out, but I'm struggling. I have
googled, but everything I find is about implicit conversion,
not explicit.

Is this implementation-specific, or does ANSI/ISO lay out what
should happen for:

--------------------------
signed char sc;
unsigned char uc;

uc = 0xFF;
sc = (signed char)uc;

[what value is now in sc? Will it always be -1? What about
non-two's complement representations, etc. I need to know
that coding is not implementation-specific.]
--------------------------

And similarly the case for
sc = -100;
uc = (unsigned char sc);

[what value is guaranteed to be uc?]

--
Ken Tough
Nov 14 '05 #1
4 2632
Ken Tough wrote:

Seems like a simple thing to find out, but I'm struggling.
I have googled, but everything I find is about implicit
conversion, not explicit.

Is this implementation-specific, or does ANSI/ISO lay out
what should happen for:

--------------------------
signed char sc;
unsigned char uc;

uc = 0xFF;
sc = (signed char)uc;

[what value is now in sc? Will it always be -1? What about
non-two's complement representations, etc. I need to know
that coding is not implementation-specific.]
--------------------------

And similarly the case for
sc = -100;
uc = (unsigned char sc);

[what value is guaranteed to be uc?]


All the answers are implementation specific, and are covered by
the values in <limits.h>. Look at CHAR_BIT, UCHAR_MAX, SCHAR_MAX,
SCHAR_MIN. On many systems those values will be 8, 255, 127,
-128, respectively, but that is far from guaranteed.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #2
Ken Tough wrote:
Seems like a simple thing to find out, but I'm struggling. I have
googled, but everything I find is about implicit conversion,
not explicit.

Is this implementation-specific, or does ANSI/ISO lay out what
should happen for:

--------------------------
signed char sc;
unsigned char uc;

uc = 0xFF;
sc = (signed char)uc;

[what value is now in sc? Will it always be -1?

That depends on chars having 8 bits, which is not always true.
See section 6.2.6.2 of the C standard for a discussion of the
representation of negative integers and 6.3.1.3 for conversions between
negative signed integers and unsigned integers. According to my reading
of it, (signed char)0xFF will always be -1 for an 8-bit char.

Robert
Nov 14 '05 #3
Ken Tough wrote:
Seems like a simple thing to find out, but I'm struggling. I have
googled, but everything I find is about implicit conversion,
not explicit.

Is this implementation-specific, or does ANSI/ISO lay out what
should happen for:

--------------------------
signed char sc;
unsigned char uc;

uc = 0xFF;
sc = (signed char)uc;

[what value is now in sc? Will it always be -1? What about
non-two's complement representations, etc. I need to know
that coding is not implementation-specific.]
The behavior is implementation-specific. To begin with,
implementations are free to use more than eight bits for a
`char' type, and some do. On such implementations it may be
the case that SCHAR_MAX is 255 or greater, and if so the
value stored in `sc' will be 255.

On implementations where SCHAR_MAX is less than 255, the
behavior is again implementation-specific, and there are two
broad classes of behavior. First, `sc' may receive some
implementation-defined value: -1, or 0, or 42, or your age
as of your grandmother's last birthday. Alternatively, the
implementation may "trap" the attempt to convert the out-of-
range value, raising an implementation-defined signal. What
happens next depends on whether you have installed a handler
for that signal, and on the implementation's rules for how
the signal is handled.

In short, although `sc = -1' is a common outcome it is
by no means guaranteed.
--------------------------

And similarly the case for
sc = -100;
uc = (unsigned char sc);

[what value is guaranteed to be uc?]


`UCHAR_MAX - 99', after repairing the syntax error.

--
Er*********@sun.com

Nov 14 '05 #4
"Ken Tough" <ke*@objectech.co.uk> wrote:
Seems like a simple thing to find out, but I'm struggling. I have
googled, but everything I find is about implicit conversion,
not explicit.
There is no semantic difference between implicit conversion and explicit
conversion. Both have exactly the same semantics (meaning and result).

The only language difference is that in some cases (especially conversions
involving pointers other than pointers to void) only explicit conversions
are allowed; implicit conversion would generate a constraint violation.

However, all types of signed and unsigned integers and floating-point values
can be implicitly converted. The addition of a cast is unnecessary.
Is this implementation-specific, or does ANSI/ISO lay out what
should happen for:

--------------------------
signed char sc;
unsigned char uc;

uc = 0xFF;
sc = (signed char)uc;

[what value is now in sc? Will it always be -1? What about
non-two's complement representations, etc. I need to know
that coding is not implementation-specific.]
If the signed char type has more than 8 value bits then the value in sc will
always be 255.

Otherwise, the signed char type has 8 value bits, the value is out of range,
and the resulting value is implementation-defined, or (in C99) the
conversion might cause an implementation-defined signal to be raised.

If the implementation takes the obvious easy option and does not change the
representation when converting from signed char to unsigned char, then the
result will be:
binary 11111111 is -1 in 2's complement
binary 11111111 is -0 in 1's complement. Negative zeros
might be silently changed back to normal zeros, or
they might be considered a valid value.
binary 11111111 is -127 in signed magnitude

Or, if the implementation decides to be perverse, it could define the result
in any way it chooses.

--------------------------

And similarly the case for
sc = -100;
uc = (unsigned char sc);
Typo: (unsigned char)sc;
[what value is guaranteed to be uc?]


The value of uc is guaranteed to be UCHAR_MAX - 99. That will be 156 on all
systems with an 8-bit byte. It could be 412 on a 9-bit byte, 65436 on a
16-bit byte, 4294967196 on a 32-bit byte, for example.

--
Simon.
Nov 14 '05 #5

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

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
7
by: HH | last post by:
int main() { uint x = -1; int y = -1; if (x == y) printf("EQUAL"); else printf("NOT EQUAL"); } This code prints "EQUAL". Does this mean that bit comparison is done for the
0
by: | last post by:
Hi everyone- I've been away from coding for a while (I turned into a professional photographer)... So now I've taken up a project involving working with EXIF and XMP metadatain image files...
4
by: Sam | last post by:
Hi, I am using some functions return a base64 encoded string. The functions write it into an unsigned char buffer. I am a little confused as to why a base64 encoded string would be using an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.