435,640 Members | 2,355 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,640 IT Pros & Developers. It's quick & easy.

bit by bit file reading

 P: 3 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. Oct 27 '16 #1

✓ answered by weaknessforcats

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.

So

0001 << 1 becomes 0010. 0001 << 2 becomes 0100. Likewise 0001 << 3 becomes 1000.

You use this shifted bit with an AND to test individual bits.

Let's say your data has a 5 in it. That would be a bit pattern of 0101.

The bit positions are numbered right to left starting with 0. The rightmost 1 is bit 0. The 0 is bit 1. The 1 on the left is bit 2.

To test bit 0 you AND your data with a bit that is 1 in positiion zero.

data & (1 << 0)

0101
0001
AND
0001 <AND result

If this is true there is a 1 in position 0 of data. 0001 is true therefore there is a bit 1 in positiion 0.

Testing position 1 would be:

data & (1 << 1)

0101
0010
AND
0000 <AND result

Here the AND reesult is 0, or false, so there is no 1 bit in position 1.

Finally,

data & (1 << 2)

0101
0100
AND
0100 <AND result

Again the AND result is not zero, or true, so there is a 1 bit in position 2.

You generalize this by:

data & (1 << x)

where x is the position in data that yoou are testing. This lets you do this in a loop so you can check every bit in your data.

Does this help?

5 Replies

 Expert Mod 5K+ P: 9,197 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. Post again if you need more help. Oct 27 '16 #2

 P: 3 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. Oct 28 '16 #3

 Expert Mod 5K+ P: 9,197 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. So 0001 << 1 becomes 0010. 0001 << 2 becomes 0100. Likewise 0001 << 3 becomes 1000. You use this shifted bit with an AND to test individual bits. Let's say your data has a 5 in it. That would be a bit pattern of 0101. The bit positions are numbered right to left starting with 0. The rightmost 1 is bit 0. The 0 is bit 1. The 1 on the left is bit 2. To test bit 0 you AND your data with a bit that is 1 in positiion zero. data & (1 << 0) 0101 0001 AND 0001

 P: 3 Yeah, thank you so much. I can get back to work now. Oct 28 '16 #5

 Expert 100+ P: 2,400 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: Expand|Select|Wrap|Line Numbers f = fopen("filename", "r+b"); Oct 28 '16 #6 