Board index » delphi » Shifting and Rotating bits in Pascal

Shifting and Rotating bits in Pascal

Could someone tell me how to rotate the bits of a number in pascal? How do you
shift bits?

e-mail please.

Thanks.

-Dan

Re:Shifting and Rotating bits in Pascal

At 05:34 AM 11/27/96 GMT, Daniel Jette wrote:

Quote

>Could someone tell me how to rotate the bits of a number in pascal? How do you
>shift bits?

>e-mail please.

If you use Turbo Pascal 6.0 or 7.0 you can use assembler instructions
ror/rol, sal,shl, shr and sar as well as rcr and rcl. Look it up in any cpu
or assembler manual.

Hope I helped (if you need further info - e-mail me).
--------------------------------------------------
Ognen Duzlevski, IS Ltd. - a WIN programming house
--------------------------------------------------
tel: ++389 91 219-049, 233-467, 221-119
fax: ++389 91 219-049
e-mail: ogn...@lotus.mpt.com.mk
--------------------------------------------------
Vera Jocic 23/1-3, 91000 Skopje, Rep. of Macedonia

Re:Shifting and Rotating bits in Pascal

Quote
> From: Daniel Jette <bm...@FreeNet.Carleton.CA>
> Newsgroups: comp.lang.pascal.misc
> Subject: Shifting and Rotating bits in Pascal
> Date: Wednesday, November 27, 1996 1:34 PM

> Could someone tell me how to rotate the bits of a number in pascal? How
do you
> shift bits?

[Shifting]

temp := temp shl 1;  { ie to shift temp 1 bit to the left - this is same as
temp x 2 except its tenfold faster}
temp := temp shr 1;  { ie to shift temp 1 bit to the right - this is same
as temp div 2 except its tenfold faster}

[Rotating]

asm
mov ax,temp  { rotate temp right once }
ror ax,1
mov temp,ax
end;

asm
mov ax,temp  { rotate temp left once }
rol ax,1
mov temp,ax
end;

---------
From Vinson ABS, Singapore.

Re:Shifting and Rotating bits in Pascal

On 21 Dec 1996 18:10:06 GMT,  "Vinson ABS"

Quote
<illus...@singnet.com.sg> wrote:
>> From: Daniel Jette <bm...@FreeNet.Carleton.CA>
>> Newsgroups: comp.lang.pascal.misc
>> Subject: Shifting and Rotating bits in Pascal
>> Date: Wednesday, November 27, 1996 1:34 PM

>> Could someone tell me how to rotate the bits of a number in pascal? How
>do you
>> shift bits?

>[Shifting]

>temp := temp shl 1;  { ie to shift temp 1 bit to the left - this is same as
>temp x 2 except its tenfold faster}
>temp := temp shr 1;  { ie to shift temp 1 bit to the right - this is same
>as temp div 2 except its tenfold faster}

Not quite.  "SHR 1" is NOT the same as "DIV 2".  SHR shifts zero
bits into the most-significant position, while DIV preserves the
sign of the original number.  The DIV equivalent shift in
assembler is usually listed as "arithmetic shift right", which
shifts everything right while replicating the sign bit.

There is usually no comparable ASR shift in Pascal.

Of course if Temp is a word, then the value is always positive
which would then have SHR 1 and DIV 2 yielding similar results.

Quote

>[Rotating]

>asm
>   mov ax,temp  { rotate temp right once }
>   ror ax,1
>   mov temp,ax
>end;

in 80x86 assembler you could simply use "ror [temp],1"

in Pascal  Temp := (Temp shr 1) + (temp shl 15)

If your version of Pascal doesn't support shr/shl, then it
becomes a little more difficult as you have to remove the
replicated sign if the number was negative.

Quote

>asm
>   mov ax,temp  { rotate temp left once }
>   rol ax,1
>   mov temp,ax
>end;

in 80x96 assembler you could simply use "rol [temp],1"

in Pascal  Temp := (Temp shl 1) + (temp shr 15)
or  Temp := Temp * 2 + Ord(Temp < 0)

If Temp is a word, then Temp * 2 + Ord(Temp > 32767)

Quote

>---------
>From Vinson ABS, Singapore.

--
I have moved:
email: rdon...@southeast.net
http://users.southeast.net/~rdonais/index.html
ftp://users.southeast.net/~rdonais/00index.txt

Re:Shifting and Rotating bits in Pascal

Quote
> R.E.Donais <rdon...@southeast.net> wrote in article

<32c1a0f4.7384...@news.southeast.net>...
Quote

> Not quite.  "SHR 1" is NOT the same as "DIV 2".  SHR shifts zero
> bits into the most-significant position, while DIV preserves the
> sign of the original number.  The DIV equivalent shift in
> assembler is usually listed as "arithmetic shift right", which
> shifts everything right while replicating the sign bit.

> There is usually no comparable ASR shift in Pascal.

> Of course if Temp is a word, then the value is always positive
> which would then have SHR 1 and DIV 2 yielding similar results.

thank you for pointing out the fact I've missed out.
i see one side of the coin and neglect the other. (actually, I never do
this with signed numbers before.)

SHR 1 is equal to DIV 2 when only when the operand is an unsigned. ie byte,
word, dword. also, this kind of operations should only occurs in areas
where speed is most critical. eg update graphics screen.... I think this is
called Base-2 mathematic. Heavy-weight ASM instructions like MUL,IMUL, IDIV
and DIV take up a lot of time to execute. Base-2 maths is very much faster
because it require only shifting.

Let's take a graphics function as example.
This is an unoptimized Mode 13h "PutPixel" function.

Procedure PutPixel(X,Y : integer; Color : byte);
Begin
Mem[\$A000:(Y *  320) + X] := Color;  { unoptimized method }
End;

This is an optimized version of above.

Procedure PutPixel(X,Y : integer; Color : byte);
Begin
Mem[\$A000:(y shl 8) + (y shl 6) + x] := Color;  { compute address using
Base-2 }
End;

The end-result is the same but the optimized version is at least 2 times
faster than the first.

Here's how to do Base-2 maths.

1.)  convert the multiplier into binary digits. (in above eg, 320 =
101000000b)
2.)  you see that bit 6 and bit 8 are set. (counting from
0.1.2.3.4.5.*6*.7.*8*...)
3.)  the formula is just (Y shl 8) + (Y shl 6) = 320 x Y.

As for division, I heard it is more difficult to do and I don't need it
too. So, I can't say much on it. Perhaps someone can explain both even
better. Although many people already knew this, I believe this is good
knowledge for newbies.

This method only weakness is that the code is fixed. But hey, if
performance matters, use it!

From Vinson ABS.