I am coming to terms with the bitwise operators found in RunRev. What I seem to be overlooking is an efficient way to bit-shift an unsigned 32-bit integer to the right/left by n binary digits. How do you do that?
Thanks!
Bitwise
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Not sure how efficient this is, but I cooked this up in ten minutes:
You can use the binaryDecode and binaryEncode functions to convert 32-bit integer bytes to rev numbers and back.
HTH,
Jan Schenkel.
Code: Select all
constant kBINLENGTH = 32
on mouseUp
-- construct a number
put 32 into tOriginalNumber
put LPadOrTrim(baseConvert(tOriginalNumber, 20, 2), 32, "0") into tOriginalBinary
put LPadOrTrim(tOriginalBinary & "0", 32) into tBitShiftedBinary
put baseConvert(tBitShiftedBinary, 2, 10) into tBitshiftedNumber
answer tOriginalNumber && "=>" && tBitShiftedNumber
end mouseUp
function LPadOrTrim pString, pLength, pPrefix
local tLeftString
if pPrefix is not empty then
repeat pLength times
put pPrefix after tLeftString
end repeat
end if
return char -pLength to -1 of (tLeftString & pString)
end LPadOrTrim
HTH,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
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
into
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
In general, though, I must ensure that the number does not exceed 32 bits, so I will write:
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
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,
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
Code: Select all
put LPadOrTrim(baseConvert(tOriginalNumber, 10, 2), 32, "0") into tOriginalBinary
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
Code: Select all
put (tOriginalNumber * 2) bitAnd 4294967295 into tBitShiftedNumber
Code: Select all
put tOriginalNumber DIV 32 into tBitShiftedNumber
Again, thanks!
Regards,