Bitwise

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ukimiku
Posts: 101
Joined: Thu Oct 22, 2009 10:45 am

Bitwise

Post by ukimiku » Thu Nov 05, 2009 4:27 pm

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!

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Fri Nov 06, 2009 7:09 am

Not sure how efficient this is, but I cooked this up in ten minutes:

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
You can use the binaryDecode and binaryEncode functions to convert 32-bit integer bytes to rev numbers and back.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

ukimiku
Posts: 101
Joined: Thu Oct 22, 2009 10:45 am

Post by ukimiku » Fri Nov 06, 2009 9:56 pm

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,

Post Reply