Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 5:27am
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
phil.o at 16-Jan-13 11:46am
   
You expressed that better than me. My 5.
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 _Amy 230
2 OriginalGriff 200
3 Manfred R. Bihy 200
4 Peter Leow 180
0 OriginalGriff 7,495
1 Sergey Alexandrovich Kryukov 6,402
2 Maciej Los 3,849
3 Peter Leow 3,568
4 CHill60 2,702


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100