Skip to content

Conversation

@gatzka
Copy link
Contributor

@gatzka gatzka commented May 13, 2025

According to the C11 standard, section 6.5, paragraph 4, the usage of bitwise operators on signed integers is implementation defined. So only unsigned integer constants shall be used for bitwise shifting.

According to the C11 standard, section 6.5, paragraph 4,
the usage of bitwise operators on signed integers is
implementation defined. So only unsigned integer constants
shall be used for bitwise shifting.

Signed-off-by: Stephan Gatzka <stephan.gatzka@gmail.com>
@sonarqubecloud
Copy link


/** Enables pin as input. */
#define GPIO_INPUT (1U << 16)
#define GPIO_INPUT (1U << 16U)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is correct. godbolt says that unsigned shifted by int is still unsigned:

https://godbolt.org/z/MG9on18Ws

And this reference:

https://learn.microsoft.com/en-us/cpp/cpp/left-shift-and-right-shift-operators-input-and-output?view=msvc-170#shifts-and-promotions

says that the resulting type is the result of promoting (unsigned, int) to a common type, which is unsigned.

I also can't find any other use of this in Zephyr.

So I don't think this removes undefined behaviour, but it does make it mildly harder to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, both links doesn't really help. It just shows how a specific compiler behaves, therefore "implementation defined".

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, for the first, but the second is Microsoft's summary of the standard, not their implementation.

Here's the relevant section from cppreference:

https://en.cppreference.com/w/cpp/language/operator_arithmetic "Built-in bitwise shift operators": "Integral promotions are performed on both operands." and "The type of the result is that of lhs."

I couldn't find a copy of the C spec to reference.

Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

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

All of these should be changed to use the BIT() macro instead.

@gatzka
Copy link
Contributor Author

gatzka commented May 19, 2025

All of these should be changed to use the BIT() macro instead.

That makes sense. I'l cancel this PR and make a new one.

@gatzka gatzka closed this May 19, 2025
@gatzka gatzka deleted the fix/gpio_signed_shift branch May 19, 2025 09:12
@gatzka gatzka restored the fix/gpio_signed_shift branch May 19, 2025 09:13
@gatzka gatzka deleted the fix/gpio_signed_shift branch May 19, 2025 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants