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

Home Posts Topics Members FAQ

C# language specs bug with section 7.11.2 ?

GP
It appears to be a flaw in the specs, or I did not understand how it works.

7.11.2 User-defined conditional logical operators says that in order to
overload conditional logical operators && and || (which cannot be directly
overloaded in C#) you need to overload the true and false operators and the
compiler will convert expressions like (x && x) to T.op_False(x) ? x :
T.op_BitwiseAnd(x, y). However this results in evaluating the wrong logical
expression since

1 && 2 == op_False(1) ? 1 : op_BitwiseAnd(1, 2) == 0

Now, if I change the op_BitwiseAnd() operator to work as a op_LogicalAnd
then I'm breaking the bitwise And. Also having a bitwise operator acting
like a logical operator seems kind of a bad inconsistency in the language
specs.

Is there something I did not understand, or is there a fix planned for this?

--Gianluca
Nov 16 '05 #1
5 1420
Wow - a real C# language question.
You're misreading the spec and mixing scalar and logical operations.

&& and & are the logical and operators.
&& short-circuits, & does not.
For scalar types, applying & to two instances performs a bitwise operation.
This is not the case for other types.
For scalar types, you cannot apply && or similar operators, as it makes no
sense to short-circuit these operations.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey

Nov 16 '05 #2
GP
Hi Mickey, if you try to overload the conditional logical operators,
following the specs, you will always get the wrong result either in the
logical ops or the bitwise ops. It still seems to me that there is something
wrong in the specs. The only way to make it work is apply logical And in the
bitwise And operator which appears to be logically wrong.

Look at this code:

DbNumber n1 = new DbNumber(1);
DbNumber n2 = new DbNumber(1);

if (n1 && n2)
{
// never gets here because the compiler generated this:
// op_True( op_False(n1) ? n1 : op_BitwiseAnd(n1, n2))
// and it is wrong!
}

if (n1 || n2)
{
// works ok.
}

DbNumber n3 = n1 & n2; // works ok.
DbNumber n4 = n1 | n2; // works ok.
class DbNumber { // it could be struct, it makes no difference

private int m_value;

public DbNumber(int value) {
this.m_value = value;
}

// bitwise and
public static DbNumber operator &(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_value & n2.m_value);
}

// bitwise or
public static DbNumber operator |(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_value | n2.m_value);
}

// logical operators
public static bool operator true(DbNumber n) {
return n.m_value!=0;
}
public static bool operator false(DbNumber n) {
return n.m_value==0;
}
}
"Mickey Williams" <my first name at servergeek.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
Wow - a real C# language question.
You're misreading the spec and mixing scalar and logical operations.

&& and & are the logical and operators.
&& short-circuits, & does not.
For scalar types, applying & to two instances performs a bitwise operation. This is not the case for other types.
For scalar types, you cannot apply && or similar operators, as it makes no
sense to short-circuit these operations.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey

Nov 16 '05 #3
GP
> DbNumber n1 = new DbNumber(1);
DbNumber n2 = new DbNumber(1);
should be
DbNumber n1 = new DbNumber(1);
DbNumber n2 = new DbNumber(2);
"GP" <no****@nospam.com> wrote in message
news:GN*****************@nwrddc01.gnilink.net... Hi Mickey, if you try to overload the conditional logical operators,
following the specs, you will always get the wrong result either in the
logical ops or the bitwise ops. It still seems to me that there is something wrong in the specs. The only way to make it work is apply logical And in the bitwise And operator which appears to be logically wrong.

Look at this code:

DbNumber n1 = new DbNumber(1);
DbNumber n2 = new DbNumber(1);

if (n1 && n2)
{
// never gets here because the compiler generated this:
// op_True( op_False(n1) ? n1 : op_BitwiseAnd(n1, n2))
// and it is wrong!
}

if (n1 || n2)
{
// works ok.
}

DbNumber n3 = n1 & n2; // works ok.
DbNumber n4 = n1 | n2; // works ok.
class DbNumber { // it could be struct, it makes no difference

private int m_value;

public DbNumber(int value) {
this.m_value = value;
}

// bitwise and
public static DbNumber operator &(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_value & n2.m_value);
}

// bitwise or
public static DbNumber operator |(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_value | n2.m_value);
}

// logical operators
public static bool operator true(DbNumber n) {
return n.m_value!=0;
}
public static bool operator false(DbNumber n) {
return n.m_value==0;
}
}
"Mickey Williams" <my first name at servergeek.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
Wow - a real C# language question.
You're misreading the spec and mixing scalar and logical operations.

&& and & are the logical and operators.
&& short-circuits, & does not.
For scalar types, applying & to two instances performs a bitwise

operation.
This is not the case for other types.
For scalar types, you cannot apply && or similar operators, as it makes no sense to short-circuit these operations.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey


Nov 16 '05 #4
Hello, GP.

I think, bitwise and logical semantics are a bit mixed in spec.

Perhaps, it would be more clear to define "x && y" resolution as
"T.false( x ) : false ? T.true( y )". So overloaded '&' wouldn't take
part in short circuit logic.

This definition is much simpler and it strictcly
seperates intuitive semantics:
'&' and '|' are treated as "bitwise" operators, and
'true' and 'false' overloading is left for logical stuff.

But C# design was done in another away. The reason was, may be, to exploit
customization provided by operator overloading.

Imho, your current code is inconsistent with C# spec intuitive sence,
'cause "true( x & y ) != ( true( x ) & true( y ) )".

Best regards!
Nov 16 '05 #5
GP
Hi Alexander,

x && y === > T.false(x) ? false : T.true(y)
x || y === > T.true(x) ? true : T.true(y)

Would be the right and logical way of resolving the logical And and logical
Or. I suspect that the reason why they did not allow for the overloading of
op_LogicalAnd (apparently it is possible in manged C++) is not to allow the
developer to break the conditional evaluation of expressions in the logical
comparison.

My code is inconsistent because the specs are inconsistent IMO. I overloaded
the bitwise And as a bitwise And (which I thought was a reasonable thing to
do). In order to make the x && y work with the "mixed" specs you need to
implement the bitwise And as a logical And and therefore break the bitwise
And op. I couldn't find any other way. A logical And/Or is as different from
a bitwise And/Or as a multiplication operator is different from a system
crash. :) A logical And between 1 && 2 returns true (1) while a bitwise And
will return 0 (it's obvious, I know). Therefore the assumption that x && y
can be translated to x & y is wrong, IMHO.

--
Best regards,
Gianluca Pivato
--
Ice Tea Group, LLC
www.iceteagroup.com
"Alexander Monakhov" <mo******@ispras.ru> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
Hello, GP.

I think, bitwise and logical semantics are a bit mixed in spec.

Perhaps, it would be more clear to define "x && y" resolution as
"T.false( x ) : false ? T.true( y )". So overloaded '&' wouldn't take
part in short circuit logic.

This definition is much simpler and it strictcly
seperates intuitive semantics:
'&' and '|' are treated as "bitwise" operators, and
'true' and 'false' overloading is left for logical stuff.

But C# design was done in another away. The reason was, may be, to exploit
customization provided by operator overloading.

Imho, your current code is inconsistent with C# spec intuitive sence,
'cause "true( x & y ) != ( true( x ) & true( y ) )".

Best regards!

Nov 16 '05 #6

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

Similar topics

34
by: nobody | last post by:
This article is posted at the request of C.W. Yang who asked me to detail my opinion of Lisp, and for the benefit of people like him, who may find themselves intrigued by this language. The...
105
by: Peter Hickman | last post by:
Well after all this discussion it would appear that a 'Python like' language has appeared => Prothon. http://www.prothon.org/index.html Very alpha, sort of like Python (if you consider the...
63
by: Tristan Miller | last post by:
Greetings. Do any popular browsers correctly support <q>, at least for Western languages? I've noticed that Mozilla uses the standard English double-quote character, ", regardless of the lang...
16
by: Ilkka Huotari | last post by:
Hi, Here are some HTML and CSS specs that I have reformatted: http://www.visiomode.com/docs/ I hope somebody else finds them useful too... The CHMs are quite good for checking things quickly,...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
6
by: Rene | last post by:
The section 7.13.2 (Compound assignment) of the C# language specification has the following paragraph: The term "evaluated only once" means that in the evaluation of x op y, the results of any...
4
by: Frank Rizzo | last post by:
I am about to jump in to developing a heavy duty winforms app with a lot of 3rd party (DevExpress, Infragistics) controls in VS 2005. What are the realistic specs for a laptop, so that my...
1
by: VK | last post by:
It is possibly more suitable to address this question to W3C mailing list, but I'm trying here first. Could anyone comment on <http://www.w3.org/TR/REC-xml/#sec-rmd> The first statement says:...
28
by: Master Programmer | last post by:
I heard from a friend that C++ is mainly being replaced as language of choice for most C++ programmers around the world. I guess this makes sence as it means that when learning you dont have to...
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
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
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: 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
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.