From the documentation of the >> operator:
In an arithmetic right shift, the bits shifted beyond the rightmost bit position are discarded, and the leftmost (sign) bit is propagated into the bit positions vacated at the left. This means that if pattern has a negative value, the vacated positions are set to one; otherwise they are set to zero.
If you are using a signed data type, F0B04080
has a negative sign (bit 1
at the start), which is copied to the vacated positions on the left.
This is not something specific to VB.NET, by the way: variable >> 4
is translated to the IL instruction shr
, which is an "arithmetic shift" and preserves the sign, in contrast to the x86 assembly instruction SHR
, which is an unsigned shift. To do an arithmetic shift in x86 assembler, SAR
can be used.
To use an unsigned shift in VB.NET, you need to use an unsigned variable:
Dim variable As UInteger = &HF0D04080UI
The UI
type character at the end of F0D04080
tells VB.NET that the literal is an unsigned integer (otherwise, it would be interpreted as a negative signed integer and the assignment would result in a compile-time error).