• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Read/Write one BIT at a time??

## Recommended Posts

n0ob    176
Is there a freely available or standard library which allows the reading/writing of single bits at a time? I mean, if there isn''t, I could probably force myself to write my own system to do that. Like, store bits in a buffer, then flush a byte at a time.. which I actually know how to do ^_^. But it would be easier if there''s one out there already. Thanks! Kings of Chaos

##### Share on other sites
ApochPiQ    23011
A byte is the smallest addressable piece of data on a computer. You cannot access data bit by bit. However, you can use bitmasks to mask out all the bits but the one you are interested in; use 2^(bit index) as the mask and perform an AND between this mask and your byte. The final value will clear all bits but the one you are after, and that bit will be 1 or 0 depending on its value in the original byte.

For example, if I have the byte 10110110, and mask it with the bitmask 00010000, the bitwise AND (& operator in C/C++/Java) will clear out all the bits and leave me with 00010000. I can then test this value; if it is 0, the original bit was 0. If the value is not zero, then the original bit was 1.

Bitmasking is a trivial programming concept and so you are not very likely to find any premade code.

##### Share on other sites
Wyrframe    2426
It''s a dead-simple thing to do. Best to do it yourself.

##### Share on other sites
Flarelocke    410
Either std::vector or std::bitset

##### Share on other sites
foofightr    130
Note that reading/writing bits (especially writing) is considerably slower than bytes, as counter-intuitive as that may sound.

##### Share on other sites
antareus    576
I wrote a Bitstream at work, however I cannot share it.

It was pretty easy, but testing it for correctness isn't the easiest thing in the world.

[edited by - antareus on August 14, 2003 7:25:39 AM]

##### Share on other sites
BeanDog    1065
You could just use bitfields. I''m not sure of the exact syntax, but it''s something like this:

struct S{    unsigned b1 : 1;    unsigned b2 : 1;    unsigned b3 : 1;    unsigned b4 : 1;    unsigned b5 : 1;    unsigned b6 : 1;    unsigned b7 : 1;    unsigned b8 : 1;};int main(){    S s;    s.b1 = 1;    s.b2 = 0;    //etc.  s takes up only one byte.}

Or you could use the following to work on int''s or DWORD''s:

#define BITMASK(n) (1<<n)#define ISBITSET(v,n) ((v) & BITMASK(n))#define SETBIT(v,n) ((v) |= BITMASK(n))#define UNSETBIT(v,n) ((v)=~(SETBIT(~(v),n)))#define CLEARBITS(v) ((v)=0)int main(){    unsigned int n;    CLEARBITS(n);    SETBIT(n,1);    UNSETBIT(n,1);    //etc.}

All source is written off the top of my head and is not guaranteed to be perfect. Please post corrections :-)

~BenDilts( void );

##### Share on other sites
n0ob    176
yes.. thanks for the help. I guess I will write my own, USING BITMASKS!! or whatever

Kings of Chaos

##### Share on other sites
HFX    122
May I inquire as to why you want to write a bit at a time. Writing a bit at a time will be a lot slower then using a whole byte or even an int. So unless your trying to run in something like an embedded device with very little storage, writing bit by bit is not recommended. Infact it is a detrement. It would probably be better read a whole byte at a time even if you do waste.

##### Share on other sites
n0ob    176
I am aware of the pros/cons of this.. I need it because I''m lazy. I''m just doing an experiment, and could care less if the write operation for a single bit took 50 ms. I KNOW already. I asked if there was one, i didn''t ask for you to convince me to not use it. Thanks for your help, I need no more.

Kings of Chaos