[fpc-pascal] two small ?s - high(real) and nearest 2^x
Jürgen Hestermann
juergen.hestermann at gmx.de
Sat Oct 10 19:34:54 CEST 2009
> 2. For the purposes of reserving memory in block sizes that can be
> easily reallocated, I like to use powers of two. So if I have, e.g., a
> dynamic array, I might start with a size of 1024 and then double it
> when it hits capacity. Hopefully this smoothes memory management, as I
> am using a lot of memory...
I have done this too. I maintain 2 values, one for the *allocated*
memory and one for the *used*. This way I don't need to reallocate
unless I hit the border. If this happens, I simply allocate a new chunk
of memory of double size (which is then again a power of 2) and
move(copy) the data to the new memory. This way I don't have to
reallocate very often because the additional memory increases by a
factor of 2 which makes it less propably for large numbers of bytes.
> Is there
> some clever way to request that the processor return the position of
> the leftmost '1' in 00101101?
The only way I can think of is a shr (or shl) loop which should run as
often as number of bits exist in the worst case;
i := 0;
while x<>0 do
begin
x := x shr 1;
inc(i);
end;
if i<>0 then
x := 1 shl i
else
error
if X=0 then overflow;
More information about the fpc-pascal
mailing list