Search code examples
c#arraysbytemasking

C# integer masking into byte array


I'm confused as to why this isn't working, can someone please provide some insight?

I have a function who is taking in an integer value, but would like to store the upper two bits of the hex value into a byte array element.

Let's say if Distance is (24,135)10 or (5E47)16

public ConfigureReportOptionsMessageData(int Distance, int DistanceCheckTime)
    {
        ...
        this._data = new byte[9];
        this._data[0] = (byte)(Distance & 0x00FF); // shows 47
        this._data[1] = (byte)(Distance & 0xFF00); // shows 00
        this._data[2] = (byte)(DistanceCheckTime & 0xFF);
        ...
    }

Solution

  • this._data[1] = (byte)(Distance >> 8);
    

    ?