Thanks Norman, taking what you said into account, here’s where I’m at:
[code]dim intTest as uint32 = &b1111 '4 x 1 bits
intTest = ShiftLeft(intTest, 3, 2) 'Returns &b0
intTest = ShiftLeft(intTest, 3, 3) 'Returns &b0
intTest = ShiftLeft(intTest, 3, 4) 'Returns &b1000
intTest = ShiftLeft(intTest, 3, 5) 'Returns &b11000
intTest = ShiftLeft(intTest, 3, 6) 'Returns &b111000
intTest = ShiftLeft(intTest, 3, 7) 'Returns &b1111000
intTest = ShiftLeft(intTest, 3, 8) 'Returns &b1111000 - the same as 7 numbits[/code]
So it seems when shifting left using Numbits:
(a) Numbits must > Shift and
(b) all high order bits beyond numbits are set to 0 and
© vacated lower order bits are set to 0.
OK, if that’s the deal, that’s the deal… BUT when shifting right:
[code]dim intTest as uint32 = &b11111 '5 x 1 bits
intTest = ShiftRight(intTest, 3, 2) 'Returns &b0
intTest = ShiftRight(intTest, 3, 3) 'Returns &b0
intTest = ShiftRight(intTest, 3, 4) 'Returns &b10001
intTest = ShiftRight(intTest, 3, 5) 'Returns &b11
intTest = ShiftRight(intTest, 3, 6) 'Returns &b11 - the same as 5 numbits[/code]
This is a deal breaker, because there’s no rhyme or reason for the results in relation to the original serial order of intTest
bits.
I suspect the bug therefore, as your code seems to prove, is that Xojo has implemented the LeftShift Numbits algorithm for RightShift Numbits too (save for the shifting itself) accounting for unexpected results…
If so, having correctly deduced the relationship between Anding, Xoring, and Oring (which I could not see) for LeftShift , have you any clue as to what the RightShift Numbits algorithm might look like to produce expected results?
As for me, looks like I will have to continue down my present path of knocking unwanted bits off by shifting left then right again (or right and left again). I guess Shifts should be cheaper anyway as no need to load another value from memory or to compare it - although with out-of-order / speculative execution only God knows what’s really happening under the hood.