Mailing List Archive: 49091 messages

## [REBOL] Re: Arithmetic shifts

### From: carl:cybercraft at: 19-Aug-2004 0:52

```
>> And using AND's quicker than TO INTEGER! too...
>>
>> >> t: now/time/precise loop 1000000 [3 and 254 / 2]print now/time/precise -
>t
>> 0:00:01.59
>> >> t: now/time/precise loop 1000000 [3 and 254 / 2]print now/time/precise -
>t
>> 0:00:01.59
>> >> t: now/time/precise loop 1000000 [3 and 254 / 2]print now/time/precise -
>t
>> 0:00:01.54
>> >> t: now/time/precise loop 1000000 [to integer! 3 / 2]print
>now/time/precise - t
>> 0:00:02.85
>> >> t: now/time/precise loop 1000000 [to integer! 3 / 2]print
>now/time/precise - t
>> 0:00:02.86
>> >> t: now/time/precise loop 1000000 [to integer! 3 / 2]print
>now/time/precise - t
>> 0:00:02.8
>>
>
>it may be faster but what results are you getting?
>
>I see things like
>>> 1  and  3 / 2
>== 0.5
>
>and it does not fill me with confidence that you are doing what you hope
>to be doing, precedence is left to right so what you may be seeing is
>
>>> 3 and 254
>== 2
>>> 2 / 2
>== 1
>
>>> 3 and 255 / 2
>== 1.5

Anyway, using AND only makes sense if you're used to playing with Boolean arithmetic.
But as you can see by the times returned above, the gains can be worth it.  Using 254
(11111110 in binary) for divisions by 2 only works with 8-bit numbers though - if you
had larger numbers, say up to 16 bit, you'd need to use 65534 (11111111 11111110 in binary),
and larger numbers up to 32 bit.

Which has just got me thinking.  Does this answer Jorge's negative value problem...?

>> x: -2
== -2
>> 4 and x / 2
== 2
>> 3 and x / 2
== 1
>> 2 and x / 2
== 1
>> 1 and x / 2
== 0
>> 0 and x / 2
== 0
>> -1 and x / 2
== -1
>> -2 and x / 2
== -1
>> -3 and x / 2
== -2

The -2 there works (I think) because it creates a signed 32bit value with just the least
signifigant bit set to 0.  (You'd up the value for greater powers of 2.)

Jorge - would this work for you?