12,813,994 members (33,746 online)
Rate this:
See more:
Hi All,

Bit of Bizzar question but I have some data that I have to logical '&' with a mask
to allow the data to be read off correctly. The issue is in the below code
```//Status (bit 4 Status) – if bit 4 = 0 then ‘False’ else ‘True’
if ((Hexvalue2 & 0x0008) == 0)
{
MessageBox.Show("hello bit 4 not set");
//checkBox4.Checked = false;
label10.Text = " Status-";
label9.Text += "False";
}
else
{
MessageBox.Show("bit 4 set");
//checkBox4.Checked = true;
label10.Text = "Status-";
label9.Text += "True";
}```

I am pretty sure this is correct as I am doing it right as I have done similar elsewhere with success which means the bit is set in the byte I am reading in. I now have a giant question mark over my head, I am testing properly am I not?

Glenn
Posted 16-Jan-13 6:27am

Rate this:

## Solution 1

Not quite.
First off, "&" is a binary AND not a logical AND - the latter is "&&".
Secondly, 0x0008 does not have bit 4 set - it has bit 3 set:
```Bit No  Hex   Decimal
0       01       1
1       02       2
2       04       4
3       08       8
4       10      16
5       20      32
6       40      64
7       80     128```
phil.o 16-Jan-13 11:46am

You expressed that better than me. My 5.
Rate this:

## Solution 2

Hi, it's quite hard to know what is going on here.
Could it be possible that your bit 4 is in fact the fifth (if you start counting at 0) ; in this case, bit 4 mask would be 0x0010 and not 0x0008.
But it's just a guess. Anyway, nothing seems wrong to me in your testing method.

Top Experts
Last 24hrsThis month
 Karthik Bangalore 390 Jochen Arndt 225 F-ES Sitecore 130 OriginalGriff 120 Garth J Lancaster 95
 OriginalGriff 4,367 Graeme_Grant 3,649 Karthik Bangalore 3,222 ppolymorphe 2,287 Jochen Arndt 2,216