[fpc-pascal] Why does this work?

Ludo Brands ludo.brands at free.fr
Fri Apr 12 12:01:42 CEST 2013


On 04/12/2013 11:47 AM, Reinier Olislagers wrote:
>   if (FNullField <> nil) and (Dst = nil) and (AFieldDef.NullPosition >=
> 0) then
>   begin
>     Src := PChar(Src) + FNullField.Offset + (AFieldDef.NullPosition shr 3);
>     Result := (PByte(Src)^ and (1 shl (AFieldDef.NullPosition and $7))) = 0;
>     exit;
>   end;
> 

NullPosition is a bit position, 8 bits in a byte. To find bit x you have
to look in byte Start + x div 8. x div 8 can be written as x shr 3. Now
that you have found the byte where bit x is you need to isolate that
bit. Its position in the byte is X mod 8 which you can write as X and
$7. The 1 shl (X and $7) is done to create a mask to get the correct bit.

Ludo




More information about the fpc-pascal mailing list