c# - Why Do Bytes Carryover? -


i have been playing byte arrays (dealing grayscale images). byte can have values 0-255. modifying bytes, , came across situation value assigning byte outside bounds of byte. doing unexpected things images playing with.

i wrote test , learned byte carries over. example:

private static int setbyte(int y) {     return y; } ..... byte x = (byte) setbyte(-4); console.writeline(x); //output 252 

there carryover! happens when go other way around well.

byte x = (byte) setbyte(259); console.writeline(x); //output 3 

i have expected set 255 in first situation , 0 in second. purpose of carry over? due fact i'm casting integer assignment? when useful in real-world?

byte x = (byte) setbyte(259); console.writeline(x); //output 3 

the cast of result of setbyte applying modulo 256 integer input, dropping bits outside range of byte.

259 % 256 = 3

why: implementers choose consider 8 least significant bits, ignoring rest.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -