## Re:Shifting bits in bytes/words/longs...

In article <krajcard.11.31F68...@ohsu.edu>, krajc...@ohsu.edu writes

##### Quote

>>Does anyone have nny suggestions on how to do this in 4 byte chunks (its

>>much faster)? Or am I stuck with doing it in 1 byte chunks (slow)?

>Raymond:

>I'm curious how you are doing it. I'm not really into this, but looked up

>bitwise operators in the help file. There are some operators that appear to

>do this for you. Also, you can build a case record type that might help. I'm

>probably not using the right terms here, so let me give an example (though

>it's not complete. I haven't played with this for a few years, so the syntax

>may not be right, but the idea works!))

>procedure move_them;

>var

> something: record case boolean of

> 1: (x:array [0..100] of integer;

> xf: integer);

> 2: (yf: integer;

> y: array [0..100] of integer);

> end;

> hold: array [0..100] of integer;

>begin

> move (something.x,hold,101); {move it to the hold}

> move (hold,something.y,101); {move it back in but offset by 1}

> something.yf := 0; {set the leading integer value}

>end;

>Hopefully, this give you the idea, more or less. Of course, I may not have

>understood the problem to begin with either!

This works fine if you want to shift by a multiple of 16 bits.

Unfortunately I need to shift by an arbitrary number of bits (eg: 17

bits). I do actually use a very similar approach to by moving the block

so that all of the bits are < 8 bits away from their correct location.

The second phase involves moving through the block and using SHL/SHR

operations move the bits to their correct location. e.g. shift them left

by shift bits:

type

double_byte_array : array[1..2] of byte; // I want this to be Integer,

// as its 4 timnes faster.

var

p : Pointer; // Points to memory to shift by < 8 bits

x : ^double_byte_array;

...

for i := 1 to num_to_do do

begin

x := double_byte_array(P);

x^[1] := (x^[1] SHL Shift) OR (x^[2] SHR (8 - shift));

inc(p);

end;

...

Another kind soul has suggested using multiply/divide instead of

SHL/SHR. Perhaps this is the way Delphi implements SHL/SHR?

Raymond.

--

Raymond Wilson