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

Home Posts Topics Members FAQ

value of the constant expression 1<<(1?1:1) < 0x9999

Hello,

one of my C compiler (Keil C51) evaluates the constant expression
1<<(1?1:1) < 0x9999
to the value 1.

// this returns 0, much to my surprise
unsigned char bug4_a(void)
{
return 1<<(1?1:1) < 0x9999;
}

Can this find a satisfactory explanation under some definition of the
C language ?
Note: I still get 0 for

return 1<<(1?1:1) < (unsigned)0x9999;

return 1<<(unsigned char)(1?1:1) < 0x9999;

but I get 1 (as I expect) for

return 1<<(1) < 0x9999;

return (unsigned)1<<(1?1:1) < 0x9999;

return 1u<<(1?1:1) < 0x9999;

return 1<<(1?1:1) < 0x7777;

return 1<<(1?1:1) == 2;

#if 1<<(1?1:1) < 0x9999
return 1;
#else
return 0;

TIA,

Francois Grieu
Jun 27 '08 #1
7 1456
Francois Grieu wrote:
Hello,

one of my C compiler (Keil C51) evaluates the constant expression
1<<(1?1:1) < 0x9999
to the value 1.
Correct...

1 << (1?1:1) < 0x9999
(1 << (1?1:1)) < 0x9999
(1 << 1) < 0x9999
2 < 0x9999
1
// this returns 0, much to my surprise
unsigned char bug4_a(void)
{
return 1<<(1?1:1) < 0x9999;
}

Can this find a satisfactory explanation under some definition of the
C language ?
No.
Note: I still get 0 for

return 1<<(1?1:1) < (unsigned)0x9999;

return 1<<(unsigned char)(1?1:1) < 0x9999;
Integer promotion must be applied, but it is incidental. They
should all return 1 on a conforming implementation.

--
Peter
Jun 27 '08 #2
Francois Grieu <fg****@gmail.comwrites:
one of my C compiler (Keil C51) evaluates the constant expression
1<<(1?1:1) < 0x9999
to the value 1.

// this returns 0, much to my surprise
unsigned char bug4_a(void)
{
return 1<<(1?1:1) < 0x9999;
}

Can this find a satisfactory explanation under some definition of the
C language ?
The behavior is inconsistent with the C standard. (If Keil C51 claims
to conform, it's a bug; if it doesn't, it may or may not be a bug,
depending on what, if anything, its documentation says).

I'll assume that types int and unsigned int are 16 bits.

1<<(1?1:1) has the value 2 and is of type int.

0x9999 has the value 39321 and is of type unsigned int.

The "usual arithmetic conversions" are applied to the operands of "<".
This converts the left operand, 2, from int to unsigned int.

So the expression 1<<(1?1:1) < 0x9999 is equivalent to 1U < 39321U,
which yields the int value 1. The return statement converts this to
unsigned char, yielding (unsigned char)1.

My guess is that a bug in the compiler is causing it to do the "usual
arithmetic conversions" incorrectly in this case. It's probably
converting the right operand to signed int rather than converting the
left operand to unsigned int, changing the comparison:
(int)2 < (unsigned)39321
to
(int)2 < (int)-26215
which yields 0.

Try replacing 0x9999 with 0x7fff and 0x8000 and see what happens.

[snip]

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3
Keith Thompson wrote:
...
I'll assume that types int and unsigned int are 16 bits.

1<<(1?1:1) has the value 2 and is of type int.

0x9999 has the value 39321 and is of type unsigned int.

The "usual arithmetic conversions" are applied to the operands
of "<". This converts the left operand, 2, from int to unsigned int.

So the expression 1<<(1?1:1) < 0x9999 is equivalent to
1U < 39321U,
ITYM: 2U < 39321U

--
Peter
Jun 27 '08 #4
Peter Nilsson <ai***@acay.com.auwrites:
Keith Thompson wrote:
>...
I'll assume that types int and unsigned int are 16 bits.

1<<(1?1:1) has the value 2 and is of type int.

0x9999 has the value 39321 and is of type unsigned int.

The "usual arithmetic conversions" are applied to the operands
of "<". This converts the left operand, 2, from int to unsigned int.

So the expression 1<<(1?1:1) < 0x9999 is equivalent to
1U < 39321U,

ITYM: 2U < 39321U
Yes, thanks.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #5
I am as tired as my cimpiler. Should have said

one of my C compiler (Keil C51) evaluates the constant expression
1<<(1?1:1) < 0x9999
to the value 0.

Thje rest was hopefully correct.

Francois Grieu
Jun 27 '08 #6
On 30 avr, 00:53, Keith Thompson <ks...@mib.orgwrote:
Francois Grieu <fgr...@gmail.comwrites:
one of my C compiler (Keil C51) evaluates the constant expression
1<<(1?1:1) < 0x9999
to the value 0 [typo fixed]
// this returns 0, much to my surprise
unsigned char bug4_a(void)
{
return 1<<(1?1:1) < 0x9999;
}
Can this find a satisfactory explanation under some definition of the
C language ?

The behavior is inconsistent with the C standard. (If Keil C51 claims
to conform, it's a bug; if it doesn't, it may or may not be a bug,
depending on what, if anything, its documentation says).
It is supposed to be "a complete implementation of the ANSI standard
for the C language", without mention of version, so I guess C89. There
is a section on "Differences from ANSI C", with no relevant entries.
I'll assume that types int and unsigned int are 16 bits.
Yes.
1<<(1?1:1) has the value 2 and is of type int.
Yes. 1 is signed, thus 1<<.. is.
0x9999 has the value 39321 and is of type unsigned int.
Yes.
The "usual arithmetic conversions" are applied to the operands of "<".
This converts the left operand, 2, from int to unsigned int.
Thanks. That was the part I'm never sure in on standards before C99.
So the expression 1<<(1?1:1) < 0x9999 is equivalent to 1U < 39321U,
which yields the int value 1. The return statement converts this to
unsigned char, yielding (unsigned char)1.

My guess is that a bug in the compiler is causing it to do the "usual
arithmetic conversions" incorrectly in this case. It's probably
converting the right operand to signed int rather than converting the
left operand to unsigned int, changing the comparison:
(int)2 < (unsigned)39321
to
(int)2 < (int)-26215
which yields 0.
Yes. However the fun thing is that the problem occurs only if
the ?: operator is used.
(int)2 < (unsigned)39321 gives 1
1<<(1?1:1)<39321 gives 0
1<<1 <39321 gives 1
(1?2:2) <39321 gives 0
Try replacing 0x9999 with 0x7fff and 0x8000 and see what happens.
Problem disapears.
Thanks, we found a compiler bug. I'll report it.

Francois Grieu
Jun 27 '08 #7
Try replacing 0x9999 with 0x7fff and 0x8000 and see what happens.

Problem disapears for 0x7fff but remain with 0x8000.
Jun 27 '08 #8

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

Similar topics

9
by: Phil Powell | last post by:
I tried using this query: select max(revenue), bonus from bonuses where revenue < 8411 group by revenue And it produces more than one row, which is what I don't want; I want only one row,...
2
by: The Plankmeister | last post by:
Hi... What's an equivalent of doing: <center>hello!</center> in 4.01 strict? I'm using stylesheets and am telling the various selectors I want to be centred to "text-align : center;" but...
1
by: Rafaela K. Azinhal | last post by:
Hi, I'm a newbie and have a question: I have following PivotTable object in a HTML page <html> <body> <object class='ptdrillthrough' classid="clsid:0002E552-0000-0000-C000-000000000046"...
2
by: GGerard | last post by:
Hello I have an Inventory text box in a datasheet that has the Control Source property set to =* and the Format property set to Currency. The datasheet is linked to the Stock table that has the...
6
by: mark_galeck_spam_magnet | last post by:
Hello, K&R say about the constant expression after #if: "The resulting constant expression is restricted: it must be integral (...)" Clearly, the expression 1 < FOO if FOO is undefined, is...
4
by: martijn | last post by:
H!, Is it possible to get a <tag:id>value</tag:id> value ? When I do this: ----------------------------------------------------- theXML = """<?xml version="1.0"?> <title>The Fascist...
35
by: Steve JORDI | last post by:
Hi, I'm trying to implement a singleton in PHP4 but it doesn't seem to work. The object is recreated each time I call it. The goal of the class is to keep a variable up to date. It's used to...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
by: Riaan | last post by:
As the subject suggests, I am trying to populate a element (of type xs:string) with a entire XML structure from the source. The problem is, I get the transformation to work... but the tranformation...
0
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,...
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...
1
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...
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...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.