Bit Shift Operator in VB.NET

How to bitwise shift in VB.NET?

Explanation

Bit Shift Operator in VB.NET

Bit Shift Operators are used to do arithmetic shifting of bits either to the right or to the left.
Following table lists the BitShift Operators used in Visual Basic.
Operator Description
<< Left shift operator
>> Right shift operator
Example:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = a >> b
MsgBox("Integer Value after RIGHT SHIFT is::>" & c,
MsgBoxStyle.OkCancel)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
Dim x As Integer
Dim y As Integer
Dim z As Integer
x = Val(TextBox1.Text)
y = Val(TextBox2.Text)
z = x << y
MsgBox("Integer Value after LEFT SHIFT is::>" & z,
MsgBoxStyle.OkCancel)
End Sub End Class
Description:

In the above example, TextBox1 is used to get integer to shift bits and TextBox2 is used to get the number of places to shift. Using the click event of two operator buttons Button1 and Button2 the bits are shifted either to left or to right. The integer value returned after shifting bits are displayed in a Message Box.

Visual Basic Tutorial


Ask Questions

Ask Question