### Bit Shift, And

Examples of Bit shifts
(1 << 0) is Bit.ShiftLeft(1,0)
(1 << 2) is Bit.ShiftLeft(1,2)
(1 << 0) OR (1 << 7) is Bit.Or(Bit.ShiftLeft(1,0),Bit.ShiftLeft(1,7))
Example Bit.ShiftLeft
'Example Int which is 4 bytes = 32 Bit
Dim i As Int = 6
Log("Binary 6 = " & Bit.ToBinaryString(i)) '= 110
'Shifting this Bit pattern To the left one position (6 << 1) results in number 12:
Log("Bit.ShiftLeft(i,1) = " & Bit.ShiftLeft(i,1))
Log("Binary 12 = " & Bit.ToBinaryString(12)) '= 1100
'The digits have shifted to the left by one position, and the last digit on the right is filled with a zero.
'Shifting left is equivalent to multiplication by powers of 2:
'So 6 << 1 is equivalent to 6 * 2 (is 12), and 6 << 3 is equivalent to 6 * 8 (is 48).
Result:
Binary 6 = 110
Bit.ShiftLeft(i,1) = 12
Binary 12 = 1100
Example Bit.AND
Log("Binary 1 = " & Bit.ToBinaryString(1))
Log("Binary 5 = " & Bit.ToBinaryString(5))
'The bits match only at the rightmost position. This is returned as 2^0, or 1.
Log("Bit.AND(1,5) = " & Bit.And(1,5))
Result:
Binary 1 = 1
Binary 5 = 101
Bit.AND(1,5) = 1
Log("Binary 13 = " & Bit.ToBinaryString(13))
Log("Binary 25 = " & Bit.ToBinaryString(25))
'The bits match at the rightmost position and at the position fourth from the right. This is returned as (2^0)+ (2^3), or 9.
Log("Bit.AND(13,25) = " & Bit.And(13,25))
Result:
Binary 13 = 1101
Binary 25 = 11001
Bit.AND(13,25) = 9

Changed: , Category: Bit and Bytes, File: T6C166M1213.htm, ID: 1213