473,756 Members | 5,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sign preserving Vs value preserving

Dear all,

I was going through the book "C a software engineering approach by
darnell & Margolis" there was a section named sign preserving vs value
preserving

it is as follows

sign preserving rule:-
when signed and un signed objects meet in an expression, the result is
always unsigned

ex:- if a is an unsigned short whose value is 2,then the expression
a - 3 evaluates to a very large un signed value rather than
the signed value of -1

value preserving rule:-

This method converts unsigned chars and unsigned shorts to
int,assuming that the int type is larger than unsigned char and
unsigned short respectively. if int is not larger the object is
converted to unsigned int

assuming 16-bit shorts and 32-bit ints in the previous example,
a would be converted to int rather than unsigned int,so the result of
the expression would be -1

note that the difference between sign-preserving and value-preserving
rules only becomes manifest when an unsigned type is shorter than
int.

if both operands are unsigned ints, the result is unsigned, so that
the expression 2u-3u always evaluates to a large unsigned value

1) how valid is the above explanation?

2)is there any implementation where the int type is smaller in size
when compared to unsigned short and unsigned char?

3)Does the condition

sizeof(short) <= sizeof(int) <= sizeof(long)

always holds in ANSI-C ? or is it true for unsigned values ?
Dec 5 '07 #1
3 3299
On Dec 5, 1:44 pm, sophia.ag...@gm ail.com wrote:
Dear all,

I was going through the book "C a software engineering approach by
darnell & Margolis" there was a section named sign preserving vs value
preserving

it is as follows

sign preserving rule:-
when signed and un signed objects meet in an expression, the result is
always unsigned
true
if both operands are unsigned ints, the result is unsigned, so that
the expression 2u-3u always evaluates to a large unsigned value
To be precise, its value is equal to UINT_MAX
1) how valid is the above explanation?
valid.
2)is there any implementation where the int type is smaller in size
when compared to unsigned short and unsigned char?
no, because your 3) is true
3)Does the condition

sizeof(short) <= sizeof(int) <= sizeof(long)

always holds in ANSI-C ? or is it true for unsigned values ?
yes.
Dec 5 '07 #2
vipps...@gmail. com wrote:
sophia.ag...@gm ail.com wrote:>
I was going through the book "C a software
engineering approach by darnell & Margolis" there was
a section named sign preserving vs value preserving

it is as follows

sign preserving rule:-
when signed and un signed objects meet in an expression,
the result is always unsigned

true
No, it's false. Let's look at the OP's (snipped) example.

"ex:- if a is an unsigned short whose value is 2, then
the expression a - 3 evaluates to a very large un signed
value rather than the signed value of -1"

It depends on whether an unsigned short promotes to int
or to unsigned int, and that depends on whether unsigned
short values can be represented by an int.

If USHRT_MAX <= INT_MAX, then the value is -1, otherwise
unsigned short promotes to unsigned int and the value is
UINT_MAX.
if both operands are unsigned ints, the result is
unsigned,
Unless by ints you mean integers. Arithmetic on two
unsigned short values may be signed or unsigned for
reasons stated above.
so that the expression 2u-3u always evaluates to a
large unsigned value

To be precise, its value is equal to UINT_MAX
1) how valid is the above explanation?
valid.
No, it's invalid. If you've been fooled it's probably
because unsigned short has the same width as unsigned
int on the implementations you've seen.
2)is there any implementation where the int type is
smaller in size when compared to unsigned short and
unsigned char?
No addressable type is smaller than a character type.
The standard has rules on the values that are representable
by types of the same signedness but different rank. It
has rules that the unsigned integer must be able to
represent all the non-negative values of it's signed
counterpart (same rank.)

It does not talk about the byte size of the representations
beyond imposing implicit minimums based on how many value
(and sign) bits are required to support the minimum range.
no, because your 3) is true
No, 3) is not necessarily true.
3)Does the condition
sizeof(short) <= sizeof(int) <= sizeof(long)
[Assuming a mathematical relation, rather than C's <=...]

always holds in ANSI-C ? or is it true for unsigned values ?

yes.
No. The standard allows sizeof(short) sizeof(int). All it
requires is that SHRT_MAX <= INT_MAX and INT_MIN <= SHRT_MIN.

You're not likely to find an implementation where shorts are
larger than ints, but the standard doesn't exclude them.

--
Peter
Dec 5 '07 #3
what about this result

when an integer value x is converted to a smaller unsigned integer
type, the result is the non negative remainder of

x/(U_MAX+1)

where U_MAX is the largest number that can be represented in the
shorter unsigned type.

i checked it using the following program

#include<stdio. h>
#include<stdlib .h>
#include<limits .h>
int main(int argc, char* argv[])
{
unsigned short j;

j = 71124;
printf("\n USHRT_MAX = %d",USHRT_MAX );
printf("\n j = %d",j);
printf("\n ......... = %d",(71124) % (USHRT_MAX+1));

puts("");
return(EXIT_SUC CESS);
}

I got the o/p as

USHRT_MAX = 65535
j = 5588
......... = 5588
Dec 7 '07 #4

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

Similar topics

1
6701
by: johndesp | last post by:
I am building an xml document to represent a url request. Some urls contain d equal signs appended to the url. Its important I preserve the equal sign in hte xml document. However, my parser is giving me errors when attempting to parse this document. <url>http://www.whatever.com/techexec/techout.nsf/home.htm?openform&login=1</url> How can I use an equal sign in value part of an element as original text?
12
18234
by: tarmat | last post by:
sorry for this silly little question, but whats the function to grab the sign of a value?
4
14596
by: gimme_this_gimme_that | last post by:
Hi, I Oracle one can have : select to_char(a_id_seq.nextval,'0000') from dual Which fetches a sequence value and pads it with zeros on the left. When the sequence value is more than 4 digits '####' is returned.
34
16872
by: Christopher Benson-Manica | last post by:
I'm trying to compute the absolute value of an integer using only bitwise operators... int x; sscanf( "%d", &x ); printf( "%d\n", (x^((~((x>>31)&1))+1)) + ((x>>31)&1) ); That works, but it assumes 32 bit integers. Is there a portable/standard way to do this? Or are ANSI integers always 32 bits? If not, is using sizeof(int) * 8 - 1 as the shift value an acceptable alternative?
8
4698
by: Fred L. Kleinschmidt | last post by:
I need to know the largets value representable in a variable. However, I do not know the variable's true type - only that it is some kind of int. It may be any of the following: #typedef Newtype short; #typedef Newtype unsigned short; #typedef Newtype int; #typedef Newtype unsigned int; #typedef Newtype long; ....etc.
2
1873
by: Sheldon Simms | last post by:
I would like to portably extract the components of a floating point value: sign, exponent, and significand. Based on the C floating point model described in 5.2.4.2.2, I came up with the following code. There are a couple of things to note about this code. 1) I convert the significand to an integer value in which the least significant bit(s) of the integer correspond/s to the least significant digit of the significand. This means that...
16
5137
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
14
2103
by: TTroy | last post by:
Hello, can anyone explain why the following function will not work for INT_MIN: /* itoa: convert n to characters in s */ void itoa(int n, char s) { int i, sign; if((sign = n) < 0) /* record sign */
18
3918
by: Sven | last post by:
Hi, I found a strange behaviour when using the time() function from time.h. Sometimes when it is called, it does not show the correct time in seconds, but an initial value. This time seem to be the time when the program was started the first time. My platform is a DEC machine with Tru64 onboard. A possible explanation could be, that the time() function is called
0
9454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10028
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9868
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9707
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
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 we have to send another system
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2664
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.