[fpc-pascal] Converting C's bit field struct

Vinzent Höfler JeLlyFish.software at gmx.net
Wed Aug 13 12:59:29 CEST 2008


leledumbo wrote:
> Since no one answers on message board, I'll post it here.
> 
> C struct:
> 
> typedef unsigned int u32int;
> 
> typedef struct page
> {
>    u32int present    : 1;   // Page present in memory
>    u32int rw         : 1;   // Read-only if clear, readwrite if set
>    u32int user       : 1;   // Supervisor level only if clear
>    u32int accessed   : 1;   // Has the page been accessed since last
> refresh?
>    u32int dirty      : 1;   // Has the page been written to since last
> refresh?
>    u32int unused     : 7;   // Amalgamation of unused and reserved bits
>    u32int frame      : 20;  // Frame address (shifted right 12 bits)
> } page_t;

Hmm. That's nasty. ;)

 > Pascal record please...

Should be something like that:

type
    Unsigned_7  = 0 .. (1 shl 7)  - 1;
    Unsigned_20 = 0 .. (1 shl 20) - 1;

type
    page_t = bitpacked record
       present  : boolean;
       rw       : boolean;
       user     : boolean;
       accessed : boolean;
       dirty    : boolean;
       unused   : Unsigned_7;
       frame    : Unsigned_20;
    end;

I guess, portability is of no concern with such a structure. ;)



Vinzent.



More information about the fpc-pascal mailing list