473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

C# Compiler Bug?

dlf
In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if the
values being added are constants, but when one is a function it doesn't work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int'
to 'byte'
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert 'int' to 'byte'
}
}

--
Darren
Nov 16 '05 #1
12 1119
Try this:

byOut = Convert.ToByte(ByteFunc() * 2);

José

"dlf" <dl*@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if
the
values being added are constants, but when one is a function it doesn't
work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int'
to 'byte'
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert 'int' to 'byte'
}
}

--
Darren

Nov 16 '05 #2
dlf <dl*@discussions.microsoft.com> wrote:
In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if the
values being added are constants, but when one is a function it doesn't work
correctly.

Is this a compiler bug or am I missing something?
You're missing something, I'm afraid.
// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
The compiler does all of the above at compile time, and can see that
the result is in the bounds of byte. (See section 13.1.6 of the ECMA
spec.)
byOut = ByteFunc(); // OK: No error
Yup, as you'd expect, no doubt.
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int'
to 'byte'
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert 'int' to 'byte'


In both of these cases, the type of the expression (on the right hand
side of the assignment operator) is int, because there is actually no
operator *(byte, byte) - each operand gets converted to int and then
operator *(int, int) is used, which has a resulting type of int. (See
section 14.7.1 of the ECMA spec.)

What you need to do is cast the whole expression:

byOut = (byte) (ByteFunc()*2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
José Joye <jo*******@KILLTHESPAMSbluewin.ch> wrote:
Try this:

byOut = Convert.ToByte(ByteFunc() * 2);


There's no need to call a method to do this - a straight cast will do
fine:

byOut = (byte) (ByteFunc()*2);

The important thing is to make sure that it's the whole expression
which is cast, not just the result of ByteFunc() (which is already a
byte).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi,
Cast the whole expression to byte: byOut = (byte)(ByteFunc() * 2);

"dlf" <dl*@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if the
values being added are constants, but when one is a function it doesn't work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int'
to 'byte'
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert 'int' to 'byte'
}
}

--
Darren
Nov 16 '05 #5
I agree :-).
However, I'm now wondering why we have such a method available:
Convert.ToByte(int).

José
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
José Joye <jo*******@KILLTHESPAMSbluewin.ch> wrote:
Try this:

byOut = Convert.ToByte(ByteFunc() * 2);


There's no need to call a method to do this - a straight cast will do
fine:

byOut = (byte) (ByteFunc()*2);

The important thing is to make sure that it's the whole expression
which is cast, not just the result of ByteFunc() (which is already a
byte).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
dlf
Thanks, this works, but I'm not sure why a byte * byte ends up to be a type
that need to be converted to byte. Seems to me that in other typed languages
that I have used, since all types are the same this woule be impict. So
whats up here? Is the return of the function coming back int even though it
is declared byte?

Darren

"José Joye" wrote:
Try this:

byOut = Convert.ToByte(ByteFunc() * 2);

José

"dlf" <dl*@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if
the
values being added are constants, but when one is a function it doesn't
work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int'
to 'byte'
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert 'int' to 'byte'
}
}

--
Darren


Nov 16 '05 #7
José Joye <jo*******@KILLTHESPAMSbluewin.ch> wrote:
I agree :-).
However, I'm now wondering why we have such a method available:
Convert.ToByte(int).


Well, they work slightly differently - in an unchecked context,
(byte)256 results in 0; Convert.ToByte will *always* throw an
OverflowException.

I suspect it also makes it easier in some cases for autogeneration of
code and possibly some reflection stuff.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
dlf <dl*@discussions.microsoft.com> wrote:
Thanks, this works, but I'm not sure why a byte * byte ends up to be a type
that need to be converted to byte. Seems to me that in other typed languages
that I have used, since all types are the same this woule be impict. So
whats up here? Is the return of the function coming back int even though it
is declared byte?


No, it's the multiplication that is coming back as int. Java works the
same way, btw.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Why are you using bytes, is it because you *actually* need a number of that
many bits, or is it because the limits for the number you are using *just
happens* to marry up to the numerical limits of a byte? If the former, then
perhaps there shouldn't be a reason why you'd want to multiply it by another
byte?

"dlf" wrote:
In this case, I am trying to perfom a math function on a byte value with
another byte value returned by a function. There appears to be no way to
convince the compiler to do this even with explicit casts. Works fine if the
values being added are constants, but when one is a function it doesn't work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int'
to 'byte'
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly
convert 'int' to 'byte'
}
}

--
Darren

Nov 16 '05 #10
Hi dlf,

As Jon said the multiplication is the reason of your confusion.
When you multiply two bytes the result cannot fit in a byte in all some of
the cases. What if you multiplay 255*255. That's why the result of
multiplication is *int*.
There couldn't be conversion from *int* to *byte*. Any alnguage is going to
at least warn you if you try to do this implicitly

--

Stoitcho Goutsev (100) [C# MVP]
"dlf" <dl*@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Thanks, this works, but I'm not sure why a byte * byte ends up to be a type that need to be converted to byte. Seems to me that in other typed languages that I have used, since all types are the same this woule be impict. So
whats up here? Is the return of the function coming back int even though it is declared byte?

Darren

"José Joye" wrote:
Try this:

byOut = Convert.ToByte(ByteFunc() * 2);

José

"dlf" <dl*@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
In this case, I am trying to perfom a math function on a byte value with another byte value returned by a function. There appears to be no way to convince the compiler to do this even with explicit casts. Works fine if the
values being added are constants, but when one is a function it doesn't work
correctly.

Is this a compiler bug or am I missing something?

// VS 2003.net C# code
public class Class1
{
public Class1()
{
}
private byte ByteFunc()
{
return (byte)1;
}
public void MyFunc()
{
byte byOut;
byOut = 1; // OK: No error
byOut = 1*1; // OK: No error
byOut = (byte)1*(byte)2; // OK: No error
byOut = ByteFunc(); // OK: No error
byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int' to 'byte'
byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly convert 'int' to 'byte'
}
}

--
Darren


Nov 16 '05 #11
Stoitcho Goutsev (100) [C# MVP] <10*@100.com> wrote:
As Jon said the multiplication is the reason of your confusion.
When you multiply two bytes the result cannot fit in a byte in all some of
the cases. What if you multiplay 255*255. That's why the result of
multiplication is *int*.


I've never bought that as an explanation, to be honest - it doesn't
explain why int*int doesn't result in a long, and why long*long isn't
banned entirely.

I suspect the reason is much more performance-based - it's cheaper to
convert bytes to ints and do an int*int multiplication than a real
byte*byte multiplication on most modern processors, I guess.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
Stoitcho,
Any alnguage is going to
at least warn you if you try to do this implicitly VB.NET knows that both arguments are bytes & returns a byte from the
expression. Just as an expression with 2 ints return an int.

At run time if the value does not fit in a Byte VB.NET will throw an
OverflowException. Looking at the IL that VB.NET creates it does the
expression as an Integer, then uses conv.ovf.u1 to convert the result back
to a Byte.

Try the following in VB.NET:

Dim b1, b2, b3 As Byte
b1 = 255
b2 = 255
b3 = b1 * b2

Hope this helps
Jay

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eY**************@TK2MSFTNGP09.phx.gbl... Hi dlf,

As Jon said the multiplication is the reason of your confusion.
When you multiply two bytes the result cannot fit in a byte in all some of
the cases. What if you multiplay 255*255. That's why the result of
multiplication is *int*.
There couldn't be conversion from *int* to *byte*. Any alnguage is going to at least warn you if you try to do this implicitly

--

Stoitcho Goutsev (100) [C# MVP]
"dlf" <dl*@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Thanks, this works, but I'm not sure why a byte * byte ends up to be a type
that need to be converted to byte. Seems to me that in other typed

languages
that I have used, since all types are the same this woule be impict. So
whats up here? Is the return of the function coming back int even though it
is declared byte?

Darren

"José Joye" wrote:
Try this:

byOut = Convert.ToByte(ByteFunc() * 2);

José

"dlf" <dl*@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
> In this case, I am trying to perfom a math function on a byte value
with > another byte value returned by a function. There appears to be no
way to > convince the compiler to do this even with explicit casts. Works
fine
if > the
> values being added are constants, but when one is a function it doesn't > work
> correctly.
>
> Is this a compiler bug or am I missing something?
>
> // VS 2003.net C# code
> public class Class1
> {
> public Class1()
> {
> }
> private byte ByteFunc()
> {
> return (byte)1;
> }
> public void MyFunc()
> {
> byte byOut;
> byOut = 1; // OK: No error
> byOut = 1*1; // OK: No error
> byOut = (byte)1*(byte)2; // OK: No error
>
>
> byOut = ByteFunc(); // OK: No error
> byOut = ByteFunc() * 2; // ERROR: Cannot implicitly convert 'int' > to 'byte'
> byOut = (byte)ByteFunc() * (byte)2; // ERROR: Cannot implicitly > convert 'int' to 'byte'
> }
> }
>
> --
> Darren


Nov 16 '05 #13

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

Similar topics

2
by: Jeff Epler | last post by:
Hello. Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild...
7
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I saw cuj's conformance roundup, but the result is quite old. I think many people like me want to know newer c++ standard conformance test...
16
by: pj | last post by:
(Was originally, probably wrongly, posted to the vc subgroup.) (This doesn't appear to be a c# problem, but a problem with a bug in the Visual Studio c# compiler, but, any help will be welcome...)...
0
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around...
3
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error...
1
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
0
by: skip | last post by:
Here's a trivial little Python session which attempts to use compiler.walk (mostly unsuccessfully): % python Python 2.4.2 (#1, Feb 23 2006, 12:48:31) on sunos5 Type "help", "copyright",...
6
by: toton | last post by:
Hi, Anyone have a link to comparative study of different C++ compilers and how much they conform to C++ language standard? Most of the big platforms I know have GCC which well supports C++...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...

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.