Skip to content

avoid negative left shifts#1

Open
Smattr wants to merge 1 commit intoclementine-m:masterfrom
Smattr:9daec7fa-3a0b-40e3-94e2-43e85a21cd3c
Open

avoid negative left shifts#1
Smattr wants to merge 1 commit intoclementine-m:masterfrom
Smattr:9daec7fa-3a0b-40e3-94e2-43e85a21cd3c

Conversation

@Smattr
Copy link

@Smattr Smattr commented May 3, 2017

Very handy utility @clementine-m, thanks for writing this! I came across the following while building with a recent Clang.


The literal 0 is treated as a signed int, which results in the expression
~0 << ... performing a left shift of a negative number. This is undefined
behaviour in C which newer GCC and Clang will warn about with
-Wshift-negative-value. This commit uses unsigned long literals to ensure the
shifts are interpreted correctly. For the record, all the compilers I have on
hand handle this code correctly, but other compilers may not.

The literal "0" is treated as a signed int, which results in the expression
"~0 << ..." performing a left shift of a negative number. This is undefined
behaviour in C which newer GCC and Clang will warn about with
-Wshift-negative-value. This commit uses unsigned long literals to ensure the
shifts are interpreted correctly. For the record, all the compilers I have on
hand handle this code correctly, but other compilers may not.
@su8
Copy link

su8 commented Apr 1, 2018

Not only that but he uses %lu with long int, instead %ld.

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