Skip to content

Fix bytes_required over-allocation for negative powers of two#3548

Open
ArjunPakhan wants to merge 1 commit into
apache:mainfrom
ArjunPakhan:fix-decimal-bytes-required
Open

Fix bytes_required over-allocation for negative powers of two#3548
ArjunPakhan wants to merge 1 commit into
apache:mainfrom
ArjunPakhan:fix-decimal-bytes-required

Conversation

@ArjunPakhan

Copy link
Copy Markdown

Description

Fixes an issue in bytes_required where negative numbers that are exact powers of two (e.g., -128, -32768) were over-allocated an extra byte of space.

Context & Root Cause

In two's complement representation, exact negative powers of two are clean boundaries where the Most Significant Bit (MSB) naturally represents the sign without requiring an extra padding bit. For example, -128 fits perfectly within a single signed byte (0x80).

The previous implementation calculated the byte length using Python's native .bit_length() directly on the integer magnitude:

return (value.bit_length() + 8) // 8

@ebyhr ebyhr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an existing PR: #3523

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

Successfully merging this pull request may close these issues.

2 participants