473,473 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Conversion fron signed to unsigned?

Does a cast from an signed to an unsigned integer always preserve the bit
pattern that represents the value?

signed int a;

unsigned int b;

a = -3;

b = a; /* what happens here ???? */

If int is 16 bits on this system, is b guarantied to get the value 0xFFFD =
65533??

Or should I write the conversion like this to be safe?

b = *((unsigned int *)&a); /* always safe?????? */

What does the standard say about this?
Nov 15 '05 #1
2 9177
"Bill" <bi******@telia.se> wrote:
Does a cast from an signed to an unsigned integer always preserve the bit
pattern that represents the value?
No. It looks like that because the algorithm which is used to convert
out-of-range values to any unsigned integer type is to take the value
modulo <TYPE>_MAX + 1. This is the same as preserving the bit
representation on 2's complement systems. However, the C Standard does
not require 2's complement: 1's complement and sign-magnitude are also
allowed. They're vanishingly rare, though.
signed int a;
unsigned int b;

a = -3;
b = a; /* what happens here ???? */

If int is 16 bits on this system, is b guarantied to get the value 0xFFFD =
65533??
Yes. Which means that it isn't bit-pattern preserving on anything except
2's complement platforms. That is, it does on the vast majority of
platforms, but not necessarily on all. It _does_ produce a predictable
value, though.
b = *((unsigned int *)&a); /* always safe?????? */


No, unsafe. Or rather, unreliable.

Richard
Nov 15 '05 #2
Bill:
Does a cast from an signed to an unsigned integer always preserve the bit
pattern that represents the value?

signed int a;
unsigned int b;

a = -3;
b = a; /* what happens here ???? */


b = -3 + UINT_MAX + 1.

BTW: That means, you can get UINT_MAX by assigning -1 to an unsigned int.

unsigned b = -1; /* b == UINT_MAX */
unsigned short c = -1; /* b == USHRT_MAX */

This has nothing to do with bit patterns, the conversion is defined
in terms of values.

Jirka
Nov 15 '05 #3

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

Similar topics

4
by: Ken Tough | last post by:
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...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
2
by: G Fernandes | last post by:
Hello, I have a question about using an unsigned int and a signed long int in an expression (where UINT_MAX > LINT_MAX). Based on the conversion rules, the unsigned int will be convered to...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
5
by: Peng Yu | last post by:
Hi, Please see the following code and the output. It seems that if one of %'s oprand is unsigned, the other will also be converted to unsigned. There is a operator precedence table in...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.