Method equivalent to BigInteger.and () in C #

9

I need to convert the java code below to C #:

public static boolean verificaPermissao(BigInteger perm1, BigInteger perm) {

    if (perm1 == null || perm == null || (perm1.equals(BigInteger.ZERO) || (perm.equals(BigInteger.ZERO))))
        return false;

    return !perm.and(perm1).equals(BigInteger.ZERO);
}

But I do not know what the function in C # is for and

public static bool verificaPermissao(BigInteger perm1, BigInteger perm)
{
    if (perm1 == null || perm == null || (perm1.IsZero) || (perm.IsZero ))
        return false;

    //Converter para C#
    //return !perm.and(perm1).equals(BigInteger.ZERO);Converter para C#
}

The and method does not exist in BigInteger of C #.

    
asked by anonymous 04.05.2017 / 19:15

2 answers

8

and is the same as using the & ( bitwise and ) between the two values and this is the same in both languages.

I made other adaptations as well. In C #, BigInteger is a type by value, that is, it will never be null and the check of zero is already done in return .

public static bool VerificaPermissao(BigInteger perm1, BigInteger perm) 
{        
    return (perm & perm1) != 0;
}
    
04.05.2017 / 19:20
7

If you want the bitwise has the operator . If you want the Boolean, it does not even have to have the operation regardless of whether it is int , BigInteger or something else.

using static System.Console;
using System.Numerics;

public class Program {
    public static void Main() {
        WriteLine(VerificaPermissao((BigInteger)1, (BigInteger)0));
        WriteLine(VerificaPermissao((BigInteger)1, (BigInteger)1));
        WriteLine(VerificaPermissao((BigInteger)2, (BigInteger)1));
    }
    public static bool VerificaPermissao(BigInteger perm1, BigInteger perm) {
        return (perm & perm1) != 0;
    }
}

See working on .NET Fiddle . And no ideone . Also I put it in GitHub for future reference .

I have taken advantage of and simplified since BigInteger in C # is one type per value and has operators for all basic math operations. This code is more idiomatic in C #.

Whoever wants to compare that the result is the same in Java .

    
04.05.2017 / 19:22