c# - Why AND two numbers to get a Boolean? -


I am working on a small hardware interface project based on Wellen's 8055 board.

The example code comes in VB.Net and I am writing it again in C #, to get the opportunity to move through most codes and to understand it.

One thing has astounded me:

They read all the digital inputs in one step and then based on the answer to the reading digital inputs (which come back into an integer) Set a checkbox and then they and this with a number:

  I = readelli digital (1). Checked = (i and 1) CBI (2). Checked = (i and 2) \ 2 cbi (3). Check = (i and 4) \ 4 cbi (4). Check = (I and 8) \ 8 CBI (5). Check = (i and 16) \ 16  

I have not done digital systems in a while and I think what they want to do, but what effect it has and two numbers Are you? Is not everything above all equal to true?

How do you translate it into C #?

I think you have to translate it:

  I & amp; 1 == 1 I & amp; 2 == 2 Eye & amp; 4 == 4  

etc ... This is using bitwire and operator.

When you use a bitword and operator, this operator will compare the binary representation, return two values, and return a binary value where only those bits are set, which are also set in two operands Are there. For example, when you do this:

2 & amp; ; 2

This will do the following:

  0010 & amp; 0010  

and the result will be:

  0010 0010 & amp; ---- 0010  

Then if you compare this result to 2 (0010), it will really return the truth.


Comments