Click here to Skip to main content
12,813,994 members (33,746 online)
Rate this:
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";
                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?

Posted 16-Jan-13 6:27am
Rate this: bad
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
phil.o 16-Jan-13 11:46am
You expressed that better than me. My 5.
Rate this: bad
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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2017
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