Nim icon indicating copy to clipboard operation
Nim copied to clipboard

testBit should work with BackwardsIndex

Open rotu opened this issue 3 years ago • 0 comments

Summary

testBit in std/bitops should be able to accept an index from the end of a type.

Description

To get the msb bit of an integer, you currently have to do something like:

testBit(x, BitsRange[typeof x].high) or testBit(x, sizeof(x)*8-1)

It would be nicer if this could be simply testBit(x, ^1)

Note that a naive implementation would be slightly less than ideal since, unlike range, BackwardsIndex is not bounds-checked at compile time. E.g. [0,1,2][5] fails at compile time but [0,1,2][^4] fails at runtime.

Alternatives

No response

Standard Output Examples

No response

Backwards Compatibility

No response

Links

No response

rotu avatar Sep 09 '22 21:09 rotu