473,795 Members | 3,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned short comparison

I have the following C program:

#include <stdio.h>

int main(void)
{
unsigned short x = 32768, y=16384;

if (x > y)
{
printf("greater \n");
}
else
{
printf("smaller \n");
}
return (0);
}

And when run on an MVS mainframe using the GCC
compiler, it prints "smaller". When run on the PC it
prints "greater". Here is the generated assembler:

00004C D201 D05C C072 0005C 000B8 42 MVC 92(2,13),=H'-32768'
000052 D201 D05E C074 0005E 000BA 43 MVC 94(2,13),=H'163 84'
000058 482D 005C 0005C 44 LH 2,92(13)
00005C 492D 005E 0005E 45 CH 2,94(13)

I think the generated code is wrong. It needs to do a
logical compare instead of an aritmetic compare.

But I have one question. Is the above C program
guaranteed to produce "greater"? I was thinking
that maybe the unsigned short was promoted to a
signed int for the comparison and that in the
process the negative value was used. I tried adding
(unsigned) in front of x and y in "x > y" but it didn't
change the generated code.

Thanks. Paul.
Nov 15 '05 #1
1 6153
Paul Edwards wrote:
I have the following C program:

#include <stdio.h>

int main(void)
{
unsigned short x = 32768, y=16384;

if (x > y)
{
printf("greater \n");
}
else
{
printf("smaller \n");
}
return (0);
}
[...]

But I have one question. Is the above C program
guaranteed to produce "greater"? I was thinking
that maybe the unsigned short was promoted to a
signed int for the comparison and that in the
process the negative value was used. [...]


Yes, the operands are promoted for comparison.
The promoted type is implementation-dependent:

- If `signed int' can represent all possible values
of `unsigned short', the promotion is to `signed int'

- Otherwise, the promotion is to `unsigned int'

Either way, the promotion preserves the original values:
the promoted values do not become negative, and the
program should print "greater."

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #2

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

Similar topics

19
6482
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 answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler supposed to do?)
4
12419
by: Anupam | last post by:
Hi, This was asked on our school newsgroup by a friend of mine. He gave the following code and said that it did not print out the elements of the array. He asked for an explanation for this behaviour. I whittled this down a bit but I am unable to pin it to a reference from the standard which I would like to have given when explaining it. Ok here goes. #include <stdio.h>
34
16693
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1 - 3;
10
4359
by: Vijay Kumar R Zanvar | last post by:
Hi clc, Please elaborate the warning: F:\Vijay\C> type unsigned2.c #include <stdio.h> #include <stdlib.h> int
7
1761
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
20
5367
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include <stdlib.h>
26
36090
by: LuB | last post by:
This isn't a C++ question per se ... but rather, I'm posting this bcs I want the answer from a C++ language perspective. Hope that makes sense. I was reading Peter van der Linden's "Expert C Programming: Deep C Secrets" and came across the following statement: "Avoid unnecessary complexity by minimizing your use of unsigned types. Specifically, don't use an unsigned type to represent a quantity just because it will never be negative...
3
11036
by: Alex | last post by:
I have a problem about the comparison between signed and unsigned integer. See the following code: int foo() { int i; unsigned int j; if (i < j) {
10
5650
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl; } else
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
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
10163
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
10000
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
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.