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

why does adding 2 bytes together result in an int?

my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type 'int' to 'byte'.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do I
need to convert ttl into an int?

Thanks.


--
mo*******@nospam.nospam
Mar 2 '06 #1
13 21433

moondaddy wrote:
my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type 'int' to 'byte'.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do I
need to convert ttl into an int?


What's 128 + 128 ?

--
Larry Lard
Replies to group please

Mar 2 '06 #2
Hello, moondaddy!

m> byte val1 = 10;
m> byte val2 = 23;
m> byte ttl;
m> ttl = val1 + val2; //this line wont compile

m> compile error: Cannot implicitly convert type 'int' to 'byte'.

m> val1 + val2 = 33 which is still in the range of a byte. therefore, why
m> do I need to convert ttl into an int?

IMO the trouble here is that IL opcode 'add' doesn't use byte ( int8 ) type to perform additions. That is why the result value is int32.

if you will write

ttl = (byte)(val1 + val2)
then 'conv.u1' opcode is added that converts the value to neede type ( int8 or byte )...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 2 '06 #3
I know that this was a discussion on code optimisation and the technology
behind .NET, but I think that explicit casting in this case is a good idea
as a matter of principle.

Performing math on a byte is an exceptional affair, so casting makes you
look carefully at code that could very quickly run into trouble. Better yet,
it has the added benefit of creating self-documenting code - you'd have to
be a real ninny not to know why the code's author was casting a byte to and
from an int.

IMHO explicit casting makes the value's severe limitation obvious to other
programmers maintaining your code (and to yourself in five years when you
forgot what you were doing!)

Carlo
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:ud**************@TK2MSFTNGP15.phx.gbl...
Hello, moondaddy!

m> byte val1 = 10;
m> byte val2 = 23;
m> byte ttl;
m> ttl = val1 + val2; //this line wont compile

m> compile error: Cannot implicitly convert type 'int' to 'byte'.

m> val1 + val2 = 33 which is still in the range of a byte. therefore, why
m> do I need to convert ttl into an int?

IMO the trouble here is that IL opcode 'add' doesn't use byte ( int8 )
type to perform additions. That is why the result value is int32.

if you will write

ttl = (byte)(val1 + val2)
then 'conv.u1' opcode is added that converts the value to neede type (
int8 or byte )...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Mar 2 '06 #4
This is because the devleopers of the platform have decided that the return
type of the + operator for the byte type should be an integer. That makes
sense, because (1) the return type must be the same for all operations, and
(2) there is no guarantee that adding 2 or more byte values together will
not overflow the size of a byte, which is 256.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"moondaddy" <mo*******@nospam.nospam> wrote in message
news:Of**************@TK2MSFTNGP15.phx.gbl...
my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type 'int' to 'byte'.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do
I need to convert ttl into an int?

Thanks.


--
mo*******@nospam.nospam

Mar 2 '06 #5
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
This is because the devleopers of the platform have decided that the return
type of the + operator for the byte type should be an integer.


Not quite. There *aren't* any + operators for the byte type. These are
the integer addition operators:

int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);

When it looks like you're adding two bytes, you're actually implicitly
converting each of them to an integer, then adding the two integers
together.

--
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
Mar 2 '06 #6
Okay, Jon, *be* that way. ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
This is because the devleopers of the platform have decided that the
return
type of the + operator for the byte type should be an integer.


Not quite. There *aren't* any + operators for the byte type. These are
the integer addition operators:

int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);

When it looks like you're adding two bytes, you're actually implicitly
converting each of them to an integer, then adding the two integers
together.

--
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

Mar 2 '06 #7

"moondaddy" <mo*******@nospam.nospam> wrote in message
news:Of**************@TK2MSFTNGP15.phx.gbl...
my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type 'int' to 'byte'.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do
I need to convert ttl into an int?


There is no really good reason.

What Jon said about the IL opcodes is true but irrelevant - the compiler
could just cast the result back to byte for you.

What others said about 128 + 128 is true but inconsistent - the same
arguments could be applied to int or long.

I agree that it is a total pain and either an oversight or excess nannying
by MS but that's the way that it is.

On the plus side it would break no code to change it so MS might do it if
enough people complain.

P.S. It's the same for short - but who uses short?
Mar 3 '06 #8
"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:PL********************@fe2.news.blueyonder.co .uk...
What others said about 128 + 128 is true but inconsistent - the same
arguments could be applied to int or long.

I agree that it is a total pain and either an oversight or excess nannying
by MS but that's the way that it is.
There was a good amount of healthy debate at The Old New Thing:

http://blogs.msdn.com/oldnewthing/ar.../10/87247.aspx

I think the best reason stated was for consistency with C++, as weak as that
may or may not be.
On the plus side it would break no code to change it so MS might do it if
enough people complain.


byte firstNumber = 128;
byte secondNumber = 128;
int sum = firstNumber + secondNumber;

I believe sum would equal 256 as things are now and 0 if byte addition was
introduced.
Mar 3 '06 #9

"James Park" <so*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:PL********************@fe2.news.blueyonder.co .uk...
What others said about 128 + 128 is true but inconsistent - the same
arguments could be applied to int or long.

I agree that it is a total pain and either an oversight or excess
nannying by MS but that's the way that it is.


There was a good amount of healthy debate at The Old New Thing:

http://blogs.msdn.com/oldnewthing/ar.../10/87247.aspx

I think the best reason stated was for consistency with C++, as weak as
that may or may not be.
On the plus side it would break no code to change it so MS might do it if
enough people complain.


byte firstNumber = 128;
byte secondNumber = 128;
int sum = firstNumber + secondNumber;

I believe sum would equal 256 as things are now and 0 if byte addition was
introduced.


Good point. I was thinking of "byte sum = firstNumber + secondNumber;"

Allowing assignment of int to byte without a warning would obviously be
unacceptable so I suppose that we just have to live with it although I don't
like the inconsistency:

In x1+x2
2 bytes get promoted to ints.
2 shorts get promoted to ints even though overflow seems rather unlikely in
this case.
2 ints don't get promoted to longs even though they can overflow just as
bytes can.
2 longs couldn't be promoted to anything reasonable even if we wanted to.


Mar 3 '06 #10
Nick Hounsome <nh***@nickhounsome.me.uk> wrote:
val1 + val2 = 33 which is still in the range of a byte. therefore, why do
I need to convert ttl into an int?
There is no really good reason.


There is: performance.

Converting it back to a byte would be a significant (and then
unavoidable) performance hit which you often wouldn't want. This way,
you *can* force the conversion back to a byte, but you don't get it if
you don't want it.
On the plus side it would break no code to change it so MS might do it if
enough people complain.


It would break code. If you had the following overloads:

void SomeMethod (int)
void SomeMethod (byte)

then:

byte a = 10;
byte b = 20;

SomeMethod (a+b);

Current behaviour is to call SomeMethod(int). Changing to a give an
operator of byte +(byte, byte) would change the call to
SomeMethod(byte).

--
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
Mar 3 '06 #11
Thanks to ALL.

This thread was very enlightening. At first it just didn't make sense that
they would allow you to add to ints together and potentially make an
overflow, but not 2 bytes. Hmmmm.

--
mo*******@nospam.nospam
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nick Hounsome <nh***@nickhounsome.me.uk> wrote:
> val1 + val2 = 33 which is still in the range of a byte. therefore, why
> do
> I need to convert ttl into an int?


There is no really good reason.


There is: performance.

Converting it back to a byte would be a significant (and then
unavoidable) performance hit which you often wouldn't want. This way,
you *can* force the conversion back to a byte, but you don't get it if
you don't want it.
On the plus side it would break no code to change it so MS might do it if
enough people complain.


It would break code. If you had the following overloads:

void SomeMethod (int)
void SomeMethod (byte)

then:

byte a = 10;
byte b = 20;

SomeMethod (a+b);

Current behaviour is to call SomeMethod(int). Changing to a give an
operator of byte +(byte, byte) would change the call to
SomeMethod(byte).

--
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

Mar 3 '06 #12
Nick Hounsome wrote:
"moondaddy" <mo*******@nospam.nospam> wrote in message
news:Of**************@TK2MSFTNGP15.phx.gbl...
my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type 'int' to 'byte'.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do
I need to convert ttl into an int?


There is no really good reason.

What Jon said about the IL opcodes is true but irrelevant - the compiler
could just cast the result back to byte for you.

What others said about 128 + 128 is true but inconsistent - the same
arguments could be applied to int or long.

I agree that it is a total pain and either an oversight or excess nannying
by MS but that's the way that it is.

On the plus side it would break no code to change it so MS might do it if
enough people complain.

P.S. It's the same for short - but who uses short?

I second the motion.

Int32 whoops = Int32.MaxValue + 1;

Scott
Mar 4 '06 #13

"Scott C" <no_spam_for_me_sdcoonce@gma_remove_il.com> wrote in message
news:OW****************@tk2msftngp13.phx.gbl...
| Nick Hounsome wrote:
| > "moondaddy" <mo*******@nospam.nospam> wrote in message
| > news:Of**************@TK2MSFTNGP15.phx.gbl...
| >> my understanding is that the max value of a byte is 255. Therefore,
why
| >> does the following code get a compile error?
| >>
| >> byte val1 = 10;
| >> byte val2 = 23;
| >> byte ttl;
| >> ttl = val1 + val2; //this line wont compile
| >>
| >> compile error: Cannot implicitly convert type 'int' to 'byte'.
| >>
| >> val1 + val2 = 33 which is still in the range of a byte. therefore, why
do
| >> I need to convert ttl into an int?
| >
| > There is no really good reason.
| >
| > What Jon said about the IL opcodes is true but irrelevant - the compiler
| > could just cast the result back to byte for you.
| >
| > What others said about 128 + 128 is true but inconsistent - the same
| > arguments could be applied to int or long.
| >
| > I agree that it is a total pain and either an oversight or excess
nannying
| > by MS but that's the way that it is.
| >
| > On the plus side it would break no code to change it so MS might do it
if
| > enough people complain.
| >
| > P.S. It's the same for short - but who uses short?
| >
| >
| I second the motion.
|
| Int32 whoops = Int32.MaxValue + 1;
|
Int32 whoops = Int32.MaxValue + 1;
produces -> error: CS0220 at compile time
and integer overflows will throw when checked mode is enabled.
Is there something else you are looking at?

Willy.

Mar 4 '06 #14

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

Similar topics

10
by: Sunny K | last post by:
Hi guys, I have a field in my DB called EventDate as a DateTime field, therefore it holds both the date and time together like this: '2004-10-14 08:42:57.000'. I need to add together all the...
3
by: Jack | last post by:
Hi, I have a form when loaded, retrieves record from an access table. Among other fields there is a check box called FinalUpdate. This is tied to a field in Access of type Yes/No. The form...
1
by: Robin Tucker | last post by:
I'm considering adding domain integrity checks to some of my database table items. How does adding such constraints affect SQL Server performance? For example, I have a simple constraint that...
4
by: David Link | last post by:
Hi, Why does adding SUM and GROUP BY destroy performance? details follow. Thanks, David Link s1.sql: SELECT t.tid, t.title, COALESCE(s0c100r100.units, 0) as w0c100r100units,
2
by: fineman | last post by:
Hi all, I want to get a 64bit(8 bytes) Encrypt result use DES class in the VS2005. Though I encrypt data is 64bit(8 bytes), but DES return encrypt result that always is 128bit(16 bytes), I don't...
2
by: jho | last post by:
hello, I have a pricing sheet form and have the following fields in one row: SKU DESC UNIT PRICE QTY PRICE I want to select the sku in the form and have the corresponding desc and unit...
25
by: tooru honda | last post by:
Hi, I have read the source code of the built-in random module, random.py. After also reading Wiki article on Knuth Shuffle algorithm, I wonder if the shuffle method implemented in random.py...
2
by: MandySmith | last post by:
i need to find a code that i can use to add bytes together in visual basic
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.