ADVERTISEMENT
ADVERTISEMENT

VB.Net program to overload bitwise left shift (<<) operator

Here, we are going to learn how to overload bitwise left shift (<<) operator in VB.Net?
Submitted by Nidhi, on January 13, 2021

Here, we will overload the left shift (<<) operator using the shared operator method, which is used to perform a bitwise left shift operation on the object of the class.

Program/Source Code:

The source code to overload the left shift (<<) operator is given below. The given program is compiled and executed successfully.

'VB.net program to overload left shift "<<" operator. 

Class Sample
    Dim num As Integer
    Sub SetValue(ByVal n As Integer)
        num = n
    End Sub

    Public Shared Operator <<(ByVal S1 As Sample, ByVal bits As Integer) As Integer
        Dim result As Integer = 0
        result = S1.num << bits
        Return result
    End Operator
End Class

Module Module1
    Sub Main()
        Dim obj As New Sample()
        Dim result As Integer = 0

        obj.SetValue(10)

        'left shift by 2 bits
        result = obj << 2

        Console.WriteLine("Result after left shift is: {0}", result)
    End Sub
End Module

Output:

Result after left shift is: 40
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains two methods SetValue() and operator method to overloading the bitwise left shift (<<) operator.

After that, we created a module Module1 that contains the Main() method, the Main() method is the entry point for the program. And, we created an object of Sample class and set the value of data member using SetValue() method and then perform bitwise left shift operator on objects. After that print the result on the console screen.

VB.Net Basic Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.