[fpc-pascal] Why does this work?

Marco van de Voort marcov at stack.nl
Fri Apr 12 11:55:40 CEST 2013


In our previous episode, Reinier Olislagers said:
> 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!

No, the second takes the remaining 3 bits ( and $7), which is the
bit number inside the byte, and does (1 shl that value) which is like 2 to the power that
value. IOW it changes a bit index to a bit mask.

So the first line calculates the position of a byte from a bit index. The
second takes the remainining bits of the index inside the byte and tests
it.  The function returns true if it is false (=0) btw.
 
>     Src := PChar(Src) + FNullField.Offset + (AFieldDef.NullPosition shr 3);
>     Result := (PByte(Src)^ and (1 shl (AFieldDef.NullPosition and $7))) = 0;




More information about the fpc-pascal mailing list