Question

I have never used Bitewise AND in my life. I have researched this operator but it still eludes me as to what it exactly does. So, I will ask with some code I just came across, what is the Bitwise And doing here:

CASE
               WHEN (ft.Receiver_Status & 2) = 2 THEN '3D'
               WHEN (ft.Receiver_Status & 1) = 1 THEN '2D'
               WHEN (ft.Receiver_Status & 32) = 32 THEN 'Invalid' -- AR 220312
               ELSE 'None'

Is it enforcing the same datatype, such as smallint converts to int before comparing the value of Receiver_Status?

Was it helpful?

Solution

ft.Receiver_Status & 1: 1 is 20, so it is pulling out the value of the bit at position 0.

ft.Receiver_Status & 2: 2 is 21, so it is pulling out the value of the bit at position 1.

ft.Receiver_Status & 32: 32 is 25, so it is pulling out the value of the bit at position 5.

Note that, for example, the = 32 in (ft.Receiver_Status & 32) = 32 is actually redundant. This could instead be (ft.Receiver_Status & 32) != 0 because all you're interested in is whether that bit is a 0 or a 1.

OTHER TIPS

The bitwise AND checks to see whether a particular bit is set. It appears ft.Receiver_Status is an integer which stores various flags in different bits.

  • 1 in binary is 00001 so ft.Receiver_Status & 1 is checking to see if the first bit is set.
  • 2 in binary is 00020 so ft.Receiver_Status & 1 is checking to see if the second bit is set.
  • 32 in binary is 10000 so ft.Receiver_Status & 32 is checking to see if the fifth bit is set.

To see precisely how this works, the result of the AND operation will be the bit at position n will be 1 f and only if the bit at position n in both the first and the second number is 1. Consider the following binary numbers:

011010001 (209)
000010000 ( 32)
---------------
000010000 ( 32)

And alternatively,

011001001 (201)
000010000 ( 32)
---------------
000000000 (  0)

(something & constant) == constant (where constant is a power of two) is a way of ensuring that the bit defined in constant is set. Consider your first case. All of the bits in 2 aren't set except for the second bit, so we know the rest will be zero. If the second bit is not set in Receiver_Status then the result will be zero, if it is set, that bit will be one and the result will be two, the same as the bit mask.

It could also be written as (ft.Receiver_Status & 2) > 0 to avoid repeating the bit mask in each case.

You should read about bit flags. That's the way to check, if particular bit within a bigger data type (e.g. byte) is set to 1 or not.

Example:

Consider having a bite with following bits content: 00110101. You'd like to check the fifth position. You need to change all other bits to 0 and check, if that one is 1 or 0. To do that, perform bitewise AND with 2^4:

00110101
00010000 &
--------
00010000

To give a concrete example with all these other great answers, if Receiver_Flags were 3, the 1 and 2 bits are on. Likewise, if it were 34, the 2 and 32 bits are on.

A lot of times an enum is used to set these fields. Consider this enum:

public enum Flags
{
    ThreeD = 1,
    TwoD = 2,
    Invalid = 3
}

You might set the value like this:

Receiver_Flags = Flags.ThreeD | Flags.TwoD

and the value would be 3. In that case the 1 and 2 bits would be on.

This is very similar to Enum.HasFlag. Here's one way to implement that for a Test enum:

static bool HasFlag(Test flags, Test flag)
{
    return (flags & flag) != 0;
}

Basically, (ft.Receiver_Status & 32) = 32 checks if the fifth bit is 1 or 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top