Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Debug safety: shifting by >= bit width #403
The decision is to have the type of the right hand side be an integer with a log2 bit count of the left hand side.
So if shifting a
There's an issue when the left hand side is a non-power-of-2 integer type, such as
Due to this issue, I'm adding these primitive types:
Having to shift with by a number == bit width has come up a fair few times in code I've been writing.
For example the following will get a compile error.
The current workaround is simply to separate the cases for possibly larger shifts.
This is a little ugly, and unfortunately we can't really seem to get around this without changing the types allowed as shift arguments (which is a nice solution in general for all other values).
I don't think the default should be changed, but a possible alternative could be to add an extra
That's interesting. I think this is evidence that this safety is really important, because that ugly if statement is what you have to do to avoid undefined behavior. If we added a builtin function
Maybe this can simply be in the standard library?
we currently have:
we could introduce: