473,385 Members | 1,445 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,385 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 1627
"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.