473,396 Members | 2,039 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,396 software developers and data experts.

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 2088
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
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...
31
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...
23
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...
1
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...
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;
7
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...
51
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...
26
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 ...
1
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>...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...
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.