VB.NETの場合はビット演算が簡単に使えます。
先ずSiftですが左Siftは<<、右Siftは>>です。
>10進7=2進0111これを左シフトすると2進1110=10進14は
Dim i As Integer = (7 << 1) 又は
Dim i As Integer = 7
i = i << 1 又は
i <<= 1
となります、2つシフトしたければ i<<2とします。
ある数のあるビットが立っているかいないか調べることをビットテストと言います。
ビットテストにはAnd演算子を使います。
これはテストしたいビットだけが立った数値とテストしたい数値のAndで実現します。
1100100 (10進 100)
And 0000100 (10進 4)
-------------------------
0000100
ANDの結果が0ならビットが立っていません。
コードは
Dim i As Integer = 100
Dim j As Integer = 2 '番目のビット
Dim k = i And 2 ^ j
If (i And 2 ^ j) > 0 Then
MessageBox.Show((j + 1).ToString + "番目のビットが立っています")
Else
MessageBox.Show((j + 1).ToString + "番目のビットが立っていません")
End If
i = 100
i = i And 123
MessageBox.Show(i.ToString())
強制的に1にするにはOrを使います
3ビット目を強制的に1にしています
1100000 (10進 96)
Or 0000100 (10進 4)
---------------------------
1100100 (10進 100)
i = 32
i = i Or 4
MessageBox.Show(i.ToString())
以上です
デリゲート:
大体そんな感じですね。
テキストボックス3つとぼたん3つを置いて、ボタンを押すと
ボタンと同じ番号のTextBoxに書き込むコードを示します。
これを応用してみてください。
Delegate Sub degaddText(ByVal textBoxNo As Integer, ByVal textBoxText As String)
Private Sub addText(ByVal textBoxNo As Integer, ByVal textBoxText As String)
Select Case textBoxNo
Case 1
TextBox1.Text += textBoxText
Case 2
TextBox2.Text += textBoxText
Case 3
TextBox2.Text += textBoxText
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim textBoxNo As Integer = 1
Dim textBoxText = "Button1"
Me.Invoke(New degaddText(AddressOf addText), New Object() {textBoxNo, textBoxText})
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
Dim textBoxNo As Integer = 2
Dim textBoxText = "Button2"
Me.Invoke(New degaddText(AddressOf addText), New Object() {textBoxNo, textBoxText})
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button3.Click
Dim textBoxNo As Integer = 3
Dim textBoxText = "Button3"
Me.Invoke(New degaddText(AddressOf addText), New Object() {textBoxNo, textBoxText})
End Sub