473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Best way to read one bit at a time from a binary file?

I'm trying to do file compression/decompression in C and I need to handle one bit at a time. I've looked around online and haven't seem to found anything that I can figure out how to implement nicely.

I currently try to do:

unsigned char byte = fgetc(fptr);

and later

byte >>= 1;

but the problem is that I have to use the first bit of the byte and then treat the next 8 bits as one byte. The byte usage keeps shifting over in this way. It's probably quite clear that I'm a bit lost. Any help would be greatly appreciated!
Nov 9 '13 #1
1 2420
weaknessforcats
9,208 Expert Mod 8TB
C you not just do a bitwise AND of the first bit using an appropriate maskk determine the state of the first bit?

I don't know what you means by "first bit". Is that the first bit read in from the file. Or is it the last bit of the first byte read in from the file? Is this Big Endian or Little Endian?
Nov 22 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jesse Noller | last post by:
The problem: I am writing a file uploading utility in python that uses the walk() function to parse a directory, finding any file under that directory, and upload it to a remote server using the...
0
by: john_phx | last post by:
I'm trying to increase the performance of a program that concatenates binary file parts into a single file. Each of the parts is contained in a binary file. The existing app simply takes the first...
0
by: Shmurthy | last post by:
I have a binary file. How do i search for a string (pattern) and replace dynamically with new data. Thank Shmurthy
5
by: maxwell | last post by:
I used to program in matlab An read an image file with : fread(a,,0) which gave me a matrix of 256*256 full of numbers which were the pidxel values from 0 to 256. How do i do the same in c++ ...
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
4
by: chinthamani | last post by:
Hai, I want to read from a particular point of the file not from the beginning of the file. It may be from the second line or the third or from any position, so how is it possible for me to reach...
6
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char...
4
by: =?ISO-8859-1?Q?Hans_M=FCller?= | last post by:
Good morning folks, I cannot read a binary file into a mysql database. Everything I tried did not succeed. What I tried (found from various google lookups...) is this: con =...
1
by: tom thomas | last post by:
Dear sir, i am very new to c# and i request you to... please look into the problem The following is the code(please see the code below:) which i uses to open the binary file,currently it works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.