开发者

How does a logical & work on two bytes in C#?

I recently had a test question:

byte a,b,c;
a = 190;
b = 4;
c = (byte)(a & b);

What is the value of c?

I have never used a logical operand in this manner, what's going on here? Stepping through this, the answer is 4, but why?

Also, where would this come up in the real world? I would argue that using logical operands in this manner, with a cast, is just bad practice,开发者_如何学Python but I could be wrong.


You are doing a bitwise AND in this case, not a logical AND, it is combining the bits of the two values of a & b and giving you a result that has only the bits set that are both set in a & b, in this case, just the 4s place bit.

190 =    10111110
& 4 =    00000100
-------------------
= 4      00000100  

Edit: Interestingly, msdn itself makes the issue of whether to call it logical vs bitwise a bit muddy. On their description of the logical operators (& && | || etc) they say logical operators (bitwise and bool) but then on the description of & itself it indicates it performs a bitwise AND for integers and a logical AND for bools. It appears it is still considered a logical operator, but the action between integer types is a bitwise AND.

http://msdn.microsoft.com/en-us/library/sbf85k1c(v=vs.71).aspx


The logical AND operator, when applied to integers performs a bitwise AND operation. The result is 1 in each position in which a 1 appears in both of the operands.

  0011
& 0101
------
  0001  

The decimal value 190 is equivalent to binary 10111110. Decimal 4 is binary 00000100.

Do a logical AND operation on the bits like this:

  10111110
& 00000100
----------
  00000100

So the result is 4.

Also, where would this come up in the real world? I would argue that using logical operands in this manner, with a cast, is just bad practice, but I could be wrong.

These operations are useful in several circumstances. The most common is when using Enum values as flags.

[Flags]
public enum MyFileOptions
{
    None = 0,
    Read = 1,   // 2^0
    Write = 2,  // 2^1 
    Append = 4, // 2^2
}

If an Enum has values that are powers of two, then they can be combined into a single integer variable (with the Logical OR operator).

MyFileOptions openReadWrite = MyFileOptions.Read | MyFileOptions.Write;

In this variable, both bits are set, so it indicates that both the Read and Write options are selected.

The logical AND operator can be used to test values.

bool openForWriting = ((openReadWrite & MyFileOptions.Write) == MyFileOptions.Write);

NOTE

A lot of people are pointing out that this is actually a bitwise AND not a logical AND. I looked it up in the spec before I posted, and I was suprised to learn that both versions are referred to as "Logical AND" in the spec. This makes sense because it is performing the logical AND operation on each bit. So you are actually correct in the title of the question.


This is a bitwise AND, meaning the bits on both bytes are compared, and a 1 is returned if both bits are 1.

10111110 &
00000100
--------
00000100


One of the uses of a logical & on bytes is in networking and is called the Binary And Test. Basically, you logical and bytes by converting them to binary and doing a logical and on every bit.


In binary

4 == 100 190 == 10111110

& is and AND operation on the boolean operators, so it does de Binary and on 4 and 190 in byte format, so 10111110 AND 100 gives you 100, so the result is 4.


This is a bitwise AND, not a logical AND. Logical AND is a test of a condition ie:

if(a && b) doSomething();

A bitwise AND looks at the binary value of the two variables and combines them. Eg:

10101010 &
00001000
---------
00001000


This lines up the binary of the two numbers, like this:

10111110
00000100
--------
00000100

On each column, it checks to see if both numbers are 1. If it is, it will return 1 on the bottom. Otherwise, it will return 0 on the bottom.


I would argue that using logical operands in this manner, with a cast, is just bad practice, but I could be wrong.

There is no cast. & is defined on integers, and intended to be used this way. And just so everyone knows, this technically is a logical operator according to MSDN, which I find kind of crazy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜