简体   繁体   中英

Validating an if-condition with bitwise operators

I am facing a problem in dealing with basic if else statement in C#.

I have two variables of type uint. I need to check whether the first variable is in the range of the second one. Also, the first variable can have value null. The output comes up fine in case of i put some value for the first value. But the desired output fails in case I put null in the first variable

I did it in this manner.

using System;

public class Test
{
    public static void Main()
    {
        uint var1 = 0x00000000;
        uint var2 = 0x0ffffffa;
        if (!((var1 == null) || (((var1 != null) && (var1 & var2)) > 0)))
        Console.WriteLine ("no");
        else
        Console.WriteLine ("yes");
    }
}

The code when the output is coming as no is :

using System;

public class Test
{
    public static void Main()
    {
        uint var1 = 0x00000000;
        uint var2 = 0x0ffffffa;
        if (!(((var1 != null) && ((var1 & var2) > 0)) || (var1 == null)))
        Console.WriteLine ("no");
        else
        Console.WriteLine ("yes");
    }
}

But the output is coming as "no". Where am I going wrong?

I want a 'yes' as an output. I need to check for the first variable. If it is null then fine, if not, then whether it is in the range of the second variable, if it is, then also fine

I'm confused as to what the question is here. You've initialized the variables, so they can't be null, you've used an OR logical operand so the statement will short-circuit when it evaluates the first term and finds it to be true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM