7 Answers
7

Numeric literals in C# are int, not byte (and the bit shift will be evaluated by the compiler, hence only the 510 remains). You are therefore trying to assign a value to a byte which does not fit. You can mask with 255:

byte b = (255 << 1) & 0xFF

to reduce the result to 8 bits again. Unlike Java, C# does not allow overflows to go by undetected. Basically you'd have two sensible options when trying to assign 510 to a byte: Either clamp at the maximum value, then you'd get 255, or throw away the bits that do not fit, in which case you'd get 254.

It should be noted, as I was pushed here from another thread, that even if you have two bytes, any bitwise operation on them will return an int.
–
Ben LeshAug 1 '09 at 3:52

Silly question, why do you use a hex representation of 255 instead of just using 255?
–
Vince PanuccioJul 7 '10 at 6:19

1

@Sir: Habit when using numbers as bitmasks. If you're and -ing or or -ing with a number you don't care about its decimal representation; you want to see immediately what bits are set and which are not.
–
JoeyJul 7 '10 at 9:37