PC Review


Reply
Thread Tools Rate Thread

AND operator with byte operands

 
 
The Last Danish Pastry
Guest
Posts: n/a
 
      7th Nov 2011

void Test(byte x, byte y)
{
byte z = x & y;
}

This gives an error: "Cannot implicitly convert type 'int' to 'byte'. An
explicit conversion exists (are you missing a cast?)"

But, in "& Operator (C# Reference)" we read...

Binary & operators are predefined for the integral types and bool.

In "Integral Types Table (C# Reference)" we see that byte is an "integral
type".

What is going on? Programmers have been anding together two bytes to get
another byte for decades.

--
Clive Tooth


 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      7th Nov 2011
On 07.11.2011 11:51, The Last Danish Pastry wrote:
> void Test(byte x, byte y)
> {
> byte z = x& y;
> }
>
> This gives an error: "Cannot implicitly convert type 'int' to 'byte'. An
> explicit conversion exists (are you missing a cast?)"
>
> But, in "& Operator (C# Reference)" we read...
>
> Binary& operators are predefined for the integral types and bool.
>
> In "Integral Types Table (C# Reference)" we see that byte is an "integral
> type".
>
> What is going on? Programmers have been anding together two bytes to get
> another byte for decades.


Probably C# invokes the & operator with implicit propagation to int (as
well as C does). So the result of x&y is of type int rather than byte
(or short). So you have to write
byte z = (byte)(x & y);

The same applies to binary operator +, where the result will not
necessarily fit into a byte.


Marcel
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      7th Nov 2011
On 11/7/2011 10:11 AM, Marcel Müller wrote:
> On 07.11.2011 11:51, The Last Danish Pastry wrote:
>> void Test(byte x, byte y)
>> {
>> byte z = x& y;
>> }
>>
>> This gives an error: "Cannot implicitly convert type 'int' to 'byte'. An
>> explicit conversion exists (are you missing a cast?)"
>>
>> But, in "& Operator (C# Reference)" we read...
>>
>> Binary& operators are predefined for the integral types and bool.
>>
>> In "Integral Types Table (C# Reference)" we see that byte is an "integral
>> type".
>>
>> What is going on? Programmers have been anding together two bytes to get
>> another byte for decades.

>
> Probably C# invokes the & operator with implicit propagation to int (as
> well as C does). So the result of x&y is of type int rather than byte
> (or short). So you have to write
> byte z = (byte)(x & y);
>
> The same applies to binary operator +, where the result will not
> necessarily fit into a byte.


Exactly.

The issue is the same in Java (and Java also requires an
explicit cast).

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:35 AM.