, , #: extensions.
32- 4 , 4 []. ? , β . . :
integer = 10399
binary = 00101000 10011111
10399 >> 1 == 0010100 01001111 each bit shifted by 1
8 , 8 ,
10399 >> 8 = 00000000 00101000
, , 0.
, , , 3 :
(byte)(10399 >> 8) == 0010100
, 4 , , , CopyToByteArray:
public static class Int32Extension
{
public static void CopyToByteArray(this int source, byte[] destination, int offset)
{
if (destination == null)
throw new ArgumentException("Destination array cannot be null");
if (destination.Length < offset + 4)
throw new ArgumentException("Not enough room in the destination array");
destination[offset] = (byte)(source >> 24);
destination[offset+1] = (byte)(source >> 16);
destination[offset+2] = (byte)(source >> 8 );
destination[offset+3] = (byte)source;
}
}
, , .
:
int something = 20;
byte[] array = new byte[4];
something.CopyToByteArray(array,0);