Click here to Skip to main content
15,883,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
//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

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.
 
Share this answer
 
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
 
Share this answer
 
Comments
phil.o 16-Jan-13 11:46am    
You expressed that better than me. My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900