473,408 Members | 1,769 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,408 software developers and data experts.

C# Byte/Short Mathematics

is there anyway to use bytes with numbers without actually casting at
the end?

byte Test = 10;
byte row = Test - 5;

Return Error: Cannot implicitly convert type 'int' to 'byte'. An
explicit conversion exists (are you missing a cast?)

I seen some integer-type-suffix's
ulong Test = 10;
ulong row = Test - 5UL;

But I only found "U" and "L"... meaning a number like "5" in "Test -
5" can only set to int / long (uint / ulong)? Whats the suffix's for
short and byte? Or just have to cast it at the end? (byte)(Test - 5)?

Thanks

Jun 14 '07 #1
8 5333
On Wed, 13 Jun 2007 21:31:09 -0700, NvrBst <nv****@gmail.comwrote:
[...]
I seen some integer-type-suffix's
ulong Test = 10;
ulong row = Test - 5UL;

But I only found "U" and "L"... meaning a number like "5" in "Test -
5" can only set to int / long (uint / ulong)? Whats the suffix's for
short and byte? Or just have to cast it at the end? (byte)(Test - 5)?
I'm not aware of a specifier to define a byte constant (other than, of
course, making a named constant of type byte).

However, I'll point out that instead of casting the result of your
calculation, you can cast the constant itself so that the entire
calculation is done with bytes:

byte row = Test - (byte)5;

That way, Test isn't promoted to int, and the expression itself winds up
with type byte instead of type int.

Pete
Jun 14 '07 #2

"NvrBst" <nv****@gmail.comwrote in message
news:11*********************@n15g2000prd.googlegro ups.com...
is there anyway to use bytes with numbers without actually casting at
the end?

byte Test = 10;
byte row = Test - 5;

Return Error: Cannot implicitly convert type 'int' to 'byte'. An
explicit conversion exists (are you missing a cast?)

I seen some integer-type-suffix's
ulong Test = 10;
ulong row = Test - 5UL;

But I only found "U" and "L"... meaning a number like "5" in "Test -
5" can only set to int / long (uint / ulong)? Whats the suffix's for
short and byte? Or just have to cast it at the end? (byte)(Test - 5)?
You have to cast it at the end, because the operands are always promoted.
Even with a byte constant you end up with an int result. Peter is wrong
about this one.

I'm convinced this is a bug, but it is designed into the C# specification.
>
Thanks
Jun 14 '07 #3
Peter Duniho <Np*********@nnowslpianmk.comwrote:
However, I'll point out that instead of casting the result of your
calculation, you can cast the constant itself so that the entire
calculation is done with bytes:

byte row = Test - (byte)5;

That way, Test isn't promoted to int, and the expression itself winds up
with type byte instead of type int.
No, both are promoted. Try it and see:

using System;

class Test
{
static void Main()
{
byte test = 10;
byte row = test - (byte)5;
}
}

Test.cs(8,20): error CS0266: Cannot implicitly convert type 'int' to
'byte'. An explicit conversion exists (are you missing a cast?)

Basically there's no operator byte +(byte, byte). Instead, the operands
are promoted and int +(int,int) is used.

The exception to this is using += and -= :

using System;

class Test
{
static void Main()
{
byte test = 10;
test += 5;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 14 '07 #4
On Wed, 13 Jun 2007 23:24:34 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
[...]
Basically there's no operator byte +(byte, byte). Instead, the operands
are promoted and int +(int,int) is used.
Ugh. Really? Sheesh.
Jun 14 '07 #5
On Wed, 13 Jun 2007 22:01:46 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
[...]
Peter is wrong about this one.

I'm convinced this is a bug, but it is designed into the C#
specification.
I'll be happy to sign the petition. :)
Jun 14 '07 #6
On Jun 14, 8:12 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
Basically there's no operator byte +(byte, byte). Instead, the operands
are promoted and int +(int,int) is used.

Ugh. Really? Sheesh.
I *believe* it's for performance reasons. Processors natively add 32
bit values together with appropriate overflow handling - adding the 8
bit values and then "manually" handling the overflow would be slower
than just treating them as 32 bit values and letting the user add the
overflow handling *where required*.

At least, that's what I've heard before (from people who know). I'm
not saying it's the best decision in the world - language choices
being affected by performance rarely are - but that's the reason I've
heard used pretty frequently.

Jon

Jun 14 '07 #7
Jon Skeet [C# MVP] wrote:
On Jun 14, 8:12 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>>Basically there's no operator byte +(byte, byte). Instead, the operands
are promoted and int +(int,int) is used.
Ugh. Really? Sheesh.

I *believe* it's for performance reasons. Processors natively add 32
bit values together with appropriate overflow handling - adding the 8
bit values and then "manually" handling the overflow would be slower
than just treating them as 32 bit values and letting the user add the
overflow handling *where required*.
AFAIK, modern ALUs are even incapable of byte arithmetic. :)
At least, that's what I've heard before (from people who know). I'm
not saying it's the best decision in the world - language choices
being affected by performance rarely are - but that's the reason I've
heard used pretty frequently.

Jon

--
Göran Andersson
_____
http://www.guffa.com
Jun 14 '07 #8
On Thu, 14 Jun 2007 00:33:17 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
I *believe* it's for performance reasons. Processors natively add 32
bit values together with appropriate overflow handling - adding the 8
bit values and then "manually" handling the overflow would be slower
than just treating them as 32 bit values and letting the user add the
overflow handling *where required*.
I can understand there being a performance issue. I can't understand the
compiler imposing that on the programmer.

To me, the type promotion as it's applied to the code need not match the
type promotion as it's applied to the implementation. Semantically, it
would make sense to allow adding two bytes, so the operator should exist.
If the implementation of the operator was done to promote both operands,
and then demote the results, I wouldn't have a problem with that. But why
exclude the operator altogether?

Pete
Jun 14 '07 #9

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

Similar topics

13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
5
by: _Andy_ | last post by:
I'm looking for the alogithm to take a piece of 8-bit audio data, and scale it to a 16-bit short. I got as far as: private static short ByteToShort(byte b) { int word = 0; word |= ( ( b &...
4
by: TRW1313 | last post by:
I'm looking to populate a byte array of some fixed size to send out over a UDP connection. The data in the byte array is mixed between characters and binary. I'm a beginner to this language. ...
3
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte,...
2
by: aengus | last post by:
Hi, I am looking to find out how to deal with the BYTE datatype in VC, as used below: This struct is part of the API for a Garmin GPS unit: typedef struct { unsigned char mPacketType; ...
9
by: Alberto Cardoso | last post by:
Is there a direct way to convert a short array to a byte array? I dont to use a for and cast every short to a byte. I want something like the BitConverter class that accpets a short array as...
3
by: jackmejia | last post by:
Hello I am fighting to sync a C++ client with a C# server, I have managed to create a byte array in C++ stored as char* to be sent over the network to the server written in C#. on the C# side,...
11
by: K Viltersten | last post by:
While i know that the bytes are cheap today, i still prefer to use a byte (or short) when i know that the entity counted isn't larger than 255 (or 65k). However, it's a real pain to cast every...
10
by: ShadowLocke | last post by:
I'm looking for a better understanding of whats going on with the code i write. I have come across something that I thought I understood but its not working as expected. Its hard to explain so let me...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i 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
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...
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
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,...
0
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...

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.