472,365 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

bits operation

I would like to know how I can extract some data from a byte array.
For example let's say I have a byte array with 4 elements ie 32 bits.
BYTE buf[4];

I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ?
Is Bitset appropriate knowing that in reality my array is 29 bytes long
and I want to extract 1-32 bits from a defined offset.
Jul 23 '05 #1
3 1569
"Vince" <vs**@caramail.com> wrote in message
news:42***********************@news.free.fr...
I would like to know how I can extract some data from a byte array.
For example let's say I have a byte array with 4 elements ie 32 bits.

BYTE buf[4];

I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ? With something like: ((buf[0]&7)<<1) | ((buf[0]>>7)&1)
(but it depends how you store the bits into the byte array).
Is Bitset appropriate knowing that in reality my array is 29 bytes long
and I want to extract 1-32 bits from a defined offset.

Yes, on a 32-bit platform, with bitset you should be able to easily
extract bits using something like:
(myBitSet>>bitOffset).to_ulong() & ((1UL<<numBits)-1);
(NB: you need to special-case the case where numBits is 32!)
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #2
raj
> BYTE buf[4];

I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ?


You could do that with ">>" , "<<" operators

Raj
Jul 23 '05 #3
Vince wrote:

I would like to know how I can extract some data from a byte array.
For example let's say I have a byte array with 4 elements ie 32 bits.
BYTE buf[4];

I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ?
Is Bitset appropriate knowing that in reality my array is 29 bytes long
and I want to extract 1-32 bits from a defined offset.

You will use two bitsets where you will assign buf[0] and buf[1] and get
the individual bits you want.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

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

Similar topics

25
by: Sean Berry | last post by:
Say I have a dictionary like the following {1:'one',2:'two',4:'three',8:'four',16:'five', etc...} and I am given some numbers, say 22, 25, and 9. I want to determine the keys, powers of 2,...
3
by: Julia Donawald | last post by:
Hi, I have the following problem, and till now sadly I couldnt find an algorithm which does the job well. My problem is as follows: Given for example the following bit-value ( saved as unsigned...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
29
by: John Devereux | last post by:
Hi, What is the best way to ensure an integer is a multiple of a given power of 2? How about int size; ...
15
by: thomas.mertes | last post by:
For a hash function I want to reinterpret the bits of a float expression as unsigned long. The normal cast (unsigned long) float_expression truncates the float to an (unsigned long) integer. ...
5
by: Oyvind Eriksen | last post by:
Hello. I need to read bits from bytes in a file. I have code that works but it's very slow. Can anybody help me? The code I have is: private bool GetBit(byte b, int pos) { return ((b &...
16
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language"...
10
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
2
by: O.B. | last post by:
I've written a small operation that sets/unsets bits within an unsigned integer. Is there a better way to do this? static uint setBits(uint original, uint newValue, uint offset, unint mask) {...
19
by: sarahh | last post by:
Hi, I need help in the following question . I have a cpu that knows to do the computations on 32 bits(unsigned integer( write a function that gets 2 64 bits numbers and return their sum. I start...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.