Skip to content
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

word(h,l) uses int expression instead of unsigned #107

Closed
PeterSommerlad opened this issue Mar 25, 2020 · 2 comments
Closed

word(h,l) uses int expression instead of unsigned #107

PeterSommerlad opened this issue Mar 25, 2020 · 2 comments

Comments

@PeterSommerlad
Copy link

uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8) | l; }

uses int instead of unsigned for its expression. While not absolutely critical it could lead to negative numbers that are then converted to unsigned. Either cast h to uint16_t or simpler use u suffix for the constant 8. Then the integral promotion rules will use unsigned for the shift.

uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8u) | l; }
@oqibidipo
Copy link

The right operand of << does not affect the result type.

C

Constraints
Each of the operands shall have integer type.
Semantics
The integer promotions are performed on each of the operands. The type of the result is
that of the promoted left operand. ...

C++

The operands shall be of integral or unscoped enumeration type and integral promotions are performed.
The type of the result is that of the promoted left operand. ...

@PeterSommerlad
Copy link
Author

Yeah, I got the rules wrong in my head. You are right, there is not significant integral promotion happening here.

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

2 participants