Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: Dropping a bit in a bitwise operation #599

Closed
Thieum opened this issue Jul 29, 2020 · 2 comments
Closed

VB -> C#: Dropping a bit in a bitwise operation #599

Thieum opened this issue Jul 29, 2020 · 2 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@Thieum
Copy link

Thieum commented Jul 29, 2020

Input code

Module Module1


    <Flags()>
    Public Enum FillType
        All = &H1
        Latest = &H2
        Clean = &H4
        Recursive = &H8
        Append = &H10
        CleanAndLatest = Clean Or Latest
    End Enum
    Sub Main()
        Dim test1 = Test(FillType.All)
        Console.WriteLine("FillType.All -> " & test1)
        Dim test2 = Test(FillType.Latest)
        Console.WriteLine("FillType.Latest -> " & test2)
        Dim test3 = Test(FillType.Recursive)
        Console.WriteLine("FillType.Recursive -> " & test3)
        Dim test4 = Test(FillType.Append)
        Console.WriteLine("FillType.Append -> " & test4)
        Dim test5 = Test(FillType.CleanAndLatest)
        Console.WriteLine("FillType.CleanAndLatest -> " & test5)
        Dim test6 = Test(FillType.All Or FillType.Recursive)
        Console.WriteLine("FillType.All Or FillType.Recursive -> " & test6)

        Console.ReadLine()
    End Sub

    Function Test(options As FillType) As Object
        Dim opts As Object
        If (options And Not FillType.Recursive) = 0 Then
            opts = FillType.All
        Else
            opts = options And Not FillType.Recursive
        End If
        Return opts
    End Function

End Module

Erroneous output

using System;
using Microsoft.VisualBasic.CompilerServices;

namespace ConsoleApp4
{
    static class Module1
    {
        [Flags()]
        public enum FillType
        {
            All = 0x1,
            Latest = 0x2,
            Clean = 0x4,
            Recursive = 0x8,
            Append = 0x10,
            CleanAndLatest = Clean | Latest
        }

        public static void Main()
        {
            var test1 = Test(FillType.All);
            Console.WriteLine(Operators.ConcatenateObject("FillType.All -> ", test1));
            var test2 = Test(FillType.Latest);
            Console.WriteLine(Operators.ConcatenateObject("FillType.Latest -> ", test2));
            var test3 = Test(FillType.Recursive);
            Console.WriteLine(Operators.ConcatenateObject("FillType.Recursive -> ", test3));
            var test4 = Test(FillType.Append);
            Console.WriteLine(Operators.ConcatenateObject("FillType.Append -> ", test4));
            var test5 = Test(FillType.CleanAndLatest);
            Console.WriteLine(Operators.ConcatenateObject("FillType.CleanAndLatest -> ", test5));
            var test6 = Test(FillType.All | FillType.Recursive);
            Console.WriteLine(Operators.ConcatenateObject("FillType.All Or FillType.Recursive -> ", test6));
            Console.ReadLine();
        }

        public static object Test(FillType options)
        {
            object opts;
            if ((int)(options & !FillType.Recursive) == 0)
            {
                opts = FillType.All;
            }
            else
            {
                opts = options & !FillType.Recursive;
            }

            return opts;
        }
    }
}

Expected output

using System;
using Microsoft.VisualBasic.CompilerServices;

namespace ConsoleApp4
{
    static class Module1
    {
        [Flags()]
        public enum FillType
        {
            All = 0x1,
            Latest = 0x2,
            Clean = 0x4,
            Recursive = 0x8,
            Append = 0x10,
            CleanAndLatest = Clean | Latest
        }

        public static void Main()
        {
            var test1 = Test(FillType.All);
            Console.WriteLine(Operators.ConcatenateObject("FillType.All -> ", test1));
            var test2 = Test(FillType.Latest);
            Console.WriteLine(Operators.ConcatenateObject("FillType.Latest -> ", test2));
            var test3 = Test(FillType.Recursive);
            Console.WriteLine(Operators.ConcatenateObject("FillType.Recursive -> ", test3));
            var test4 = Test(FillType.Append);
            Console.WriteLine(Operators.ConcatenateObject("FillType.Append -> ", test4));
            var test5 = Test(FillType.CleanAndLatest);
            Console.WriteLine(Operators.ConcatenateObject("FillType.CleanAndLatest -> ", test5));
            var test6 = Test(FillType.All | FillType.Recursive);
            Console.WriteLine(Operators.ConcatenateObject("FillType.All Or FillType.Recursive -> ", test6));
            Console.ReadLine();
        }

        public static object Test(FillType options)
        {
            object opts;
            if ((int)(options & ~FillType.Recursive) == 0)
            {
                opts = FillType.All;
            }
            else
            {
                opts = options & ~FillType.Recursive;
            }

            return opts;
        }
    }
}

Details

  • VS Extension v81.6.0

Dropping a bit with bitwise operator in VB.NET is done with a combination of AND and NOT :

https://stackoverflow.com/a/38201868/444469

whereas in C# it's done with & and the complement operator ~ :

https://stackoverflow.com/a/4778181/444469

@Thieum Thieum added the VB -> C# Specific to VB -> C# conversion label Jul 29, 2020
@GrahamTheCoder
Copy link
Member

Thanks for the full example and explanation. Should be a straightforward adjustment to convert Not into ~ for numeric types

@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Aug 1, 2020

I believe this is fixed in e596227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants