When you shifting a negative number to the right the bit 1
is added at the beginning, not 0
. So when you shift the value 0xFF000000
to the right you get 0xFFFFFFFF
(which is -1
btw.). This ensures that the negative number stays negative and not suddenly become positive because of the shift operation.
However, this applies for Int32 values you have written there. But with the code
(a & 0xFF000000)
you get a result of type Int64
or long
, not int
(or Int32
). So instead of having 0xFF000000
you actually have 0x00000000FF000000
, a positive number. If you shift it to the right you get 0x00000000000000FF
, which is the positive number 255
.
The value 0xFF000000
is a UInt32
value. Combining it with an Int32
value with the &
operator results in a Int64
value.
int a = 4;
uint b = 15;
object c = a & b;
Console.WriteLine($"{a} - {a.GetType()}");
Console.WriteLine($"{b} - {b.GetType()}");
Console.WriteLine($"{c} - {c.GetType()}");
This results in the following output:
4 - System.Int32
15 - System.UInt32
4 - System.Int64