473,766 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1653
"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>>bitO ffset).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
1874
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, that comprise the number. Ex. 22 = 16+4+2 25 = 16+8+1 9 = 8+1
3
3719
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 long ): 001101000001101010, so the unsigned long variable has the value: 53354. Furthermore I know the length of this bit-value ( here: 18 ). Now I want to delete the leading 8 bits of such a bit value, so that after the operation I get the...
53
8214
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 all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values.
29
1974
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
7003
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. But this is not the effect I want for a hash function. With unions my hash function can be implemented:
5
32303
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 & (byte)(1 << pos)) != 0); }
16
11280
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" states that "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE USING". Does this mean "float" variables have different sizes on different machines?
10
10709
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
3372
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) { return ((original >offset) & (~mask) | newValue) << offset; } Example:
19
2774
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 with the following structure. typedef struct{ unsigned int low; unsigned int high; }64bits;
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.