Jan,
thanks a bunch for your work!
I understood from your code that you want to shift a binary representation of the 32 by one binary digit to the left and output the result in decimal notation. When I clicked to activate your code, the window displayed "32 => 124", which, of course, could not be, as 32 shifted to the left by one bit yields 64, not 124.
So I changed your line
Code: Select all
put LPadOrTrim(baseConvert(tOriginalNumber, 20, 2), 32, "0") into tOriginalBinary
into
Code: Select all
put LPadOrTrim(baseConvert(tOriginalNumber, 10, 2), 32, "0") into tOriginalBinary
since the original 32 was given as a base-10-number.
Then your code worked like a charm! Thanks!
P.S. I recalled that shifting a binary to the left by 1 bit is equal to multiplying it by 2. So your code could be condensed into
Code: Select all
answer tOriginalNumber && "=> " & tOriginalNumber * 2
In general, though, I must ensure that the number does not exceed 32 bits, so I will write:
Code: Select all
put (tOriginalNumber * 2) bitAnd 4294967295 into tBitShiftedNumber
As for shifting to the right by one bit, one could DIV by 2 and just take the result. Accordingly, shifting to the right by, say, 5 digits could be done by
Code: Select all
put tOriginalNumber DIV 32 into tBitShiftedNumber
When I wrote my post, asking for an efficient way to bit-shift binary numbers, I knew about the operators bitAnd, bitOr, bitXOR, but had not found anything like the Javascript << and >> operators. I guess I will have to live with that, knowing that multiplication by powers of 2 will probably be handled quite efficiently by any decent compiler.
Again, thanks!
Regards,