對位模式執行數學左移位。
= <<
各部分說明
- result
- 必選項。整型數值。對該位模式進行移位的結果。數據類型與 pattern 的數據類型相同。
- pattern
- 必選項。整型數值表達式。要進行移位的位模式。數據類型必須爲 Byte、Short、Integer 或 Long。
- amount
- 必選項。數值表達式。要將該位模式移位的位數。數據類型必須爲 Integer 或擴展到 Integer。
備註
數學移位不是循環的,即不會將在結果的一端移出的數位從另一端重新移入。在數學左移位運算中,丟棄移出結果數據類型範圍的數位,而將右端空出的數位位置設置爲零。
爲防止移位的結果超出它所支持的位數,Visual Basic 使用與 pattern 的數據類型相對應的大小掩碼來屏蔽 amount 的值。可使用這些值的二進制與運算結果作爲移位量。大小掩碼如下所示:
pattern 的數據類型 | 大小掩碼(十進制) | 大小掩碼(十六進制) |
---|---|---|
Byte | 7 | &H00000007 |
Short | 15 | &H0000000F |
Integer | 31 | &H0000001F |
Long | 63 | &H0000003F |
如果 amount 爲零,則 result 的值與 pattern 的值相同。如果 amount 爲負值,則將把它作爲無符號的值,並使用相應的大小掩碼進行屏蔽。
數學移位絕不會產生溢出異常。
示例
此示例使用 << 運算符對整數值執行數學左移位。結果的數據類型始終與被移位的表達式相同。
Dim Pattern As Short = 192 ' Bit pattern is 0000 0000 1100 0000. Dim Result1, Result2, Result3, Result4, Result5 As Short Result1 = Pattern<<
0 ' Result is 192 (0000 0000 1100 0000). Result2 = Pattern<<
4 ' Result is 3072 (0000 1100 0000 0000). Result3 = Pattern<<
9 ' Result is -32768 (1000 0000 0000 0000). Result4 = Pattern<<
17 ' Result is 384 (0000 0001 1000 0000). Result5 = Pattern<<
-1 ' Result is 0 (shifted 15 places to left).
Result4
的移位量以 17 AND 15 計算,結果等於 1。
注意幾點:
1 數據類型,整型Byte Short Integer Long
2 大小掩碼,超過的要換算成掩碼範圍內的進行計算
3 數學移位不是循環的