Lanting Guo
Lanting Guo

Reputation: 805

how to deal with bit fields in Julia when writing a wrapper of C

typedef struct {
uint32_t is_bin:1, is_write:1, is_be:1, is_cram:1, dummy:28;
int64_t lineno;
kstring_t line;
char *fn, *fn_aux;
union {
    BGZF *bgzf;
    struct cram_fd *cram;
struct hFILE *hfile;
    void *voidp;
} fp;
htsFormat format;
} htsFile;

how to deal with the bit fields part of htsFile in Julia? If I am not care about the specific value uint32_t is_bin:1, is_write:1, is_be:1, is_cram:1, dummy:28;, can I just use a Cuint variable to repalce it in julia?

Upvotes: 0

Views: 333

Answers (2)

tholy
tholy

Reputation: 12179

You can extract the individual bits using julia'a bitwise manipulation functions (&, >>, etc.) You could define convenience functions like is_bin(x) = (x & 0x01) > 0.

Upvotes: 1

Isaiah Norton
Isaiah Norton

Reputation: 4366

can I just use a Cuint variable to replace it in julia?

Yes, the alignment will be the same. If you do need the values, you can use bitmasks to select the required bit(s).

As a side note, the StrPack package may be of interest for dealing with complicated struct [de]serialization (although I don't think it has built-in bitfield support).

Upvotes: 2

Related Questions