By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,681 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,681 IT Pros & Developers. It's quick & easy.

Need Help getting values from bits.

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
&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 discussion thread is closed

Replies have been disabled for this discussion.