I want to write the numbers 1 and 2 (UInt32) to the file using BinaryWriter, then output all the bytes in turn to the console.
static void Main(string[] args) { FileStream fs = new FileStream(@"G:\out.dat", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write((UInt32) 1); bw.Write((UInt32) 2); bw.Close(); fs = new FileStream(@"G:\out.dat", FileMode.Open); BinaryReader br = new BinaryReader(fs); for (int i = 0; i < 8; i++) { byte b = br.ReadByte(); string byte_in_str = ByteToStr(b); Console.WriteLine(i + 1 + ". " + byte_in_str); } br.Close(); } static string ByteToStr(byte b) { string bin = ""; for (int i = 128; i > 0; i /= 2) if ((b & i) != 0) bin += "1"; else bin += "0"; return bin; } As a result, I have the following result: non-zero bits are in 1st and 5th place for each of the numbers, judging by the conclusion. In theory, the number '1' in the UInt32 representation has only the 32nd nonzero bit, that is, the first 3 bytes are uniquely zero. And here the first byte is non-zero, after which 3 zero passes go, which contradicts the previous statement. What don't I understand?
