[fpc-pascal] Why does this work?

Reinier Olislagers reinierolislagers at gmail.com
Fri Apr 12 11:47:33 CEST 2013

As a certified high-level coder with a fear for anything that smells
like pointers and bits I hesitated to post this as the answer may well
be: figure it out yourself, it's obvious ;)... but at least it's worth a
laugh to you bitpushers out there.

Have mercy ;)

Going through the dbase code, I hit this code:
(TDbfFile.UpdateNullField in db_dbffile)

src, dst: pointers (to buffers).

FNullField is a field that has a bitmap of flags for setting a field to
NULL: bit 0=first field, bit 1=second field etc.
Those bits are stored beginning in FNullField.Offset; it can span
multiple bytes (apparently stored BE: the first field goes into the
lower byte, the 8th field in the next byte).

I have a lot of trouble with shr and shl going on. The first line
seemingly drops the three lower bits... but why?
... and some bits are shifted back again in the next line? Arggh!

  if (FNullField <> nil) and (Dst = nil) and (AFieldDef.NullPosition >=
0) then
    Src := PChar(Src) + FNullField.Offset + (AFieldDef.NullPosition shr 3);
    Result := (PByte(Src)^ and (1 shl (AFieldDef.NullPosition and $7))) = 0;

Any suggestions?


More information about the fpc-pascal mailing list