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

checking status of individual bits in C#

P: n/a
I'm receiving a byte that contains the pass/fail test results of multiple tests. The first six bits are set. I need to read each of them separately. Is there an elegant way to do this?

Thanks in advance for any and all help.

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Daniel,

I would create a BitArray instance, and then access the bits you want
with the indexer, like so:

// Create the BitArray instance.
BitArray bitArray = new BitArray(new byte[1]{ byteValue });

// Access the values.
// First bit, will be boolean.
Console.WriteLine(bitArray[0]);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel Passwater via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:a2******************************@DotNetMonste r.com...
I'm receiving a byte that contains the pass/fail test results of multiple
tests. The first six bits are set. I need to read each of them separately.
Is there an elegant way to do this?

Thanks in advance for any and all help.

--
Message posted via http://www.dotnetmonster.com

Nov 16 '05 #2

P: n/a
Daniel Passwater via DotNetMonster.com <fo***@DotNetMonster.com> wrote:
I'm receiving a byte that contains the pass/fail test results of
multiple tests. The first six bits are set. I need to read each of
them separately. Is there an elegant way to do this?


Use a bitwise and:

if ((x & 1) != 0)
....

if ((x & 2) != 0)
....

if ((x & 4) != 0)
....

etc

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.