Giter VIP home page Giter VIP logo

bits's Introduction

bits

bits is a tiny C++ bitstring library. It allows reading and writing numerical values of defined bit size, at a defined bit offset in a memory buffer. There are no restrictions imposed on the bit size or the bit offset, making it possible to write values without paying attention to byte boundaries.

Bit operations assume values are stored with the MSB(most significant bit) first.

Say what?

Suppose you have a 2-byte buffer, and you need to retrieve/update a 4-bit value located in bits 7,8 of the first byte and bits 1,2 of the second.

buffer: 00101100 11001010
bits:         ^^ ^^        (offset==6, bit_size==4)

In this simple case the task may be accomplished by a combination of bit operations (<< >> &), but as bit_size increases it becomes slightly more complex and unreadable. bits hides all the bitwise operations:

int value = bits::getbitbuffer<int> (buffer, 6, 4); // value == 3

The inverse operation is also possible:

bits::setbitbuffer (buffer, 6, 4, 5);
// before: 00101100 11001010
// bits:         ^^ ^^
// after:  00101101 01001010
assert ( bits::getbitbuffer<int> (buffer, 6, 4) == 5 );

In practice

Bits' primary use case is parsing and building binary protocol packets. A bits::bitstream class is included in the library to make this task cleaner and faster. The example snippet below parses an MPEGTS packet header:

unsigned char packetData[188];
bits::bitstream packetStream(packetData);

assert (packetStream.read<unsigned> (8) == 0x47);  
//read automatically moves an internal offset
bool TEI = packetStream.read<bool> (1);
bool PUSI = packetStream.read<bool> (1);
bool TP = packetStream.read<bool> (1);

unsigned PID = packetStream.read<unsigned> (13);

unsigned char payload[188 - packetStream.position()/8];
packetStream.readstring (payload, 188*8 - packetStream.position()];

A packet may be re-created in a very similar fashion:

packetStream.rewind();
packetStream.write(8, 0x47);
packetStream.write(1, TEI);
packetStream.write(1, PUSI);
packetStream.write(1, TP);
packetStream.write(13, PID);
packetStream.writestring (188*8 - packetStream.position(), payload);

Installing

bits uses the scons build system, so to build the library unpack the source and run:

$ scons

in the source folder. To install run:

$ scons install

to uninstall:

$ scons -c

That's it!

Thank you to: QuasarDonkey

bits's People

Contributors

wroniasty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.