I need help. I'm trying to read a binary file in digits of zeros and ones using C. However, it's not working with me. So I was wondering if anyone can help me and show me how it's done. Thanks in advance.
You test an individual bit by an AND of your data with a bit in a known position.
The bitwise AND operator is &.
An unsigned int containing 1 has only bit 0 set to 1:
0001
You can shift this bit using the bit shift operator <<. This operator shifts bits to the left. There is the >> operator which shifts bits to the right. You use whichever one is best for your situation.
The data you have read is already in binary bits that are either 1 or 0. However, the 1 of a binary bit is not the 1 of a character you can see with your eye. You must either write, or get your Google, and locate a binary to ASCII conversion function.
When you read a file... How do you know when a certain bit is a 1 or a 0 to represent that 1 or that 0 as the number 1 or the number 0 in memory? What I mean is mapping the 1 as the number 1 and the 0 as the number 0 in memory. Thanks for replying.
You test an individual bit by an AND of your data with a bit in a known position.
The bitwise AND operator is &.
An unsigned int containing 1 has only bit 0 set to 1:
0001
You can shift this bit using the bit shift operator <<. This operator shifts bits to the left. There is the >> operator which shifts bits to the right. You use whichever one is best for your situation.
You can open a file in either text or binary mode. Text mode performs certain conversions that are helpful for text files but that will cause problems with binary files.
You will want to be careful to open your file in binary mode. For example: