473,387 Members | 1,348 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,387 software developers and data experts.

bit insertion and extraction

Hi,

I would like to extract or insert some values (of different size) inside
a buffer.
I would like to have something like this :
BYTE Buffer[207];
CBitAPI bitbuf<Buffer, sizeof(Buffer)>;

// Extract different data
int version = bitbuf.get(0,4); // Extract 4 bits from bit 0
int whatever = bitbuf.get(4,2); // Extract 2 bits from bit 4
bool ack = bitbuf.get(6,1) //Extract 1 bit from bit 6

or even better :

int nValue;
bitbuf.get(4,2, nValue);

DWORD dwValue;
bitbuf.get(6,4, dwValue);
Unfortunately I searched everywhere (boost, google, ) and I didn't found
anything appropriate.

dynamic_bitset for instance cannot be initialized from an array...
Jul 23 '05 #1
5 4373

"Vince" <vs**@caramail.com> wrote in message
news:42***********************@news.free.fr...
Hi,

I would like to extract or insert some values (of different size) inside
a buffer.
I would like to have something like this :
BYTE Buffer[207];
CBitAPI bitbuf<Buffer, sizeof(Buffer)>;

// Extract different data
int version = bitbuf.get(0,4); // Extract 4 bits from bit 0
int whatever = bitbuf.get(4,2); // Extract 2 bits from bit 4
bool ack = bitbuf.get(6,1) //Extract 1 bit from bit 6

or even better :

int nValue;
bitbuf.get(4,2, nValue);

DWORD dwValue;
bitbuf.get(6,4, dwValue);
Unfortunately I searched everywhere (boost, google, ) and I didn't found
anything appropriate.

dynamic_bitset for instance cannot be initialized from an array...


lets say you have a bit array A and you want to extract the nth bit.
Normally A would be stored as an array of bytes or ints.
|xxxxxxxx|xxxxxxxx|xxxxxxxx|xxx@xxxx|xxxxxxxx|xxxx xxxx|

lets say we want the bit at the @. First we have to figure out which byte
this is in... its pretty easy, you take the location of @ which is n(it is
28 in this example) and divide by 8 and that gives you what "bin" you are
in(i.e., what byte @ belongs to)

to find out what how "far" into the byte @ is in, we just have to compute
n - 8*floor(n/8).

8*floor(n/8) represents what the location of the byte that @ is in in terms
of bits.

n - 8*floor(n/8) is the same as n mod 8.

so, the simple formula to extract the byte that contains the bit is
(floor(n/8) means n / 8 in integer division)

OurByte = BitArray[floor(n/8)];

Now to get the bit we simply have to do this

((1 << (n % 8)) & OurByte) >> (n % 8)

You should learn bitwise arithmetic to understand that. I'm sure there are
many tutorials online

note that

1 << (n % 8) has a 1 in the position of the location of @ and 0's everywhere
else

so 1 << (28 % 8) = 1 << 4 = 00010000 in the case above (note the 1 is at the
4th bit (this is what (1 << x) means... means we put a 1 at the xth bit)

00010000 & xxx@xxxx = 000@0000

and the << shifts the bit back to the first location..

000@0000 >> (28 % 8) = 000@0000 >> 4 = @0000000 = @
Now, I'm sure you can find some stuff online that can make it clearer if you
don't understand it.

Two things to note: You might want to use ints instead of bytes... I think
you should be ablet o figure out the obvious generalization. Second is that
this has to be modified on different architectures because of the way
certain data types or stored(look for endianess)

Hope that helps some.

Jon
Jul 23 '05 #2
Jon Slaughter wrote:
Now to get the bit we simply have to do this

((1 << (n % 8)) & OurByte) >> (n % 8)


Not to mention that it's worth taking a look at the endianness of the
system.
Jul 23 '05 #3

"Sensei" <se******@tin.it> wrote in message
news:d9**********@news.doit.wisc.edu...
Jon Slaughter wrote:
Now to get the bit we simply have to do this

((1 << (n % 8)) & OurByte) >> (n % 8)


Not to mention that it's worth taking a look at the endianness of the
system.


heh... I did mention that...
Jul 23 '05 #4
Jon Slaughter wrote:
heh... I did mention that...

Really!? I should wear eyeglasses...... :P
Jul 23 '05 #5

"Vince" <vs**@caramail.com> wrote in message
news:42***********************@news.free.fr...
Hi,

I would like to extract or insert some values (of different size) inside
a buffer.
I would like to have something like this :
BYTE Buffer[207];
CBitAPI bitbuf<Buffer, sizeof(Buffer)>;

// Extract different data
int version = bitbuf.get(0,4); // Extract 4 bits from bit 0
int whatever = bitbuf.get(4,2); // Extract 2 bits from bit 4
bool ack = bitbuf.get(6,1) //Extract 1 bit from bit 6

or even better :

int nValue;
bitbuf.get(4,2, nValue);

DWORD dwValue;
bitbuf.get(6,4, dwValue);
Unfortunately I searched everywhere (boost, google, ) and I didn't found
anything appropriate.

dynamic_bitset for instance cannot be initialized from an array...


It can according to:
http://www.boost.org/libs/dynamic_bi...l#header-files

You would use the constructor that takes two input iterators. Since this is
a templated constructor, Buffer elements don't have to be 8bit bytes.

boost::dynamic_bitset<> bits( &Buffer[0], &Buffer[0] + 207 );

Also see the non-member functions from_block_range,to_block_range and
to_ulong that allow transfer from/to integral data types. The latter could
be used to create a templated function to implement your desired behavior.

// Untested and needs error checking

template< typename T, class BITSET >
T extract_value( BITSET aBits, size_t aBegin, size_t aSize )
{
// implement size assertions here

aBits.resize( aBegin + aSize );

return T(aBits<<aBegin).to_ulong());
}
Jeff Flinn


Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kangol kangoll | last post by:
hi, i've been trying to figure out how to make visual basic start extracting text at a certain line to when i tell it to stop. I am making trying to make a program where it extracts text from a...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
2
by: Jason Huang | last post by:
Hi, Would someone show me how to do the data extraction to Excel in ASP.Net using C# web form? I am not familiar with VB, so I am asking someone to help me out! Any help will be appreciated. ...
1
by: James Lehman | last post by:
Hello. I want to write a program that reads AutoCAD shape (font) files. They are written with the convention that hexadecimal values have a leading zero and decimal values do not. All numbers...
4
by: Andrix | last post by:
Hi, I have a table with 20.000.000 of tuples. I have been monitoring the performance of the insertion and updates, but not convince me at all. The table have 30 columns, what and 12 of it, are...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
0
by: polocar | last post by:
Hi, I have noticed a strange behaviour of CurrencyManager objects in C# (I use Visual Studio 2005 Professional Edition). Suppose that you have a SQL Server database with 2 tables called "Cities"...
3
by: dec01louis | last post by:
Hi all, actually i'm now doing something on license plate recognition system for my project. The first step would be the license plate extraction algorithm which means it is needed to extract a...
9
by: python_newbie | last post by:
I don't know this list is the right place for newbie questions. I try to implement insertion sort in pyhton. At first code there is no problem. But the second one ( i code it in the same pattern i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.