How would I read a text file into a std::bitset object?
For example, I have a text file (myfile.txt) that contains a string ("The cow jumped over the moon."). How would I read the string into a bitset as a sequence of binary digits? Would I need to know, in advance, the number of bits in the text file in order to satisfy the N argument in bitset<N>.
In class we've learned about reading a file using ifstream and ios::binary, but I'd like to use a bitset for my project.
You would need to know at compile time the size of the file, which is not possible. Why do you want the whole file in the bitset, instead of individual bytes?
I need to do a CRC check on the entire file. So basically I need to read the file into some type of variable as one long binary number, then divide the binary number by another binary number using CRC division. Lastly, I need to store the remainder (the checksum) in another variable.
I thought that using bitsets would be the way to go since I can use bitwise operations on the entire container.
For now though all I want to do is figure out a way to get the file contents into a bitset as one string of binary digits.
So, you don't need to perform some kind of explicit conversion of the text file to binary?
We are using CRC-4 (10011) as the divisor. I'm guessing I would need to XOR the first 4 bits of the dividend (the text file) with the divisor. Then shift to the right and XOR again. And, continue doing this until there are no more bits in the divisor.
Unfortunately, I'm still having trouble envisioning this in code. Could you offer some insight?
8. A Straightforward CRC Implementation
---------------------------------------
Etc...
With this in mind, we can sketch an implementation of the CRC
division. For the purposes of example, consider a poly with W=4 and
the poly=10111. Then, the perform the division, we need to use a 4-bit
register:
3 2 1 0 Bits
+---+---+---+---+
Pop! <-- | | | | | <----- Augmented message
+---+---+---+---+
1 0 1 1 1 = The Poly
(Reminder: The augmented message is the message followed by W zero bits.)
To perform the division perform the following:
Load the register with zero bits.
Augment the message by appending W zero bits to the end of it.
While (more message bits)
Begin
Shift the register left by one bit, reading the next bit of the
augmented message into register bit position 0.
If (a 1 bit popped out of the register during step 3)
Register = Register XOR Poly.
End
The register now contains the remainder.
(Note: In practice, the IF condition can be tested by testing the top
bit of R before performing the shift.)
We will call this algorithm "SIMPLE".
I understand how modulus-2 polynomial division works as an algorithm. My problem is converting this algorithm to code.
For example, Duoas, you say "Read a byte. Take the first four bits," but I don't know how to read a byte from a text file. Also could I just read the first four bits rather than a whole byte? The exact size would depend on the size of the checksum, which in my case is four bits long.