c# - Result of addition of two bytes when it cross its limit -


let have got 2 byte variable:

byte a= 255; byte b= 121;  byte c= (byte) (a + b);  console.writeline(c.tostring()); 

output:120

please explain me how adding values. know crossing size limit of byte don't know operation performs in such situation because not looking chopping result.

thanks

edit: sorry 120 answer.

you overflowing byte storage of 255 starts 0.

so: + b integer = 376

your code equivalent to:

byte c = (byte)376; 

that's 1 of reasons why adding 2 bytes returns integer. casting byte should done @ own risk.

if want store integer 376 bytes need array:

byte[] buffer = bitconverter.getbytes(376); 

as can see resulting array contains 4 bytes necessary store 32 bit integer.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -