The Wayback Machine - https://web.archive.org/web/20090214005406/http://www.codeguru.com:80/cpp/cpp/cpp_mfc/bits/article.php/c4087/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> C++ >> C++ & MFC >> Bits and Bytes


Representing Arrays as Streams of Bits
Rating:

Alexander Hritonenkov (view profile)
March 31, 2003

Environment: MFC

I wrote this class when I needed to extract separate bits of data from an array, one by one. After some calculations, I found I needed to put modified bits in another array of data. I think this class may be useful, so here it is.


(continued)



This class operates with data located in the CByteArray object. So, the common steps to use CBitStream are:

  1. Declare a CByteArray object.
  2. Declare a CBitStream object, supplying a reference to the newly created CByteArray object to the constructor of the first one.
  3. Enjoy. :)

To set/get the next bit, you can use either overloaded shifting operators or SetBit()/GetBit() functions. Also, you can navigate inside the array of data. You can set a "pointer" to any bit inside your array.

Also, you can choose the order in which bits of every byte will be processed. The default is a "left-to-right" order. In this order, the bits affected are: 7th, 6th, 5th, 4th, 3rd, 2nd, 1st, and then the 0 bit. You can define the RIGHTTOLEFTFILL identifier to change the order to right-to-left, in which bits are accessed: 0, 1st, 2nd, 3rd, 4th, 5th, 6th, and then 7th. I prefer the left-to-right order because it is easier to understand. Look to the picture below. a) is the left-to-right order (default) and b) is the right-to-left order.

CByteArray array;
CBitStream stream(array);

stream<<0<<1<<0<<0<<1<<0<<0<<1;
// The first byte now consists of 01001001 bits, so it equals 73
// (0x49). It's true when you are using the left-to-right order.
stream.SetBit(0);
stream.SetBit(1);
stream.SetBit(0);
stream.SetBit(0);
stream.SetBit(1);
stream.SetBit(0);
stream.SetBit(0);
stream.SetBit(1);
// The second byte is now the same as the first.

Downloads

Download demo project - 10 Kb
Download source - 3 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Good - Legacy CodeGuru (04/01/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers