U
U
USBHawk2017-10-15 00:45:32
C++ / C#
USBHawk, 2017-10-15 00:45:32

Reading a C# file?

I apologize for the stupid question, but it's kind of hard to understand how bit by bit is read in C#. I found this code on the Internet, can you help me figure out what's going on here?

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace hdlc_ip
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream file = new FileStream("C:\\1.txt",
                FileMode.Open, FileAccess.Read);
            byte[] buf = new byte[1024];
            int r = -1;
            byte b;
            while (r != 0)
            {
                r = file.Read(buf, 0, buf.Length);
                for (int j = 0; j < r; j++)
                {
                    b = buf[j];
                    for (int i = 7; i >= 0; i--)
                        Console.Write((b >> i) & 1);
                    Console.Write((j + 1) % 4 == 0 ? '\n' : ' ');
                }
            }
            file.Close();
            Console.ReadKey(true);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
famidur, 2017-10-15
@USBHawk

A buffer of 1024 bytes is created, part of the file is read into this buffer, then 1 byte is divided by 8 (int i = 7; i >= 0; i--) and a bit is output.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question