Binary "Shift Left" operator?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Binary "Shift Left" operator?

david bovill
I am reverse engineering a Javascript function which uses a "shift left"
operator:

> Shift Left Operator
>
> The shift left operator is binary. The syntax is
>
> *operand1* << *operand2*
>
>  You obtain the result of the expression by treating *operand1* as a set
> of 32 bits and shifting the bits left by the value in *operand2*.
> High-order bits are discarded, and the low-order bits are filled with 0's.
>
> Example:
>
> var tmp = 31 << 3;
>
>  In this example, *operand1* is 31 (00000000000000000000000000011111) and
> *operand2* is 3. tmp is set to 248 (00000000000000000000000011111000).
>
Is there an equivalent in Rev - if not can anyone translate this to some
basic math functions?
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: Binary "Shift Left" operator?

Colin Holgate
At 5:23 PM +0100 4/29/08, David Bovill wrote:
>Is there an equivalent in Rev - if not can anyone translate this to some
>basic math functions?

Shift left is the same as * 2. 31 << 3 would be the same as * 8.

What was it that you needed to achieve?

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: Binary "Shift Left" operator?

david bovill
In reply to this post by david bovill
2008/4/29 Colin Holgate <[hidden email]>:

> At 5:23 PM +0100 4/29/08, David Bovill wrote:
>
> > Is there an equivalent in Rev - if not can anyone translate this to some
> > basic math functions?
> >
>
> Shift left is the same as * 2. 31 << 3 would be the same as * 8.
>
> What was it that you needed to achieve?
>

I'm making a start on the Map library as part of a suite of open source API
libraries. This JavaScript function is for a Mercator Projection used in
Google maps amongst others. The script looks like this:

function Adjust(x,y,deltaX,deltaY,z)

> {
> var offset=268435456;
> var radius=offset/Math.PI;
>
> function LToX(x)
> {
> return Math.round(offset+radius*x*Math.PI/180);
> }
>
> function LToY(y)
> {
> return Math.round(offset-radius*Math.log((1+Math.sin(y*Math.PI/180))/(1-Math.sin(y*Math.PI/180)))/2);
> }
>
> function XToL(x)
> {
> return ((Math.round(x)-offset)/radius)*180/Math.PI;
> }
>
> function YToL(y)
> {
> return (Math.PI/2-2*Math.atan(Math.exp((Math.round(y)-offset)/radius)))*180/Math.PI;
> }
>
> return {x:XToL(LToX(x)+(deltaX<<(21-z))),y:YToL(LToY(y)+(deltaY<<(21-z)))};
> }
>
>
And its the last line that looks tricky. The

> deltaX<<(21-z)
>
> is the bit I guess i am looking most to figure out?
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: Binary "Shift Left" operator?

Colin Holgate
Not sure how many bits of arithmetic are being assumed there, but
shift would also have a modulus effect. If I'm right, and if it's 32
bit, n << 21 might be like n mod 512 * 2^21.

I just made this:

http://staff.funnygarbage.com/colin/mapcalc.swf

It will let you play with values while you try to work out the relationships!

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: Binary "Shift Left" operator?

mwieder
In reply to this post by david bovill
David-

>> deltaX<<(21-z)
>>
>> is the bit I guess i am looking most to figure out?

Howz about

repeat (21-z) times
 put deltaX * 2 into deltaX
end repeat

--
 Mark Wieder
 [hidden email]



_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
--
 Mark Wieder
 ahsoftware@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Binary "Shift Left" operator?

masmit
In reply to this post by david bovill
As Mark and Colin said, shift left is just multiplying by 2 however  
many times, but assuming the numbers are 32 bit positive integers,  
you have to get rid of anything above 2^32, so I think this does it:

function shiftLeft pNum, pSteps
    if pNum = 0 then return 0
    if pSteps is empty then put 1 into pSteps
    put pNum * (2 ^ pSteps) into tShiftedNum
    if tShiftedNum > 2 ^ 32  then subtract 2 ^ 32 from tShiftedNum
    return tShiftedNum
end shiftLeft

Best,

Mark


On 29 Apr 2008, at 18:34, David Bovill wrote:

> 2008/4/29 Colin Holgate <[hidden email]>:
>
>> At 5:23 PM +0100 4/29/08, David Bovill wrote:
>>
>>> Is there an equivalent in Rev - if not can anyone translate this  
>>> to some
>>> basic math functions?
>>>
>>
>> Shift left is the same as * 2. 31 << 3 would be the same as * 8.
>>
>> What was it that you needed to achieve?
>>
>
> I'm making a start on the Map library as part of a suite of open  
> source API
> libraries. This JavaScript function is for a Mercator Projection  
> used in
> Google maps amongst others. The script looks like this:
>
> function Adjust(x,y,deltaX,deltaY,z)
>> {
>> var offset=268435456;
>> var radius=offset/Math.PI;
>>
>> function LToX(x)
>> {
>> return Math.round(offset+radius*x*Math.PI/180);
>> }
>>
>> function LToY(y)
>> {
>> return Math.round(offset-radius*Math.log((1+Math.sin(y*Math.PI/
>> 180))/(1-Math.sin(y*Math.PI/180)))/2);
>> }
>>
>> function XToL(x)
>> {
>> return ((Math.round(x)-offset)/radius)*180/Math.PI;
>> }
>>
>> function YToL(y)
>> {
>> return (Math.PI/2-2*Math.atan(Math.exp((Math.round(y)-offset)/
>> radius)))*180/Math.PI;
>> }
>>
>> return {x:XToL(LToX(x)+(deltaX<<(21-z))),y:YToL(LToY(y)+(deltaY<<
>> (21-z)))};
>> }
>>
>>
> And its the last line that looks tricky. The
>
>> deltaX<<(21-z)
>>
>> is the bit I guess i am looking most to figure out?
> _______________________________________________
> use-revolution mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: Binary "Shift Left" operator?

Colin Holgate
At 3:58 PM +0100 4/30/08, Mark Smith wrote:
>function shiftLeft pNum, pSteps
>    if pNum = 0 then return 0
>    if pSteps is empty then put 1 into pSteps
>    put pNum * (2 ^ pSteps) into tShiftedNum
>    if tShiftedNum > 2 ^ 32  then subtract 2 ^ 32 from tShiftedNum
>    return tShiftedNum
>end shiftLeft

Did you read my other message, where I suggested that doing a mod
would solve the same issue?

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: Binary "Shift Left" operator?

masmit
Colin, - not until too late!...

Mark

On 30 Apr 2008, at 16:08, Colin Holgate wrote:

> At 3:58 PM +0100 4/30/08, Mark Smith wrote:
>> function shiftLeft pNum, pSteps
>>    if pNum = 0 then return 0
>>    if pSteps is empty then put 1 into pSteps
>>    put pNum * (2 ^ pSteps) into tShiftedNum
>>    if tShiftedNum > 2 ^ 32  then subtract 2 ^ 32 from tShiftedNum
>>    return tShiftedNum
>> end shiftLeft
>
> Did you read my other message, where I suggested that doing a mod  
> would solve the same issue?
>
> _______________________________________________
> use-revolution mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution