M
Markus Hahn
While coding some binary data handling I found this IMHO strange behavior:
Dim bToShift As Byte = 1
bToShift <<= 9
Console.WriteLine("and the value is {0}", bToShift)
The output is "2", I expected it to be zero (outshifted bits don't come back).
The rule I determined is: whatever the shift operator S is, its real value is (S mod 8).
Is this as designed? I read the VB lanuage specification, but I cannot find anything there which would explain it. Looking at the IL
code confirms the shift value.
The real world problem I experienced wassomething like
Dim b as Byte()
Dim nReg as Integer
...
nReg = (b(0) << 24) or ((b1 and &hFF) << 16)
with (b(0) << 24) not chaging anything(!) The following solution
nReg = (CType(b(0), Integer)) << 24 or ((b1 and &hFF) << 16)
solved the problem. What's confusing is that the AND operator apparently enforces a Byte2Integer conversion automatically
Does anybody have similar experiences/solutions/explanations?
-markus
Dim bToShift As Byte = 1
bToShift <<= 9
Console.WriteLine("and the value is {0}", bToShift)
The output is "2", I expected it to be zero (outshifted bits don't come back).
The rule I determined is: whatever the shift operator S is, its real value is (S mod 8).
Is this as designed? I read the VB lanuage specification, but I cannot find anything there which would explain it. Looking at the IL
code confirms the shift value.
The real world problem I experienced wassomething like
Dim b as Byte()
Dim nReg as Integer
...
nReg = (b(0) << 24) or ((b1 and &hFF) << 16)
with (b(0) << 24) not chaging anything(!) The following solution
nReg = (CType(b(0), Integer)) << 24 or ((b1 and &hFF) << 16)
solved the problem. What's confusing is that the AND operator apparently enforces a Byte2Integer conversion automatically
Does anybody have similar experiences/solutions/explanations?
-markus