Skip to content

Unclear meaning of bitsize of an array #23781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ericlangedijk opened this issue May 4, 2025 · 6 comments
Open

Unclear meaning of bitsize of an array #23781

ericlangedijk opened this issue May 4, 2025 · 6 comments

Comments

@ericlangedijk
Copy link

ericlangedijk commented May 4, 2025

I encountered an unclear language thing, which - as a simple user - was surprising me.

pub const Letter = packed struct {
    machinechar: u5 = 0,
    is_blank: bool = false,
}
const Letters7 = [7]Letter;

The sizeOf Letters7 returns obviously 7, but bitSizeOf returns 54.

Where are the 2 missing bits?
The meaning of the bitsize is unclear in my opinion and should maybe be clarified somewhere (if possible).
As far as I understand the bitsize is the size which it would occupy in a packed struct.
However: Letters7 is not allowed by the compiler to reside inside a packed struct.
The reason that I am posting this issue is, is the question:
Is this a little inconsistency in the language?

@alexrp
Copy link
Member

alexrp commented May 4, 2025

Arrays are not bit-packed. @bitSizeOf(Letter) == 6, so when you put these in an array, there will be 2 extra bits of padding per item.

@Rexicon226
Copy link
Contributor

Should note that it's 2 extra bits of padding per item, except the last one. So (6 + 2) * 7 - 2 is 54.

@ericlangedijk
Copy link
Author

Yes that I understand. Array items are not bitpacked, but still these last 2 bits are chopped off inside the bitSizeOf builtin function.
If this little array could reside in a packed struct it would be logical...

@190n
Copy link
Contributor

190n commented May 5, 2025

Should @bitSizeOf even be allowed for types that can't appear in a packed struct or union?

@alexrp
Copy link
Member

alexrp commented May 5, 2025

cc @mlugg

@mlugg
Copy link
Member

mlugg commented May 5, 2025

Intended @bitSizeOf semantics are defined by the accepted, but not yet implemented, proposal #19755. The current semantics are indeed kinda weird in places; don't pay them too much attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants