473,655 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help getting values from bits.

Hi,
First, thanks for any time you spend helping me, I'm at a loss. I'm not
bit-savvy, so I apologize if this is extremely simple, or I am going about
this the wrong way.

I am trying to take a byte array and extract some information from that
array, and convert it back to a hex value. For instance, I have a byte[]
that I populated from a BinaryReader of a file. Now, lets say it's 4 bytes
in length. Out of those 32 bits I need to get the hex value of certain
bits, like 3 bits starting at offset 10. The header information I am
reading from the file is not byte aligned, so most of the values I am going
to be looking at will be two to eight bits spanning different bytes.

I've been able to move the data into a BitArray, but from that point I'm at
a loss as to what to do with the BitArray, or if that is even the right
direction to go.

Any help in the right direction would be greatly appreciated.

Thank you!
Nov 29 '06 #1
2 2100
KH
Hey Pete,

You probably need to use bitmasking - in the C languages the bitwise
operators & (AND), | (OR), ~ (NOT), and ^ (XOR). See C# docs and
http://en.wikipedia.org/wiki/Bitwise_operators

If you have only 3 variables I'd be inclined to declare 3 boolean variables
and set them by masking the appropriate byte (untested):

byte[] bytes = { 1, 2 };

bool a = (bytes[0] & 0xFF) == 1; // TRUE because the first bit of bytes[0]
is ON
bool b = (bytes[0] & 0xFF) == 2; // FALSE because the 2nd bit of bytes[0] is
OFF
bool c = (bytes[1] & 0xFF) == 2; // TRUE because the 2nd bit of bytes[1] is ON
// Use the bool variables now...

If there's a bunch of significant bytes you might just test each one as you
go...

if ((bytes[1] & 0xFF) == 2) // do something

.... or maybe each byte represents only one condition? ...

if ((bytes[0] & 0xFF) 0) // do something

HTH ... - KH
"Pete" wrote:
Hi,
First, thanks for any time you spend helping me, I'm at a loss. I'm not
bit-savvy, so I apologize if this is extremely simple, or I am going about
this the wrong way.

I am trying to take a byte array and extract some information from that
array, and convert it back to a hex value. For instance, I have a byte[]
that I populated from a BinaryReader of a file. Now, lets say it's 4 bytes
in length. Out of those 32 bits I need to get the hex value of certain
bits, like 3 bits starting at offset 10. The header information I am
reading from the file is not byte aligned, so most of the values I am going
to be looking at will be two to eight bits spanning different bytes.

I've been able to move the data into a BitArray, but from that point I'm at
a loss as to what to do with the BitArray, or if that is even the right
direction to go.

Any help in the right direction would be greatly appreciated.

Thank you!
Nov 29 '06 #2
&FF for a bit test doesn't look right - this will just become:
bool a = bytes[0] == 1;
which isn't what was asked.

I don't know if there are any handy methods of enumerating bits (as bytes)
without byte alignment - it does make things a bit scrappy (you'd have to
use masks everywhere) - however in general the approach would be to create a
bit mask of the bits you want, and test ((value & mask) == mask) for "all
bits in mask", and ((value & mask) != 0) for "any bit from mask". Note you
can do this for all of the integral types, so you can work (for instance) on
an Int32 rather than 4 bytes. For your specific question the mask would be
3584 or 7158 depending on whether you mean 9 zeros or 10 zeros(whether the
offset is 1-based or 0-based).

There is also a BitVector32 structure, that (from an int) allows you to use
an indexer to look at specific bits - not sure if it is worth the effort
though.
If you do use multi-byte operations (Int32 etc), be sure to understand your
endianness; if you are happy to use the system default endianness you can
use BitConverter (e.g. ToInt32, ToInt64 etc) - but you should probably check
the data source. This might mean building your objects manually, else Jon
has an endian converter in his toolbox:
http://www.yoda.arachsys.com/csharp/miscutil/

Does that help any?
Nov 29 '06 #3

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

Similar topics

10
2622
by: Raghavendra Mahuli | last post by:
i need to store a lot of integers(say 10,000) in a datastructure. The constraints are: 1. It should be fast. 2. It should be orderded or sorted. 3.Insterting and deleting should be fast. 4. The no. of elements can change at runtime and hence it should be dynamic. Right now i am using SortedVector , but it is very slow. So, can u pls suggest a suitable Datastructure.
31
2626
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant but it appears to work. using time, i measured it takes .041s to execute, which i admit isnt much. but, the problem is that this procedure will be called many, many times in my project (probably at least a few thousand times, if not more) so...
23
2585
by: Adam | last post by:
I am coding a microkernel based off of Tanebaum's theroy. For Isis to be extensible, fast, and secure, it has been decided it will be a microkernel. Not in the old Mach sense of the word, but in the size of the entire project. It is a microkernel, that will limit interprocess communication. But as Tanenbaum says, you learn nothing by reading theroy alone. The code should be self-explaintory. Another reason I choose the microkernel method...
1
1209
by: CptDondo | last post by:
I've been struggling with this concept for a while, and I'm getting a bit burned out... I've got a piece of equipment that has data that I need to get. The data is stored in "bytes" for lack of a better word, and I need to parse it out according to an XML file format. Here goes:
10
10688
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;
7
3607
by: junky_fellow | last post by:
Guys, Does the standard allow to an implementation to chose any maximum and minimum value for an object of type int ? For eg. Can an implementation with sizeof(int)=4, chose to have value of INT_MAX = 2147483646 (in limits.h). Or it is mandatory that if sizeof(int)=4, then the INT_MAX should have
51
2581
by: cool_ratikagupta | last post by:
hello friends i ha just started learning c can u all give me the tips to make myself strong in c lanuage . as i want to be the best in watever i do . so just a request from all of u here plz help me out in improving my c .plz guide me as a techer . to do my best in c lanuage . i m clear with the concepts i just want to improve in c . as programming is best when u use less memory space n less ilterations.
26
266
by: Adem24 | last post by:
I need a good and fast random number generator (RNG), and a linear congruential generator (LCG), both with a max period >= 31 bits; the bigger the better. Additional requirements: - Must use integer-values only (32 or 64 bit), no floating point. - The RNG should have passed some statistical tests. - The "RAND_MAX" of these generators should equal the period. - The LCG should of course generate each number only once in a period.
1
4386
by: duane.lortie | last post by:
I'm writing a routine that fetches XML and attempts to parse some values from it. A condensed entry node of the XML looks like this .. <entry> <title>Some title</title> <author> <name>Author</name> </author> <source:resource url="http://somesite.com?tid=123456"/>
0
8380
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
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
8710
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...
0
7310
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...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
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
4150
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...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.