java-native-access / java-native-access/jna

Support bit fields in structs

Open
#423 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
8.9k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

JNA should support bit fileds in structs. Currently users are forced to read/set bits by hand, this is error prone and boiler plate.

Given this sample structure:

typedef struct VALUES{
  WORD fVal1  :4;
  WORD fVal2  :2;
  WORD fVal3  :3;
  WORD fDummy  :7;
} 

It would be nice if it could be written like this with JNA:

public static class VALUES extends Structure {
  @BitSize(4) byte fVal1;
  @BitSize(2) byte fVal2;
  @BitSize(3) byte fVal3;
  @BitSize(7) byte fDummy;
  ...
} 

I guess it would not be that simple, cause JNA needs to know the field order and the target type, were the bits should be written to (in this case a 16bit-int).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JNA's Structure field layout and native type handling, then determine how declared bit-sized fields would be ordered and packed into the 16-bit target shown in the C example. Done means the sample VALUES mapping can read and write its stated bit fields without manual masking, with the field order and widths handled consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, java
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.